if 语句
- 简单的 if语句的例子
age = 22
if age >= 20 :
print('年龄大于20')
if age != 21 :
print('年龄不等于21')
if age <= 20 :
print('年龄小于20')
else:
print('年龄不小了')
# if elif else 结构
for item in range(1,21,2):
if item >= 17:
print('年龄大于17岁')
elif item<=15 and item >=10:
print(str(item)+ '年龄')
else :
print(str(item)+'这也没谁了')
- 使用if 语句处理列表
# if 语句处理列表
# 手抓饼配料
Ingredients = ['鸡蛋','热狗','鸡柳','五花肉']
# 顾客要求
req = ['鸡柳','火腿']
for item in req:
if item in Ingredients:
print('这个'+item+'---免费')
else:
print('非常抱歉我们没有'+item)
字典
字典与js中的对象类似
# 字典
person0 = {
'name' : '小明',
'age' : 22,
'gender' : 'man',
}
print(person0['name'])
- 字典中的简单操作
# 字典
person0 = {
'name' : '小明',
'age' : 22,
'gender' : 'man',
}
print(person0['name'])
# 字典添加键值对
person0['hobby'] = '篮球'
print(person0)
# 修改字典中的值
person0['hobby'] = 'rap'
print(person0)
# 删除字典中的键值对
del person0['hobby']
print(person0)
- 遍历字典
# 字典
person0 = {
'name' : '小明',
'age' : 22,
'gender' : 'man',
}
# 遍历所有的键值对
# items() 方法返回的是包含键值对的元组列表
print(person0.items())
for key, value in person0.items():
print('key:'+key)
print('value:'+str(value))
# 遍历所有的键
# keys()方法结果为 字典的键的列表
print(person0.keys())
for key in person0.keys():
print('key===>'+key)
# 遍历字典中的所有的值
# values()方法返回为 字典中的所有的值的列表
print(person0.values())
for value in person0.values():
print('value===>' + str(value))