自学Python 边学边记DAY5

RT,小白学习py,个人理解,如有不对希望指正,不胜感激

参考书籍:《Python编程从入门到实践》 Eric Matthes

1.在字典中储存列表:

#在字典中存储列表
pizza={
    'crust':'thick',
    'toppings':['mushrooms','extra_cheese'],
    }
#概述所点的披萨
print("You ordered a "+pizza['crust']+ "-crust pizza "+"with the following toppings: ")
for topping in pizza['toppings']:
    print("\t"+topping)
#pizza是一个字典,里面的toppings键值对 是一个列表,也就是列表储存于字典了

2.字典嵌套列表

#字典嵌套列表,m每一个对 都是一个列表,类似于二维数组?
favorite_lang={
    'jen':['python','ruby'],
    'sarah':['c'],
    'edward':['ruby','go'],
    'phil':['python','haskell'],
}
#二层循环
for name,languages in favorite_lang.items():
    print("\n"+name.title()+"'s favorite langs are: ")
    for language in languages:
        print("\t"+language.title())

3.字典中存字典:

#在字典中存储字典
users={
    'aeinstein':{
        'first':'albert',
        'last':'einstein',
        'location':'princeton',
    },
    'mcurie':{
        'first':'marie',
        'last':'mcurie',
        'location':'paris',
    },
    'torres':{
        'first':'fernando',
        'last':'torres',
        'location':'spain',
    },
}

for username,user_info in users.items():
    print("\nUsername: "+username)
    fullname=user_info['first']+" "+user_info['last']
    location=user_info['location']

    print("\tFull name is "+fullname.title())
    print("\tLocation is "+location.title())

4.关于input函数

message=input("输入点东西,我是复读机")
print(message)

height=input("How tall are you?\n")
height=int(height)
if height >= 150:
    print("You're tall enough to ride a biycle")
else:
    print("You'll be able to ride when you're tall enough")

5.Python没有a++?????

6.while循环:

prompt = "输入然后我复读:\n"
prompt += "输入'退出'结束:\n"
message=""
while message != '退出':
    message=input(prompt)
    if message!='退出':
        print(message)

7.Bool 类似于 的使用

prompt = "输入然后我复读:\n"
prompt += "输入'退出'结束:\n"
active=True
while active:
    message=input(prompt)
    
    if message=='退出':
        active=False
    else:
        print(message)

8.break 和 continue 的使用类似于CPP

9.发现可以用列表非空作为一个条件循环


#使用while来处理列表和字典
#创建一个待验证用户列表,和一个用于存储已验证用户的空列表
unconfirmed_users=['alice','brain','candace']
confirmed_users=[]

#验证每个用户,直到没有未验证用户为止,将每个经过验证的列表都移到已验证的拥护列表中
while unconfirmed_users:
    current_user=unconfirmed_users.pop()

    print("Verifying user: "+current_user.title())
    confirmed_users.append(current_user)

#显示所有已经验证的拥护
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())

10.#删除包含特定值的所有列表元素,注意循环的条件!!!!!!!!!!!!!!!!

#删除包含特定值的所有列表元素
#remove可以用来删除列表中的特定值 
#但是如果存在多个这样包含这个值的元素,怎么删除?
pets=['dog','cat','dog','fish','cat','rabbit','cat']
print(pets)

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

print(pets)

11.利用输入 动态扩充字典?

#用用户输入来填充字典
responses={}

#设置一个标志
polling_active=True

while polling_active:
    #提示输入被调查者的名字喝回答
    name=input("\nWhat's your name?\n")
    response=input("Which mountain do you like ?\n")

    #将答卷存储到字典中
    responses[name]=response

    #看看是否还有人要参与调查
    repeat=input("Would you like to let another one respond?(yes/no)")
    if repeat=='no':
        polling_active=False

#调查结束,显示结果
print("\n---Poll Results---")
for name,response in responses.items():
    print(name+" would like to climb "+response+".")

print(responses)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tototototorres

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值