The Python and process control operators

The Python and process control operators

Operators

Arithmetic Operators

+ - * / % // **   # 返回一个数值

Comparison Operators

 > >= < <= == !=  # 返回一个布尔值

3. assignment operator

+=  -+  *=  /=

Logical Operators

and/or/not

Identity operator

and is the difference between ==: is configured to determine whether two variables refer to the same objects (whether in the same memory space), a reference value for determining == variables are equal.

x = 257
y = x
z = 257

print(f'x is y:{x is y}') # x is y:True
print(f'x == y:{x == y}') # x == y:True

print(f'x is z:{x is z}') # x is z:False
print(f'x == z:{x == z}') # x == z:True

Bitwise Operators

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

Member operator

in/not in 

Python operator precedence

python operator precedence in mathematics equivalent of the first count and then count addition and subtraction multiplication and division, in fact, a little stupid, high priority brackets on the line ...

determining if the flow control

if: If the - world in judgment "reality

Single-branch structure

dog_name = 'dachangtui'  # 一个=是赋值

 if dog_name == 'dachangtui':  # 两个=是两端比较是否相等
     print('干它')

 if dog_name == 'fenggou':
     print('干掉它')
# if判断的语法

# 自上而下运行if 条件: (:表示你接下来的代码需要缩进) # 条件为True运行缩进内代码;不成立不运行缩进内代码
	print('条件成立干嘛干嘛')  # 条件成立才会执行该段代码,不成立不执行该段代码    
	code1
	code2
	code3
	代码块
	....
print(1)

Two-branch structure

'''
if 条件:    
	print('条件成立干嘛干嘛')  # 条件成立才会执行该段代码,不成立不执行该段代码
else:    
	print('条件不成立干嘛干嘛)# 条件不成立才会执行该段代码,成立不执行该段代码'''
	
dog_name = '' 
if dog_name =='fenggou':
    print('干掉它') 
else:#     
	print('干它')

Multi-branch structure

# 沿伸多个分支总结 

if 条件:    
	print('条件成立干嘛干嘛')  # 条件成立才会执行该段代码,不成立不执行该段代码
elif 条件:    
	print('if条件不成立走这条')
elif 条件:    
	...
#elif可以有无限个...    
else:    
	print('条件不成立干嘛干嘛)# 条件不成立才会执行该段代码,成立不执行该段代码

The while loop flow control

流程控制:控制变量往一个方向变化
循环:重复(按照某种规律)干一件事
while 当

while syntax

'''
while 条件: # 条件成立运行代码,不成立结束while循环
    代码 # 代码执行结束后会进入下一次循环(再一次判断条件)
'''

while 条件(限制的范围)
    code 1
    code 2
    code 3
    ...

while True:
    print('*1'*100)
    print('*2'*100)

while + break

break off the circulation means terminates the current layer, other code execution.

while + break
count = 0
while 1:
    if count == 100:
        break  # break终止循环
    count += 1
    print(count)

print('bzr')

while + continue

means continue to terminate this cycle, the next cycle directly into

count = 0
while 1:
    if count == 100:
        break  # break终止循环
    count += 1
    if count == 50:# 不打印50
        continue  # continue跳出本次循环,不执行下面的代码
    print(count)

print('bzr')

continue the code can not be added to the final step in the execution of the loop because the code to add the phrase is meaningless, as the following location continue where is meaningless.

ps: Note that the code to perform the last step, not the last line.

while + else (as understood only)

while + else: else will only execute code in else in the while not break.

# while+else  仅作了解(非用不可可以使用,不要和if。。else混了)
count = 0
while count < 100:
    count += 1
    print(count)
else:
    print('没有被break干掉我就能出来')


count = 0
while count < 50:
    if count == 100:
        break
    count += 1
    print(count)
else:  # 没有被break干掉就执行,被break终止了就不执行
     print('没有被break干掉我就能出来') # 可以判断while是否被break终止

Control flow of the for loop

Loop: Repeat thing

# while循环:可以循环一切事物 (不好控制)
lt = [1, 2, 3, 4]

ind = 0

while True:
    print(lt[ind])

    ind += 1
    
# for循环:提供了一种手段,不依赖索引取值(好控制)    
lt = [1, 2, 3, 4]

for i in lt:
    print(i)    

# for 变量名(会拿到容器类元素的每一个值,没有了就结束循环) in 容器类元素:
#     print(变量名)
dic = {'a': 1, 'b': 2, 'c': 3}

count = 0
for i in dic:  # 对于字典,for循环只能拿到key
    print(i, dic[i])
    count += 1

# range方法
 print(list(range(10)))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
# 默认从0开始,10结束,默认步长为1,顾头不顾尾
 print(list(range(1,10)))  # [1, 2, 3, 4, 5, 6, 7, 8, 9]  
# 1表示开始,10表示结束,默认步长为1,顾头不顾尾
 print(list(range(1,10,2)))  # [1, 2, 3, 4, 5, 6, 7, 8, 9]  
# 1表示开始,10表示结束,2表示步长,顾头不顾尾

for + break

# for + break
for i in range(50,101,3):  # 顾头不顾尾,2表示步长
    if i == 53:
        break  # 中断循环
    print(i)

for + continue

# for + continue
for i in range(50,101,3):  # 顾头不顾尾,2表示步长
    if i == 53:
        continue  # 跳出本次循环,不执行下面的代码
    print(i)

for + else (as understood only)

# for + else(仅作了解):for循环不被break终止就执行else下的代码,否则不执行
for i in range(50,101,3):
    if i == 1000:
        break
    print(i)
else:u
    print('如果没有被break终止我就打印')

Guess you like

Origin www.cnblogs.com/jzm1201/p/12590511.html