版权声明:欢迎转载请注明转自方辰昱的博客https://blog.csdn.net/viafcccy https://blog.csdn.net/viafcccy/article/details/89523676
python中的if语句 与其他语言基本一致这里使用几个例子 就能看懂
age_1 = 1
age_2 = 2
age_3 = 3
if age_1 < age_2 and (age_2 < age_3 or age_2 > age_3) : print('TRUE')
else : print('FLASE')
if age_1 > age_2 and age_2 < age_3 or age_2 > age_3 : print('TRUE')
else : print('FLASE')
#and优先级大于or 同时注意:不要忘记了
num = 5
if num == 3: # 判断num的值
print ('boss')
elif num == 2:
print ('user')
elif num == 1:
print ('worker')
elif num < 0: # 值小于零时输出
print ('error')
else:
print ('roadman') # 条件均不成立时输出
总结一下python与c在if的区别
1.and or 取代 && ||
2.and 优先级高于 or
3.字符串是一种基础数据类型可以直接 ==
检查特定值是否在列表中
array = [1,2,3,4,5]
print(1 in array)
同理not in
array = [1,2,3,4,5]
num = 6
if num not in array:
print('6 不在数字列表中')
同时我们知道了在python中布尔值使用 Ture 和 False
关于空列表
array = []
if array:
print('I founded.')
else :
print('are you kidding?')
while
在py中while循环与其它语言基本一致这里不再赘述
count = 1
while count <= 5:
print(count)
count += 1
使用while处理列表和字典
unconfirmed_user = ['jack','rose','tom']
user = []
while unconfirmed_user:#这里涉及到空列表的返回值是flase
current_user = unconfirmed_user.pop()
print('verifying:'+str(current_user.title()))
user.append(current_user)
print('condirmed user:')
for users in user:
print(users)
删除特定的值
array = {1,2,3,4,5,1,2,1,2,3,1}
print (array)
while 1 in array:
array.remove(1)
print(array)
demo:与用户交互 投票调查统计结果
respone = {}
polling_active = True
while polling_active:
name = input("\nwhat is your name?")
respone_result = input("do you like me?")
respone[name] = respone_result
repeat = input('others want to join this?(y/n)')
if repeat == 'n':
polling_active = False
print('-----------the poll result--------')
for names,respones in respone.items():
print(names,respones)