预科班D9
2020.09.17星期四 预科班D9
学习内容:
一、列表与字典的嵌套
大前提:将所有同学的信息存起来,取值需求
1、取第二个学生的性别
stus_info = [
{"name": "uzi", "age": 18, "gender": "male"},
{"name": "lili", "age": 20, "gender": "female"},
{"name": "jack", "age": 25, "gender": "male"},
]
print(stus_info[1]["gender"])
2、取uzi的年龄
stus_info ={
"uzi": {"age": 18, "gender": "male"},
"lili": {"age": 20, "gender": "female"},
"jack": {"age": 25, "gender": "male"},
}
print(stus_info["uzi"]["age"])
3、取uzi多个爱好中的第一个
stus_info = {
"lili": {"age": 18, "gender": "female", "hobbies": ["play", "read"]},
"uzi": {"age": 19, "gender": "male", "hobbies": ["music", "play", "read"]},
}
print(stus_info["uzi"]["hobbies"][0])
二、输入输出
1、输入
db_user = "drug" inp_user = input("请输入你的用户名:") print(inp_user == db_user) # 输入为drug则True,否则False.
2、输出
print("My name is %s My age is %s" %("Drug","28")) #输出为My name is Drug My age is 28 #%s内容可换
TIPS: print("hello")
print("world")中自带换行,应是print("world",end="/n")
三、基本运算符
1、算数运算符
print(10 - 3.1) # 减法 print(10 + 3.1) # 加法 print(10 * 3.1) # 乘法 print(10 / 3) # 除法 print(10 // 3) # 只保留整数部分 print(10 % 3) # 取余数,取模 print(10 ** 3) # 10的3次方
2、赋值运算符
age = 18 age += 3 # age = age + 3 age *= 3 # age = age * 3 age /= 3 # age = age / 3 age **= 3 # age = age **
3、比较运算符
print(10 > 3) # True print(10 >= 3) # True print(10 < 3) # False print(10 <= 3) # False print(10 == 10.0) # True print("drug" == "drug") # True print([1,2,3] == [2,3,1]) # False print([1,2,3] == [1,2,3]) # True print(10 != 3) # True10不等于3
4、逻辑运算符
#not print(10 > 3) # True print(not 10 > 3) # False #and print(10 > 3 and True and 3 == 10) # False print(10 > 3 and False and 3 == 10) # False #or print(10 < 3 or 3 > 100 or 3 == 10) # True print(10 > 3 or 3 > 100 or 3 != 10) # True #短路运算 #and运算中从前到后只要有一个不成立,后面不用看了直接False #or运算中从前到后只要有一个成立,后面不用看了直接True
5、流程控制之if判断
if 条件1:
代码1
代码2
代码3
...
elif 条件2:
代码1
代码2
代码3
...
elif 条件3:
代码1
代码2
代码3
...
......
else:
代码1
代码2
代码3
...
1、单分支
gender = "female" age = 18 is_beautiful=True if gender == "female" and age > 16 and age < 20 and is_beautiful: print("NICE冲冲冲!!!") print('HAVE A NICE DAY!')
2、双分支
gender = "female" age = 21 is_beautiful=True if gender == "female" and age > 16 and age < 20 and is_beautiful: print("NICE冲冲冲!!!") else: print("886") print('HAVE A NICE DAY!')
3、if嵌套
gender = "female" age = 19 is_beautiful=True is_ok=True if gender == "female" and age > 16 and age < 20 and is_beautiful: print("NICE冲冲冲!!!") if is_ok: print("NICE") else: print("886") else: print("886") print('HAVE A NICE DAY!')
4、例子
码农------------------------------计算机
接收用户输入的账号
接收用户输入的密码
判断 输入的账号 等于 正确的账号 并且 输入的密码 等于 正确的密码:
告诉用户登录成功
否则:
告诉用户账号或密码输入错误
inp_user = input("请输入您的账号:") inp_pwd = input("请输入您的密码:") # inp_pwd = "123" if inp_user == "egon" and inp_pwd == "123": print("登录成功") else: print("用户账号或密码输入错误")