2021-10-20

Python Day1

# print ("hello world!")#Ctrl+B配置后输出

#常量直接大写

# for value in range(1,5):#shift+ctrl+/快速注释

# print(value)

# for value in range(5):

# print(value)

# number=list(range(5))

# print(number)

#输出一个乘方列表

# squares=[]

# for value in range(1,11):

# squares.append(value**2)

# print(squares)

# number=list(range(1,5))

# print(number)

# digits=[1,2,3,4,5,6,7,8,9,0]

# print(sum(digits))

# min(digits)

# max(digits)

# squares=[value**2 for value in range(1,11)]

# print(squares)

# players=['A','B','C','D','E']切片

# print(players[1:4])

# print(players[:4])

# print(players[1:])

# names=['lin','jiang','hao','guo','liu']

# for name in names[1:4]:

# print(name.title())

#复制列表

# my_foods=['A','B','C','D','E']

# myfriends_foods=my_foods[:]

# print(my_foods)

# myfriends_foods.append('F')

# print(myfriends_foods)

#P57课后作业

#遍历输出前三个/中间元素

# subjects=['chinese','english','math','art','physics']

# print("The first three items in the list are:")

# for subject in subjects[1:4]:

# print(subject.title())#upper/lower

#if语句

#在python中用冒号:来代替形如c语言中的{},且覆盖区域需要在下一行进行缩进!

# names=['zhangsan','lisi','wangwu','zhaoliu']

# for name in names:

# if name=='lisi':

# print(name.upper())

# else:

# print(name.title())

# libohao='huairen'

# if libohao != 'haoren':

# print("libohao is headbroken! ")

# alien_color='green'

# if alien_color =='yellow':

# print("The player get 5 points!")

# else :

# # print("The player get 1 point!")

# alien_colors=['yellow','green','blue','red']

# alien_color='yellow'

# if alien_color in alien_colors:

# print("There is a yellow alien")

# if 'green' in alien_colors:#elif 跳过

# print("There is a green alien")

# if 'blue' in alien_colors:#elif

# print("There is a blue alien")

# # if:/else:

# print("There is a red alien")

#点菜点菜

# available_toppings=['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']

# requested_toppings=['mushrooms','french fries','extra cheese']

# for requested_topping in requested_toppings:

# if requested_topping in available_toppings:

# print(f"Adding {requested_topping}--")

# else:

# print(f"Sorry,we do not have {requested_topping}")

# print("Finished making your pizza!")

#字典/键值对{'color':'blue','point':5}

# alien_0={}

# alien_0['color']='blue'

# alien_0['point']=5

# print(alien_0)

# alien_0={

# 'linshujin':24,

# 'linjinzhi':22,

# 'liuchangjiang':23,

# 'liuzhuang':23,

# }

# age=alien_0.get('haoshuai','no name has been find!')#用get方法来避免空值错误,第一个参数是指定值(必需),第二个参数用来调整默认参数(可选)

# print(f"linshujin is {alien_0['linshujin']}")

# print(age)

#遍历字典

# for key,value in alien_0.items():

# print(f"{key}'s age is {value}")

# for name in alien_0.keys():#仅遍历键/遍历值

# for age in alien_0.values():

# print(f"{name}")

# #顺序遍历/sorted临时排序

# subjects={

# 'math':'数学',

# 'english':'英语',

# 'chinese':'语文',

# 'enl':'英语',

# }

# # for subject in sorted(subjects.keys()):

# # print(subject)

# for subject in set(subjects.values()):

# print(subject)#set集合存储的元素不会重复但是是随机顺序

#字典嵌套

# aliens=[]

# #创建30个绿色外星人

# for alien_number in range(30):

# new_alien={'color':'green','point':5,'speed':'slow'}

# aliens.append(new_alien)

# #外星人变化

# for alien in aliens[:3]:

# if alien['color']=='green':

# alien['color']='yellow'

# alien['point']=10

# alien['speed']='fast'

# #输出前五个外星人

# for alien in aliens[:5]:

# print(alien)

# print("-----------------------------------------------")

# print(f"The total number is {len(aliens)}")

#在字典中存储列表

# pizza={

# 'crust':'thick',

# 'toppings':['mushrooms','extra cheese'],

# }

# print(f"You order a {pizza['crust']} pizza with the following toppings:")

# for topping in pizza['toppings']:

# print("\t"+topping)

# favorite_subject={

# 'linshujin':['english','chinese','math'],

# 'linjinzhi':['chinese','math','PE'],

# 'liuchangjiang':['math','art'],

# 'liuzhuang':['physics'],

# }

# for name,subjects in favorite_subject.items():

# print(f"{name}'s favorite subjects are:")

# for subject in subjects:

# print(subject)

#在字典中嵌套字典

users={

'linshujin':{

'age':24,

'hometown':'fujian',

'favorite_subject':'math',

},

'liuchangjiang':{

'age':23,

'hometown':'hebei',

'favorite_subject':'english',

}

}

# for user,user_info in users.items():

# print(f"Username: {user.title()}")

# age=user_info['age']

# hometown=user_info['hometown']

# favorite_subject=user_info['favorite_subject']

# print(f"\tAge:{str(age)}")

# print(f"\tHometown:{hometown.title()}")

# print(f"\tFavorite subject:{favorite_subject.title()}")

#用户输入/需要加载sublime的扩展SublimeREPL

#配置快捷键Preferences/Key Bindings/.../ctrl+m直接运行交互程序

#ctrl+b default

# name=input("Please input your name: ")输入数字时用int()转换

# print(f"\nWelcome {name}")

#求模运算

# number=input("Enter a number,and i'll tell you if it's even or odd: ")

# number=int(number)

# if number%2==0:

# print(f"\nThe number {number} is a even.")

# else:

# print(f"\nThe number {number} is a odd.")

#while循环

# number=1

# while number<=5:

# print (number)

# number+=1

#这里的prompt其实就是Input()中的提示词,其实可以省略

# prompt ="\nTell me something ,and i will repeat it to you:"

# prompt +="\nEnter 'quit' to end the program."

# # message=""

# # while message!='quit':

# # message=input(prompt)

# # if message != 'quit':

# # print(message)#记得缩进!不缩进python没法判断生效区域!!!

# active=True#标记

# while active:

# message=input(prompt)

# if message=='quit':

# active=False

# else:

# print(message)

# prompt="\nPlease enter the name of city you have visited:"

# prompt+="\n(Enter 'quit' when you are finished.)"

# message=""

# active=True

# while active:

# message=input(prompt)

# if message=='quit':

# break

# else:

# print(message)

#continue

# current_number=0

# while current_number<10:

# current_number+=1

# if current_number%2==0:

# continue

# else:

# print(current_number)

#使用while循环来处理列表和字典

#在列表之间移动元素

# unconfirmed_users=['zhangsan','lisi','wangwu','zhaoliu']

# confirmed_users=[]

#认证用户中----------

#列表pop()删除并返回列表中的最后一个元素

# while unconfirmed_users:

# verifying_user=unconfirmed_users.pop()

# print(f"Verifying user:{verifying_user}")

# confirmed_users.append(verifying_user)

# #输出全部已认证用户

# print("All confirmed users are follows:")

# for confirmed_user in confirmed_users:

# print(confirmed_user.title())

#使用用户输入来填充字典内容

#新建一个字典用来存储用户输入的信息

responses={}

active=True

while active:

name=input("Please input your name:")

age=input("Please input your age:")

responses[name]=age

repeat=input("Would you like to let another person respond?(Yes/No)")

if repeat=='No':

active=False

print("-------------------research done--------------------")

for name,age in responses.items():

print(f"{name} is {age} years old.")

你可能感兴趣的:(2021-10-20)