一.常量
在python中没有一个专门的语法代表常量 程序员约定俗称用变量名的全部大写代表常量
二.基本运算符补充
1.算术运算
print(10 / 3)
print(10 // 3
2.赋值运算之增量赋值
例 age=18
age=age+1
age += 1
3.赋值预算之交叉赋值
例 x=10 y=20
temp=x
y=x
temp=y
x,y=y,x
4. 链式赋值
x=10
y=x
z=y
x=y=z=10
print(id(x))
print(id(y))
print(id(z))
5. 解压赋值
例:L[1.2,2.2,3.3,4.4,5.5]
a,b,c,d,e,=L
print(a,b,c,d,e)
三.if 判断
语法1
age_of_bk=30 print('start...') inp_age=input('>>>:') inp_age=int(inp_age) if inp_age == age_of_bk: print('猜对了')
语法2
age=19 gender='female' is_beautifui=True if age >= 18 and age <= 25 and gender == 'female' and is_beautifui: print('开始表白...') else: print('阿姨好')
语法3
score=input('youe csore') score=int(score) if score >= 90: print('优秀') elif score >= 80: print('良好') elif score >= 70: print('普通') else: print('垃圾')
语法4
age=50 gender='famale' is_beautiful=True is_successful=True if age >=18 and age <= 25 and gender == 'famale' and is_beautiful: print('表白.....') else: print('我逗你玩呢')
四.while循环: 条件循环
while之"条件循环" name_of_bk='egon' pwd_of_bk='123' tag=True while tag: inp_name=input('your name>>:') inp_pwd=input('your password>>:') if inp_name == name_of_bk and inp_pwd == pwd_of_bk: print('login successful') tag=False else: print('username or password error') print('other code...')
while+break break代表结束本层循环 while True: print(1) break print(2) print(3) name_of_bk='egon' pwd_of_bk='123' while True: inp_name=input('your name>>:') inp_pwd=input('your password>>:') if inp_name == name_of_bk and inp_pwd == pwd_of_bk: print('login successful') break else: print('urername or password error') print('other code...')
while+continue continue代表结束本次循环直接进入下一次 count=1 while count<6: if count==3: count+=1 continue print(count) count+=1 name_of_bk='egon' pwd_of_bk='123' count=0 while True: if count == 3: print('输错的次数过多...') break inp_name=input('your name>>:') inp_pwd=input('your password>>:') if inp_name == name_of_bk and inp_pwd == pwd_of_bk: print('login successful') break else: print('username or password error') count += 1 while+else else('else的子代码块只有在while循环没有被break打断的情况下才会执行') count = 0 while True: if count == 10: break print(count) count += 1
while+else else('else的子代码块只有在while循环没有被break打断的情况下才会执行') count = 0 while True: if count == 10: break print(count) count += 1 count=0 while count <= 10: print(count) count += 1 name_of_bk='egon' pwd_of_bk='123' count = 0 tag = True while tag: if count == 3: print('输错的次数过多') break inp_name=input('your name>>:') inp_pwd=input('your password') if inp_name == name_of_bk and inp_pwd == pwd_of_bk: print('iogin successful') while tag: print(""" 0 退出 1 购物 2 支付 3 查看购物 """) cmd = input('>>>:') if cmd == '0': tag = False continue elif cmd == '1': print('购物...') elif cmd == '2': print('支付.....') elif cmd == '3': print('查看购物车') else: print('输入错误的指令') else: print('username or password error') count += 1