文章目录

  • 前言
  • 一、if语句
  • 1.条件测试
  • 2.if语句
  • 3.使用if语句处理列表
  • 二、例题代码
  • 1.条件测试
  • 2.if语句
  • 3.if语句处理列表
  • 总结



前言

编程时经常需要检查一系列条件,并据此决定采用什么措施,if语句能够让你检查程序当前的状态,并采取相应的措施


一、if语句

1.条件测试


每条if语句的核心都是一个值为True或者False的表达式,这种表达式被称作条件测试。,如果if后的结果为True,那么就执行后面的程序,否则就不执行。

常用的条件测试有:==、!=、<、>、<=、>+等。
检查多个条件时,可以考虑使用and语句或者or语句。
注意,当检查字符串是否相等时,一定要注意大小写问题,因为python是严格区分大小写的,若是不需要区分大小写,可以将他们转换成小写字母在进行比较。
检查特定值是否在列表中,用in或者not in 。
布尔表达式,是一种特殊的条件测试,可以记录条件。

2.if语句

最简单的if语句是:if+条件测试+:,后面的语句需要缩进。
缩进的作用与for语句的作用是一样的,只有缩进才执行。
if-else语句,在条件测试通过了的时候执行,if后缩进的语句,当条件测试不通过,那么就执行else后面缩进的语句。
if-elif-else语句,它会依次检测条件语句,直到条件测试通过或者执行else,这种多用于有分级或者划分的地方。而且这种结构可以嵌套好多层,最后的else可以不用,改用elif这样会使代码更易读。

3.使用if语句处理列表

结合if语句与列表,可以对列表中特殊值进行特殊处理,也可以对列表中不断变换的值进行把握。

检查特殊元素:有时候列表中的元素是在不断减少的,我们需要结合if语句来判断那个元素减少到了0
确定列表使是不是空的:列表的空与否也会一定程度影响人们的决策,用if语句可以判断列表是否为空
使用多个列表:有时候需要根据不同的情况选择不同的列表,此时就有需要用到if语句。

二、例题代码

1.条件测试

1.令car=‘subaru’,然后对汽车进行预测,输出布尔类型的数据。
2.创建更多的条件测试,如字符串相等、数字比大小、列表中元素等

car='subaru'
print('Is car == subaru ? I predict True.')
print(car=='subaru')
print('\nIs car == audi ? I predict True.')
print(car=='audi')

str_1='hello world!'
str_2='Hello World!'
print(str_1==str_2)
print(str_1.lower()==str_2.lower())

num_1=123
num_2=234
num_3=345
print('num1是不是比num2小?')
print(num_1<num_2)
print('num2是不是比num3小?')
print(num_2<num_3)
print('num1是不是比num2小?且比num3小')
print(num_1<num_2 and num_1<num_3)
print('num2是不是比num3小?且比num1大')
print(num_1<num_2 and num_2<num_3)

num=['1','2','3','4']
print('1' in num)
print('1' not in num )

结果如下

Is car == subaru ? I predict True.
True

Is car == audi ? I predict True.
False
False
True
num1是不是比num2小?
True
num2是不是比num3小?
True
num1是不是比num2小?且比num3小
True
num2是不是比num3小?且比num1大
True
True
False

2.if语句

外星人颜色1:创建一个alien_color的变量,存储外星人的颜色为‘queen’ ‘yellow’和‘red’若是绿色则输出5点,若是黄色输出10点若是红色输出15点
2.人生的不同阶段
3.喜欢的水果,检查是否有水果。

#外星人颜色
alien_color='green'
if alien_color=="green":
    print('the player got 5 points')
if alien_color=="yellow":
    print('the player got 10 points')
if alien_color=="red":
    print('the player got 15 points')

alien_color='yellow'
if alien_color=="green":
    print('the player got 5 points')
else:
    print('you got 10 points')

alien_color='red'
if alien_color=="green":
    print('the player got 5 points')
elif alien_color=="yellow":
    print('the player got 10 points')
elif alien_color=="red":
    print('the player got 15 points')

#人生的不同阶段
age=23
if age<2:
    print("baby")
elif age>=2 and age<4:
    print('kid')
elif age>=4 and age<13:
    print("child")
elif age>=13 and age<20:
    print('teenager')
elif age>=20 and age<65:
    print('adult')
elif age>=65:
    print("senior")

#喜欢的水果
favourite_fruit=['apple','banana','watermelon']
if "apple" in favourite_fruit:
    print("you really like apple")
if "banana" in favourite_fruit:
    print('you really like banana')
if "watermelon" in favourite_fruit:
    print('you realyy like watermelon')
if 'peer' in favourite_fruit:
    print('you really like pear')
if 'orange' in favourite_fruit:
    print('you really like orange')

结果如下

the player got 5 points
you got 10 points
the player got 15 points
adult
you really like apple
you really like banana
you realyy like watermelon

3.if语句处理列表

1.以特殊的方式跟管理员打招呼,跟普通用户打招呼
2.处理没有用户的情况
3.检查用户名
4.序数

#打招呼
usernames=['admin','lihua','zhangsan','lisi','wangwu']
for username in usernames:
    if username=='admin':
        print('Hello admin, would you like to see a status report?')
    else:
        print('Hello '+username+',thank you for logging in again.')

#检查用户是否为空
usernames=[]
if len(usernames)==0:
    print('we need to find some users')

#确保用户名不会重复
current_users=['Admin','lihua','zhangsan','lisi','Wangwu']
new_users=['Zhangsan','lisi','xiaoming','xiaohong','xiaolv']
temp=[]
for current_user in current_users:
    current_user=current_user.lower()
    temp.append(current_user)
print(temp)
for new_user in new_users:
    if new_user.lower() in temp:
        print('you cannot use '+new_user)
    else:
        print("you can use "+new_user)

#序数
nums=list(range(1,11))
for num in nums:
    if num==1:
        print('1st')
    elif num==2:
        print('2nd')
    elif num==3:
        print('3rd')
    else:
        print(str(num)+'th')

结果如下

Hello admin, would you like to see a status report?
Hello lihua,thank you for logging in again.
Hello zhangsan,thank you for logging in again.
Hello lisi,thank you for logging in again.
Hello wangwu,thank you for logging in again.
we need to find some users
['admin', 'lihua', 'zhangsan', 'lisi', 'wangwu']
you cannot use Zhangsan
you cannot use lisi
you can use xiaoming
you can use xiaohong
you can use xiaolv
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th

注意,整数类型的数据不能与字符串类型的数据一起输出。


总结

本章重点有
条件测试
if语句的三种结构
if与for语句配合,处理列表。