目录
一、基本运算符
算术运算符
+ - * / % // ** # 返回一个数值
比较运算符
> >= < <= == != # 返回一个布尔值
赋值运算符
=
逻辑运算符
(把多个条件同时叠加)
name = 'shiki'
height = 170
weight = 120
# and 左右两个条件都为True,则为True,否则为False
print(name == 'shiki' and height ==170) # True
print(name == 'shiki1' and height ==170) # False
# or 左右两个条件只要有一个满足则为True,否则为False
print(name == 'shiki' or height == 190) # True
print(name == 'shiki1' or height == 190) # False
# not 否,如果条件为True,则为False,如果条件为False,则为True
print(not name == 'shiki') # False
身份运算符
没一个变量值都有内存地址(身份/id)
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)
位运算符
不常见
0和1 二进制
方法一: 计算器
方法二:手工计算
成员运算符
判断元素是否在容器类元素里面(字符串)
student_lt = ['s1', 's2', 'S3']
print('s1' in student_lt) # True
print('s1' not in student_lt) # False
print('s4' in student_lt) # False
s = 'shiki'
print('h' in s) # True
Python运算符优先级
需要优先就加括号,括号优先级最高
二、流程控制之if判断
流程控制--》控制 变量变化 的一个方向
单分支结构
'''
if 条件:(:表示你接下来的代码需要缩进) # 条件为True运行缩进内代码;不成立不运行缩进内代码
print('1')
代码块
print(1)
'''
双分支结构
'''
if 条件:(:表示你接下来的代码需要缩进) # 条件为True运行缩进内代码;不成立不运行缩进内代码
code1 条件成立就执行code1
else:
code2 条件不成立就执行code2
print(1)
'''
多分支结构
'''
if 条件1:
code1 条件1成立就执行code1
elif 条件2:
code2 条件1不成立,条件2成立,执行code2
elif 条件3:
code3 条件1和2不成立,条件3成立执行code3
elif 条件n:
coden
else:
codex 所有条件都不成立 执行codex
'''
三、流程控制之while循环
循环:重复(按照某种规律)干一件事
'''
while 条件: # 条件成立运行代码,不成立结束while循环
代码 # 代码执行结束后会进入下一次循环(再一次判断条件)
'''
while + break
终止循环
while + continue
跳出本次循环,不执行下面的代码
tag
中间变量 控制while循环