Python if判断+字典+用户输入输出+while循环

1、if判断语句

cars=['audi','bmw','subaru','toyota']
for car in cars:
    if car=='bmw': #判断还是要考虑大小写的,也是需要用==小心
        print(car.upper()) 
    else:
        print(car.title())

所有的if-else条件都是要加: 的,别忘加

age=21
if age<4:
    price=0
elif age<18:
    price=5
elif age<65:
    price=10
else:
    price=5
print("Your adiminssion cost is $"+str(price)+".")

 

requested_toppings=[]
if requested_toppings:
    print("OK!")
else: print("NO!")

 

2、字典

字典:键+值,用: 表示关联关系,与键相关联的值可以是数字、字符串、列表乃至字典。键-值对的排列顺序与添加顺序不同。Python不关心键-值对的添加顺序,而只关心键和值之间的关联关系。也可以正常修改键对应的值。

alien={"color":"green","point":5}

print(alien['color'])
print(alien['point'])

alien["name"]="riley"
print(alien)
alien["color"]="pink"
print(alien["color"])

 

2.1、字典的遍历

遍历的时候,可以使用items()函数,就可以取到键和值,但是输出顺序可能和最开始输入顺序都不相同。

在不需要使用字典中的值时,方法keys()很有用。keys()直接返回一个列表,其中包含字典中的所有键,当然在用keys的时候也可以省去,系统默认访问所有键值,但是最好加上方便理解。

如果只要字典包含的值,可使用方法values(),它返回一个值列表,而不包含任何键。要是想对值中去重(值里面有些是相同的),就可以用set()

favorite_l={"riley":"matlab","chen":"python","sherry":"c","louise":"matlab"}
for key,value in favorite_l.items():
    print(key.title()+": "+str(value)+"\n")

for name in favorite_l.keys():
    print(name.title())
for name in favorite_l: #这个就是没有加keys,但是就是返回了键
    print(name.title())

if "alben" not in favorite_l.keys():#这个就是列表的用法
    print("NO STUDENT!!")

for content in favorite_l.values():
    print(content.title())
    
print("\n")
for content in set(favorite_l.values()):#用set()去重了一下
    print(content.title())

2.2、字典列表嵌套

2.2.1 字典嵌套在列表里面

alien0={"color":"green","point":5,"name":"riely"}
alien1={"color":"red","point":10,"name":"chen"}
alien2={"color":"black","point":0,"name":"louise"}
aliens=[alien0,alien1,alien2]
print(aliens)

for alien_number in range(10):
    alien={"color":"green","point":5,"name":"riely"}
    aliens.append(alien)
for alien in aliens[:5]:#只显示5个
    print(alien)

这个嵌套有一个很好的地方在于,list是讲究顺序的,所以对于alien0,alien1...之类的来说排序是有顺序的,而每个alien的属性是没有顺序的

2.2.2 列表嵌套字典在里面

favorite_l={
    "riley":["matlab","python"],
    "chen":["python"],
    "sherry":"c",
    "louise":["c","matlab"]}

for name,languages in favorite_l.items():
    print("name: "+name.title()+"\tlanguage: ")
    for language in languages: 
        print(language)

 

字典里面键值嵌套列表,就可以使得键对应的值有多个,而且循环列表也很方便,不像字典+字典还需要再整成列表才好输出,而且即使只有一个不是一个列表也可以这么输出

users={
    'aeinstein':{
        'first':'albert',
        'last':'sinstein',
        'location':'princeton',
    },
    
    'mcurie':{
        'first':'marie',
        'last':'curie',
        'location':'paris',
    },
}

for username,user_info in users.items():
    print("\nUser name: "+username)
    for user_detail in user_info.keys(): #因为此处字典中字典每个都具有相同的属性,但其实不一样的情况也是这样就能实现
        print(user_detail+": "+user_info[user_detail])

字典与字典的嵌套,跟字典中嵌套列表很像,目前觉得差不太多,遍历会有一个找到字典键的一个步骤

3、用户输入输出

message="Tell me something, "
message+="and I will repeat it back to you: "
remessage=input(message)
print(remessage)

input()得到的值都是字符串,要是想得到int类型,直接加int()即可

message=input("Enter a number: ")
int(message)>18 #只有数值直接才可以比较大小

 

4、while循环

message=""
while message!="quit":
    message=input("Tell me something, and I'll repeat it back:\nEnter 'quit' to end this program")
    if message!="quit":
        print(message)

break, 退出循环

continue, 进行下一次循环

程序陷入无限循环,可按Ctrl + C,也可关闭显示程序输出的终端窗口。有些编辑器(如Sublime Text)内嵌了输出窗口,这可能导致难以结束无限循环,因此不得不关闭编辑器来结束无限循环

pets=['dog','cat','goldfish','cat','rabbit','cat']
print(pets)

while 'cat' in pets:
    pets.remove('cat')
    
print(pets)

while+一点列表 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值