1. 运算符
算数运算符
+ - * /
int / float :数字类型
# print(10 + 3.1)
# print(10 / 3)
# print(10 // 3)
# print(10 % 3)
# print(10 ** 2)
赋值运算符
增量赋值
age=18
age=age+1
print=(age) #19
age+=1
print(gae)#19
交叉赋值
x=18 y=22
x,y=22,18
print(x,y) # 22,18
链式赋值
x=y=z=10
print(x,y,z) #10,10,10
解压赋值
列表的解压赋值
# salaries=[1.1,2.2,3.3,4.4,5.5]
# a,b,_,_,_=salaries
# a,b,*_=salaries
# *_,a,b=salaries
# a,*_,b=salaries
# print(a,b,) 2. 字典的解压赋值
dic={'aaa':1,'bbb':2,'ccc':3}
# x,y,z=dic
# print(x,y,z)
逻辑运算符
and or not
# and: 左右两个条件必须同时成立,最终结果才为True
# print(10 < 3 and 3 == 3) # or: 左右两个条件只要有一个成立,最终结果就为True
# print(10 < 3 or 3 == 3)
# print(10 < 3 or 3 < 3) # not:将紧跟其后的条件结果取反
# print(not 10 > 3)
# print(not 10 < 3 or 3 < 3) # res=(10 > 3 and 3 == 1) or ((4 < 3 and True) or (not 1 > 2 or 3 > 2)) # print(res)
比较运算符
# ==
# print(10 != 3) # 了解
# msg1='abcdef'
# msg2='abcz' # print(msg2 > msg1) l1=[1,'aaa',333]
l2=[2,'b']
print(l2 > l1)
2. 流程控制
if
语法一:
# if 条件:
# 代码1
# 代码2
# 代码3 # gender='female'
# age=18
# is_beautiful=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('开始表白。。。。')
#
# print('其他代码') #语法二:
# if 条件:
# 代码1
# 代码2
# 代码3
# else:
# 代码1
# 代码2
# 代码3 # gender='female'
# age=26
# is_beautiful=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('开始表白。。。。')
# else:
# print('阿姨好')
#
# print('其他代码') #语法三:
# if 条件1:
# if 条件2:
# 代码1
# 代码2
# 代码3 # gender='female'
# age=18
# is_beautiful=True
# is_successfull=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('开始表白。。。。')
# if is_successfull:
# print('在一起,,,')
# else:
# print('逗你玩呢。。。')
# else:
# print('阿姨好')
#
# print('其他代码')
# #语法四:
# if 条件1:
# 代码1
# 代码2
# 代码3
# elif 条件2:
# 代码1
# 代码2
# 代码3
# elif 条件3:
# 代码1
# 代码2
# 代码3
# .......
# else:
# 代码1
# 代码2
# 代码3 '''
如果:成绩>=90,那么:优秀 如果成绩>=80且<90,那么:良好 如果成绩>=70且<80,那么:普通 其他情况:很差
''' score=input('your score: ')
score=int(score) if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')
循环(while/for)
引入:
# name='egon'
# pwd='123'
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
#循环就是重复做某件事
# 语法:
# while 条件:
# 代码1
# 代码2
# 代码3 # while True:
# 1+1 # n=1
# while n < 10:
# print(n)
# n+=1 # name='egon'
# pwd='123'
#
# tag=True
# while tag:
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# tag=False
# else:
# print('name or password error') # while+break:终止本层循环
name='egon'
pwd='123' while True:
inp_name=input('your name: ')
inp_pwd=input('your password: ')
if inp_name == name and inp_pwd == pwd:
print('login successfull')
break
else:
print('name or password error')