Python basis (b)

1. assignment

(1) Action: assignment definitions +

(2) compound with chain assignment

a = b = 2
a *= 2

(3) a special type of assignment

Sequence of assignment:

a,b,c = (1,2,3)
print(a,b,c)

Variable exchange:

a,b = b,a

No additional temp variable.

2. Comment

Starting with # line is the comment.

3. Operator

(1) Arithmetic Operators

Operators meaning
+ plus
- Less
* Multiply
/ except
% Modulo
// Divisible (not a comment ....)
** power

(2) relational operators

Operators meaning
== equal
! = Or <> not equal to
> more than the
< Less than
>= greater or equal to
<= Less than or equal

(3) assignment operator

Operators meaning
= Assignment
+= x + = 3 is equivalent to x = x + 3
-= x- = 3 is equivalent to x = x-3
*= X =. 3 is equivalent to X = X . 3
/= x / = 3 is equivalent to x = x / 3
%= It is equivalent to x% = 3 x = x% 3
//= x // = 3 is equivalent to x = x // 3
**= X =. 3 is equivalent to X = X . 3

(4) logical operators

Operators meaning
and with
or or
not non-

(5) Bitwise Operators

Operators meaning
& Bitwise AND
¦ Bitwise or
^ Bitwise XOR
~ Bitwise
>> Right
<< The left

(6) set operator

Operators meaning
in $ \ In $, belong
not in $ \ Notin $, does not belong
== Sets are equal
!= Collections are not equal
< $ \ Subseteq $, Mako Collection
<= $ \ Subset $, subset
> $ \ Supset $, true superset
>= $ \ Supseteq $, superset
& $ \ Cap $, intersection
¦ $ \ Cup $, and set
- Relative difference between the set or complement
^ Symmetric difference

(7) special operators

in operator

Determine whether a variable can not be used with a given container.

x = {1,3,4}
1 in x
9 not in x

在这里插入图片描述

It is operator

Check whether a reference point to the same

x = y = {1,3,4}
x is y

在这里插入图片描述
Note that if written

x = {1,3,4}
y = {1,3,4}
x is y

False, the point is not the same object.

4.if statement

(1) The basic syntax

if(xxx):
    print(a)
elif(xxx):
    print(b)
else:
    print(c)

Note the colon and indentation.

(2) if a single line

The

if(xxx):
    A
else:
    B

Changes to

A if (xxx) else B

Essentially the statement in advance if, then write a line.

(3) with pass multiple lines elif

if(a < 3):
    print(a)
elif(a > 8 and a < 11):
    pass
else:
    print(a)

Note that can not be used; instead of pass.

5.for statement

(1) The basic syntax

for xxxx in xxxxx. wherein in later iterations it is represented by an object or an iterator.

for x in (1,2,3):
    print(x)

(2)range()

With the range for general use, range () returns an iterator, there are three parameters, the first two for the start and end values, optional third, showing step. Note that the range [start, end), that is, comprising start value, the end does not contain a value.

for x in range(1,10):
    print(x,end=' ')
print()

顺序输出1到9,

6.while语句

(1)基本语法

x = 1
while(x<10):
    print(x,end=' ')
    x+=1
print()

要注意的一个就是不要使用++......不要用自增运算符,没用的....

(2)continue+break

x = 1
while(x < 10):
    if(x == 3):
        break
    print(x,end=' ')
    x+=1
print()
x = 1
while(x < 10):
    if(x == 3):
        continue
    print(x,end=' ')
    x+=1
print()

Guess you like

Origin www.cnblogs.com/Blueeeeeeee/p/12109809.html