一、赋值运算符
1.增量赋值
age = 18
age += 1
print(age) age输出值为19。
2.链式赋值
x = 10
y = x
z = y
可以直接写成x=y=z=10
3.交叉赋值
m = 111
n = 222
temp = m
m = n
n = temp
print(m,n)
4.解压赋值
salaries = [1.1,1.2,1.3,1.4,1.5]
mon0 = salaries[0]
mon1 = salaries[1]
mon2 = salaries[2]
mon3 = salaries[3]
mon4 = salaries[4]
print(mon0)
print(mon1)
print(mon2)
print(mon3)
print(mon4)
上面太过繁琐,中间部分可以写为mon0,mon1,mon2,mon3,mon4 = salaries
也可以只取开头几个值,而忽略后面的值:
mon0,mon1,*_ = salaries
print(mon0)
print(mon1)
print(_)
也可以只取后面几个值,而忽略前面的值:
*_,mon3,mon4 = salaies
print(mon3)
print(mon4)
print(_)
也可以只取前面后面几个值,而忽略中间的值:
salaries = [1.1,1.2,1.3,1.4,1.5]
mon0,mon1,*_,mon4 = salaries
print(mon0)
print(mon1)
print(mon4)
print(_)
二、布尔类型
创造布尔值的方法有两种:
1.直接定义:x = True
2.通过比较运算的来:res1 = 10 > 3
两种创造布尔值的方式为显性的布尔值。
隐式的布尔值:首先所有类型的数据都可以当作隐式的布尔值,其次只需要记住0、None、空三类隐藏的布尔值为False,其余的都为True。
三、逻辑运算符
条件:只要能得到True 和False这两种值的都可以当作条件。
1.not: 把布尔值结果取反。
2.and:用来连接左右两个条件,左右两个条件同时为True,最终结果才为True。
3.or:用来连接左右两个条件,左右两个条件但凡有一个结果为True,最终结果就为True。
偷懒行为 ==》 短路运算
例1:10 < 3 and 1==1 #第一个为False,则后面就不需要看了,直接False。
例2:True or 1==1 #第一个为True,则后面就不需要看了,直接True。
优先级:not > and >or
例1:res1 = 3 >4 and 4>3 or not (1==3 and 'x' == 'x') or 3>3
可以加上括号:res1 = (3 >4 and 4>3)or (not (1==3 and 'x' =='x'))or 3>3,这样更加清晰。
四、if流程判断
1.什么是if流程判断?
判断条件1 并且 条件2
做什么事情......
否则:
做什么事情....
2.为什么要有if判断?
为了控制计算机能够像人一样去完成判断的过程。
3.如何使用if判断
(1)if和else组合
if True and 10>8: #碰到冒号,子代码缩进四格
print("条件成立")
else:
print("条件不成立")
(2)只有if
inp_name = input ("你的名字")
if inp_name = "Bob":
print ("输入正确")
print("其他代码")
(3)if + elif
inp_name = input('你的名字:')
if inp_name =="Bob":
print("您的身份是超级vip")
elif inp_name =="Ben":
print("您的身份是钻石vip")
(4) if + elif +else
inp_score =input("请输入你的成绩:")
inp_score =int (inp_score)
if inp_score >=90:
print("优秀")
elif inp_score >=80:
print("良好")
elif inp_score >=70:
print("普通")
else:
print("很差")
(5)if判断嵌套if
以自己编写的一个简单配对相亲代码为例子:
sex =input("请输入性别:")
if sex =="female":
age =input("请输入年龄:")
age =int(age)
if age >=20 and age<=30:
inp_beautiful =input("是否漂亮:")
if inp_beautiful =="True":
print("心仪对象")
else:
print("不符合标准")
else:
print("不符合标准")
else:
print("不符合标准")
作业:
1、短路运算面试题,请说出下列运算的结果
>>> 1 or 3 and 4
答:True
>>> 1 and or 0 and 3
答:无法运行
>>> 0 and 3 and 1
答:False
>>> 1 and 2 or 1
答:True
>>> -31 and 2 or 1 or 4
答:True
>>> 33 or False and 1 or 5
答:True
2、用户输入账号密码,程序分别单独判断账号与密码是否正确,正确输出True,错误输出False即可
答:
cor_account = "784192462"
cor_pwd = "56814698"
inp_account = input("请输入您的账号:")
if cor_account == inp_account:
print("True")
else:
print("False")
inp_pwd = input("请输入您的密码:")
if cor_pwd == inp_pwd:
print("True")
else:
print("False")
3、让计算机提前记下egon的年龄为18岁,写一个才年龄的程序,要求用户输入所猜的年龄
,然后程序拿到用户输入的年龄与egon的年龄比较,输出比较结果即可
答:
egon_age = 18
inp_age = input("请输入您所猜的年龄:")
if inp_age == egon_age:
print("Ture")
else:
print("False")
4、程序从数据库中取出来10000条数据,打算显示到页面中,
但一个页面最多显示30条数据,请选取合适的算数运算符,计算
显示满30条数据的页面总共有多少个?答:10000//30
最后一页显示几条数据?答:10000%30
5、egon今年为18岁,请用增量赋值计算3年后egon老师的年龄
答:
egon_age = 18
egon_age += 3
print(egon_age)
6、将值10一次性赋值给变量名x、y、z
答:x = y =z =10
7、请将下面的值关联到它应该对应的变量名上,你懂的
dsb = "egon"
superman = "alex"
答:
dsb = "egon"
superman = "alex"
zjf =dsb
dsb = superman
superman = zjf
print(dsb)
print(superman)
8、我们只需要将列表中的傻逼解压出来,一次性赋值给对应的变量名即可
names=['alex_sb','wusir_sb','oldboy_sb','egon_nb','lxx_nb','tank_nb']
答:
names=['alex_sb','wusir_sb','oldboy_sb','egon_nb','lxx_nb','tank_nb']
sb0,sb1,sb2,*_ = names
print(sb0)
print(sb1)
print(sb2)
9、编写验证用户身份的程序(用计算机取代业务员)
答:
cor_account = "784192462"
cor_pwd = "56814698"
inp_account = input("请输入你的账号:")
inp_pwd = input("请输入你的密码:")
if cor_account == inp_account and cor_pwd == inp_pwd:
print("登陆成功")
else:
print("账号或密码存在错误")
10、编写程序实现如下功能
# 要求用户输入今天是周几,然后作出判断
# 如果:今天是Monday,那么:上班
# 如果:今天是Tuesday,那么:上班
# 如果:今天是Wednesday,那么:上班
# 如果:今天是Thursday,那么:上班
# 如果:今天是Friday,那么:上班
# 如果:今天是Saturday,那么:出去浪
# 如果:今天是Sunday,那么:出去浪
答:
week_plan = ["上班","出去浪"]
inp_today = input("请输入今天星期几?")
if inp_today == "Monday"or inp_today == "Tuesday" or inp_today == "Wednessday" or inp_today == "Thursday" or inp_today == "Friday":
print(week_plan[0])
elif inp_today == "Saturday" or inp_today == "Sunday":
print(week_plan[1])
选做题:
编写用户登录接口(学的多的同学,尝试做下述作业,这是截止到下周二学完文件处理之后的作业)
#1、输入账号密码完成验证,验证通过后输出"登录成功"
答:cor_account = "784192462"
cor_pwd = "56814698"
inp_account = input("请输入你的账号:")
inp_pwd =input("请输入你的密码:")
while cor_account != inp_account or cor_pwd != inp_pwd:
print("账号或密码输入错误")
inp_account = input("请重新输入你的账号:")
inp_pwd = input("请重新输入你的密码:")
print("登陆成功")