11. 数据运算
算数运算
+,-,,/,%,//
比较运算
==,!=,<>,<,>,>=,<=
赋值运算
=,+=,-=,=,/=,%=,=,//=
逻辑运算
and,or,not
成员运算
is,is not
位运算**
&,|,^,~,<<,>>
a=60
b=13
c=0
c=a&b;
print(c)
c=a|b
print(c)
c=a^b
print(c)
c=~a
print(c)
c=a<<2
print(c)
c=a>>2
print(c)
运算符优先级
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
/ % // 乘,除,取模和取整除
-+ 加法减法
<< 右移,左移运算符
& 位 'AND'
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not or and 逻辑运算符
12.if…else
用户名密码登录验证
name=input("请输入用户名:")
pwd=input("请输入密码:")
if name =="alex" and pwd=="cmd":
print("welcome,alex!")
else:
print("error")
猜年龄游戏
name=21
cai_name =int(input("please guess:"))
if cai_name > name:
print("be younger")
else:
if cai_name==name:
print("yes")
else:
print("be older")
------------------------------
if cai_name > name:
print("be younger")
elif cai_name==name:
print("you got it")
else:
print("be older")
外层变量,可以被内层代码使用
内层变量,不应被外层代码使用
13. 表达式for loop
continue
for i in range(10):
if i < 5:
continue
print("loop:",i)
break
for i in range(10):
if i >5:
break
print("loop:",i)
14. While loop
count=0
while True:
print("你是风儿我是啥?",count)
count+=1
if count==100:
print("去你妈的海枯石烂")
break
实现循环三次猜年龄
name=21
count=0
while count<3:
cai_name =int(input("please guess:"))
if cai_name == name:
print("you are so smart")
break
elif cai_name>name:
print("be younger")
else:
print("be older")
count+=1
else:
print("you idot!!!")
15. 入门知识拾遗
- bytes类型
三元运算
result= a if a>b else b
一切皆对象
作业!
作业一:编写登陆接口
输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定
count=0
username="usr"
password="pwd"
while count<3:
usr=input("username:")
pwd=input("password:")
if usr==username and pwd==password:
print("welcome login!!!")
break
else:
print("you got the wrong number!")
count+=1
else:
print("you ! stop it !")
作业二:多级菜单
三级菜单
可依次选择进入各子菜单
所需新知识点:列表、字典
caidan={"shanghai":
{
"yangpu":
{
"五角场":"1",
"中原":"2",
"定海":"3"
},
"pudong":
{
"川沙":"4",
"唐镇":"5",
"曹路":"6"
}
}
}
for i in caidan:
print(i)
i1=input("pls choose!")
if i1 in caidan:
for x in caidan[i1]:
print(x)
i2 = input("pls choose!")
if i2 in caidan[i1]:
for y in caidan[i1][i2]:
print(y)
i3=input("pls choose!")
if i3 in caidan[i1][i2]:
print(caidan[i1][i2][i3])
else:
print("shit")
作业三:购物车程序
需求:
启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
shop_list=[("iphone",5800),("macbook pro",12000),("imac pro",20000),("iwatch",3000)]
salary=int(input("请输入你要充值的金额:"))
print("您的余额为%s元" %salary)
count=0
while count<3:
a=input("是否要开始购物?(yes or no)")
if a =="yes":
b="yes"
while b=="yes":
for index,i in enumerate(shop_list):
print(index,i)
choose=int(input("请选择需要购买的商品序号:"))
if shop_list[choose] in shop_list:
if salary > shop_list[choose][1]:
salary=salary-shop_list[choose][1]
print("您的余额为%s元" % salary)
b = input("是否继续购物?(yes or no)")
else:
print("余额不足")
break
break
elif a=="no":
print("感谢您的购物!")
break
else:
print("您的输入有误!")
count+=1
else:
print("已强制退出!")