Python编程:从入门到实践------第5章:if语句

1、if语句的基本应用

输入如下代码:

foods=("apple","pear","melon","strawberry","lemon")
for food in foods:
    if food=="apple":
        print(food.upper())
    else:
        print(food.lower())

输出如下:

APPLE
pear
melon
strawberry
lemon

2.利用and与or检测多个语句

利用==来判断表达式的值是否相等,and相当于且,or相当于或,最后输出True或False。

age0=18
age1=20
print(age0==18 and age1<21)
print(age0==19 or age1<21)

输出如下:

True
True

3.利用in、not in检测特定值是否包含在列表

name=["liming","wanghua","zhangsan"]
user="liming"
print(user in name)
print(user not in name)
if user in name:
    print(user.title())

输出如下:

True
False
Liming

动手试一试 5-1 5-2:

car='subaru'
print("Is car =='subaru'? I predict True.")
print(car == 'subaru')

print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')

输出如下:

Is car =='subaru'? I predict True.
True

Is car == 'audi'? I predict False.
False

4.if语句的具体使用

python中if语句同C语言,可用if / if else / if elif else等结构,只是需要加冒号,在此不再赘述。

动手试一试5-3 - 5-7:

#5-3

alien_color='green'
if alien_color == 'green':
    print('You got 5 points.')

#5-4

alien_color='red'
if alien_color == 'green':
    print('You got 5 points.')
else:
    print('You got 10 points.')

#5-5

alien_color='red'
if alien_color == 'green':
    print('You got 5 points.')
elif alien_color == 'yellow':
    print('You got 10 points.')
elif alien_color == 'red':
    print('You got 15 points.')

#5-6

age=20
if age <2:
    print("婴儿")
elif age >=2 and age <4:
    print("蹒跚学步")
elif age >=4 and age <13:
    print("儿童")
elif age >=13 and age <20:
    print("青少年")
elif age >=20 and age <65:
    print("成年人")
elif age >=65:
    print("老年人")

#5-7

favourite_fruits=['apple','banana','pear']
fruit='apple'
if fruit in favourite_fruits:
    print("You really like "+fruit)
fruit='banana'
if fruit in favourite_fruits:
    print("You really like "+fruit)
fruit='pear'
if fruit in favourite_fruits:
    print("You really like "+fruit)
fruit='melon'
if fruit in favourite_fruits:
    print("You really like "+fruit)
fruit='strawberry'
if fruit in favourite_fruits:
    print("You really like "+fruit)

输出如下:

You got 5 points.
You got 10 points.
You got 15 points.
成年人
You really like apple
You really like banana
You really like pear

5.使用if语句处理列表

  1. for语句与if相结合,检查特殊元素。
numbers=['1','2','3','4','5']
for number in numbers:
    if number=='3':
        print("aaa")
    else:
        print("nnn")
  1. if +列表名 判断列表是否为空`
numbers=[]
if numbers:
    print("aaa")
else:
    print("errors")

动手试一试5-8 - 5-11

#5-8

names=['admin','b','c','d','e']
for name in names:
    if name=='admin':
        print("Hello "+name+",would you like to see a status report?")
    else:
        print("Hello "+name+",thank you for logging in again")

#5-9

names=[]
if names:
    for name in names:
        if name=='admin':
            print("Hello "+name+",would you like to see a status report?")
        else:
            print("Hello "+name+",thank you for logging in again")
else:
    print("We need to find some users")

#5-10

current_users=['abc','b','c','d','e']
new_users=['Abc','b','f','g','h']
for user in new_users:
    if user in current_users:
        print("For the name "+user+",You need a new name")
    else:
        print("For the name "+user+",You can use it")

#5-11

locations=[1,2,3,4,5,6,7,8,9]
for location in locations:
    if location == 1 or location ==2 or location ==3:
        print(location)
    else:
        print(str(location)+"st")

输出如下:

Hello admin,would you like to see a status report?
Hello b,thank you for logging in again
Hello c,thank you for logging in again
Hello d,thank you for logging in again
Hello e,thank you for logging in again
We need to find some users
For the name Abc,You can use it
For the name b,You need a new name
For the name f,You can use it
For the name g,You can use it
For the name h,You can use it
1
2
3
4st
5st
6st
7st
8st
9st

猜你喜欢

转载自blog.csdn.net/weixin_44487378/article/details/104084825
今日推荐