Code
= ["audi","bmW","subaru","toyota"]
cars for i in cars:
if i.lower()=='bmw':
i.upper() else:
i.title()
if conditional_test1:
do something
if conditional_test2:
do something
......
if conditional_testn:
do something
if conditional_test:
do something
else:
do anotherthing
else:
可省略
if conditional_test1:
do something
elif conditional_test2:
do something
......
elif condtional_testn:
do something
else:
do otherthings
= ["audi","bmW","subaru","toyota"]
cars for i in cars:
if i.lower()=='bmw':
i.upper() else:
i.title()
与 and
、或or
= [17,34,29,18,46,55,9,60,35,14,56,64]
ages = []
state for i in ages:
if i <18:
"young")
state.append(elif i>=18 and i<60:
"adult")
state.append(else:
"elder")
state.append( state
['young',
'adult',
'adult',
'adult',
'adult',
'adult',
'young',
'elder',
'adult',
'young',
'adult',
'elder']
在或不在 in
、not in
= ["小米","小亮","小美","小明","小帅"]
users
= "小美"
user1 if user1 in users:
print(f"欢迎光临,{user1}!")
= "小花"
user2 if user2 not in users:
print(f"请加入我们,{user2}!")
欢迎光临,小美!
请加入我们,小花!
对于数值0
,空值None
,单引号空字符串''
,双引号空字符串""
,空列表[]
,空元组()
,空字典{}
,python都会返回False。
= []
users
if users:
for i in users:
print(f"欢迎光临,{i}!")
else:
print("无人使用")
无人使用
= ["A","S","D","F","G","H","J","K"]
invited = ["A","F","Z","X","K","C"]
present
for pre in present:
if pre in invited:
print(f"欢迎您的到来,{pre}先生/女士!")
else:
print(f"抱歉,{pre}先生/女士,你没有请柬,无法进入。")
print("\n 欢迎")
欢迎您的到来,A先生/女士!
欢迎您的到来,F先生/女士!
抱歉,Z先生/女士,你没有请柬,无法进入。
抱歉,X先生/女士,你没有请柬,无法进入。
欢迎您的到来,K先生/女士!
抱歉,C先生/女士,你没有请柬,无法进入。
欢迎
=0
iwhile i<=9:
print(i)
=i+1 i
0
1
2
3
4
5
6
7
8
9
= "\n 输入一条消息,我将会复述一遍:\n 输入 quit 结束程序。"
prompt = ""
message while message != 'quit':
= input(prompt)
message if message !='quit':
print(message)
= "\n 输入一条消息,我将会复述一遍:\n 输入 quit 结束程序。"
prompt = True
active while active:
= input(prompt)
message if message !='quit':
print(message)
else:
=False active
= "\n 输入一条消息,我将会复述一遍:\n 输入 quit 结束程序。"
prompt while True:
= input(prompt)
message if message !='quit':
print(message)
else:
break
=0
iwhile i <10:
=i+1
iif i%2 ==0:
continue
print(i)
1
3
5
7
9
# 首先,创建一个待验证用户列表
# 和一个用于存储已验证用户的空列表
= ['alice', 'brian', 'candace']
unconfirmed_users = []
confirmed_users
# 验证每个用户,直到没有未验证用户为止
# 将每个经过验证的用户都移到已验证用户列表中
while unconfirmed_users:
= unconfirmed_users.pop()
current_user print(f"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())
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice
The following users have been confirmed:
Candace
Brian
Alice
= ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
pets print(pets)
while 'cat' in pets:
'cat')
pets.remove(print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
# 调查程序
= {}
responses = True
flag
while flag:
# 提示被调查者输入性名和回答,存储在字典中
= input("\n What is your name?")
name = input("What is your favorite fruit?")
response = response
responses[name]
= input("Whether to move on to the next person (yes/no)")
repeat if repeat == "no":
= False
flag
print("\n ---Results---")
for i,j in responses.items():
print(f"{i}'s favorite fruit is {j}.")