[python笔记]4.if语句

一.示例

cars=['audi','bmw','subaru','toyota']
for car in cars:
    if car=='bmw':
        print(car.upper())
    else:
        print(car.title())

输出

Audi
BMW
Subaru
Toyota

二.条件测试

1)检查是否相等

>>> car='bmw'
>>> car=='bmw'  
True
>>> car='audi'
>>> car=='bmw' 
False

2)区分大小写

python判断相等时区分大小写,大小写不同值不相同

>>> car='Audi'
>>> car=='audi'  
False
>>> car='Audi'         
>>> car.lower()=='audi'
True
>>> car
'Audi'

如果判断是不需要大小写,可以转换为小写之后再判断,lower方法不会改变原有变量中的值,car中的值还是’Audi’。

3)检查是否不相等

requested_topping='mushroom'
if requested_topping != 'anchovies':
    print("Hold the anchovies")

输出

Hold the anchovies

4)比较数字

>>> age=18 
>>> age==18
True
>>> age<21
True
>>> age<=21
True
>>> age>21
False
>>> age>=21
False
answer=17
if answer!=42:
    print('That is not the correct answer.Please try again!')

输出

That is not the correct answer.Please try again!

5)检查多个条件

**1.and 相当于与 &&**必须所有条件都满足

>>> age_0=22
>>> age_1=18
>>> age_0>=21 and age_1>=21
False
>>> age_1=22
>>> age_0>=21 and age_1>=21
True

2.or相当于或 || 多个条件中只有一个满足即可

>>> age_0=22 
>>> age_1=18 
>>> age_0>=21 or age_1>=21  
True

6)检查特定值是否再列表中

1.检查特定值在列表中 in关键字若在则返回true,不在则返回false

>>> requested_toppings=['mushrooms','onions','pineapple']  
>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings  
False

2.检查特定值不在列表中 not in 关键字 若不在则返回true,在则返回false

>>> requested_toppings=['mushrooms','onions','pineapple']  
>>> 'pepperoni' not in requested_toppings 
True

7)bool表达式

bool表达式(bool变量)是条件测试的别名,其值只能为flase或true

game_active=True
can_edit=False

三.if语句

1)简单的if语句

格式

if condition_test:
    do something
age=19
if age>=18:
    print('You are old enough to vote')
     print('Have you registered to vote yet')

输出

You are old enough to vote
Have you registered to vote yet

2)ifelse 语句

age=17
if age>=18:
    print('You are old enough to vote')
    print('Have you registered to vote yet')
else:
    print('Sorry,you are to young to vote')
    print('Plesae register to vote as soon as you turn 18')

输出

Sorry,you are to young to vote
Plesae register to vote as soon as you turn 18

3)if-elif-else结构

elif即相当于C++中的else if,用法相同

age=12
if age<4:
    print('Your admission cost is $0.')
elif age<18:
    print('Your admission cost is $5.')
else :
    print('Your admission cost is $10.')

输出

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

这里的price要用str(price)转换为字符串格式输出

输出

Your admission cost is $5.

if-elif-else可以使用多个

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

输出

Your admission cost is $5.

eg2:将列表中的数字以序数形式输出

numbers=[1,2,3,4,5,6,7,8,9]
for number in numbers:
    if number==1:
        print(str(number)+'st')
    elif number==2:
        print(str(number)+'nd')
    elif number==3:
        print(str(number)+'rd')
    else:
        print(str(number)+'th')

输出

1st
2nd
3rd
4th
5th
6th
7th
8th
9th

可以省略else块

age=12
if age<4:
    price=0
elif age<18:
    price=5
elif age<65:
    price=10
elif age>=65:
    price=5
print("Your admission cost is $"+str(price)+'.')

输出

Your admission cost is $5.
requested_topping=['mushrooms','extra cheese']
if 'mushrooms' in requested_topping:
    print('Adding mushrooms')
if 'pepperoni' in requested_topping:
    print("Adding pepperoni")
if 'extra cheese' in requested_topping:
    print('Adding extra cheese')
print("\nFinished making your pizza")

输出

Adding mushrooms
Adding extra cheese

Finished making your pizza

这里的第二第三个if不能改成elif ,否则只会执行一个if

四:使用if处理列表

1)检查特殊元素

requested_toppings=['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
    if requested_topping=='green peppers':
        print("Sorry, we are out of green peppers right now.")
    else:
        print('Adding '+requested_topping+'.')
print('\nFinished making your pizza')

输出

Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.

Finished making your pizza

2)确定列表不是空的

requested_toppings=[]
if requested_toppings:
    for requested_topping in requested_toppings:
        if requested_topping =='green peppers':
            print("Sorry, we are out of green peppers right now.")
        else:
            print('Adding '+requested_topping+'.')
else:
    print("Are you sure you want a plain pizza")

输出

Are you sure you want a plain pizza

如果是空列表 则if requested_toppings返回false

3)使用多个列表

available_toppings=['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']
requested_toppings=['mushrooms','french frices','extra cheese']
for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print('Adding '+ requested_topping+'.')
    else:
        print("Sorry,we don't have "+requested_topping+".")
print('\nFinished making your pizza')   

输出

Adding mushrooms.
Sorry,we don't have french frices.
Adding extra cheese.

Finished making your pizza
发布了101 篇原创文章 · 获赞 1 · 访问量 2999

猜你喜欢

转载自blog.csdn.net/weixin_44699689/article/details/104099886
今日推荐