目录
基本运算符
算术运算符
+ - * / % // ** # 返回一个数值
比较运算符
> >= < <= == != # 返回一个布尔值
赋值运算符
x=10
逻辑运算符(把多个条件同时叠加)
name='nick'
height=180
weight=140
and 左右两个条件都为True,则为True,否则为False
print(name == 'nick' and height == 180) # True
print(name == 'nick1' and height == 180) # False
or 左右两个条件只要有一个满足则为True,否则为False
print(name == 'nick' or height == 190) # True
print(name == 'nick1' or height == 190) # False
not 否,如果条件为True,则为False;如果条件为False,则为True
print(not name == 'nick') # False
身份运算符
每一个变量值都有内存地址(身份)
x= 257
y = x
z= 257
print(id(x) == (id(y)))
print(x is y) # is比较的是内存地址
print(x is not y) # is not判断是否不等于
print(not x is y)
print(id(x) == id(z))
print(x is z)
位运算符
位运算符是把数字看作二进制来进行计算的
成员运算符
判断元素是否在容器类元素里面(字符串)
class_student_lt = ['s1','s2','s3']
print('s1' in class_student_lt) # True
print('s1' not in class_student_lt) # False
print('s4' in class_student_lt) # False
s = 'nick'
print('n' in 'nick')
# Python运算符优先级
# + - * / : 先算* / 再算 + -,就叫做优先级
# 需要优先,就加括号,括号优先级最高
流程控制之if判断
单分支结构
if 条件:
code1
code2
code3
...
双分支结构
if 条件:
code1
code2
code3
...
else:
code1
code2
code3
...
多分支结构
if 条件1:
code1
code2
code3
...
elif 条件2:
code1
code2
code3
...
elif 条件3
code1
code2
code3
...
else:
code1
code2
code3
流程控制之while循环
while
while 条件:
代码块
while + break
while 条件:
代码块
break # 结束本层循环,跳出循环
while + continue
while 条件:
代码块
if 条件:
代码块
continue # 不执行下面代码,然后继续循环,即跳出本次循环
代码块
while + else
while 条件:
代码块
else:
print('如果没有被break,就会被打印出来')