Python Class 5:if语句

本文详细介绍了Python中条件测试(如相等、不相等、大小写敏感等)、if-else语句、if-elif-else结构,以及列表处理(包括特殊元素查找、空列表检查和多列表操作)。通过实例演示了字符串比较、数字比较、布尔表达式和条件判断的实际应用场景。
摘要由CSDN通过智能技术生成

目录

5.1 示例

5.2 条件测试 

1.检查条件是是否相等

2.检查时区分大小写

3.检查是否不相等

4.比较数字

5.多条件检查

6.检查特定值

7.检查不在特定值是否在列表中

8.布尔表达式

5.3 if语句

1.简单语句

2.if-else语句

3.if-elif-else语句

4.使用多个elif代码

5.省略else

6.测试多个条件

5.4 列表处理  

1.检查特殊元素

2.检查空列表

3.使用多列表

作业


5.1 示例

>>> cars=['audi','bmw','subaru','toyota']
>>> for car in cars:
...   if car=='bwm':
...     print(car.upper())
...   else:
...     print(car.title())
...
Audi
Bmw
Subaru
Toyota

5.2 条件测试 

每条 if 语句的核心都是一个值为 True False 的表达式,这种表达式被称为条件测试。Python根据条件测试的值为True 还是False 来决定是否执行if 语句中的代码。如果条件测试的值为True,Python就执行紧跟在if 语句后面的代码;如果为False ,Python就忽略这些代码。

1.检查条件是是否相等

>>> car='bmw'
>>> car=='bmw'
True
>>> car=='toyota'
False

2.检查时区分大小写

>>> car='bmw'
>>> car=='Bmw'
False

3.检查是否不相等

>>> car='bmw'
>>> car!='toyota'
True

4.比较数字

>>> age=18
>>> age==18
True

>>> answer=17
>>> answer==42
False

5.多条件检查

and

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

or

>>> age_0>=21 or age_1>=21
True

6.检查特定值

使用关键字in

>>> foods=["mushrooms","onions","pineapple"]
>>> 'mushrooms' in foods
True
>>> 'tomato' in foods
False

7.检查不在特定值是否在列表中

使用not in

>>> dislike_food='tomato'
>>> if dislike_food not in foods:
...  print("we don't have "+ dislike_food)
...
we don't have tomato

8.布尔表达式

布尔表达式要么为True要么为False

5.3 if语句

1.简单语句

>>> if age>=18:
...  print("you are an adult")
...
you are an adult

2.if-else语句

>>> if age>=18:
...  print("you are an adult")
... else:
...  print("you are not an adult")
...
you are an adult

3.if-elif-else语句

>>> age=17
>>> if age>=18:
...  print("you need cost 10yuan")
... elif age<4:
...  print("you are free")
... else:
...  print("5 yuan")
...
5 yuan

4.使用多个elif代码

可使用多个elif代码设定不同条件

5.省略else

if-elif结构后可没有else

6.测试多个条件

在可能有多个条件为True的情况下,不适合使用elif或else

name=['curry','lebron','kobe']
if 'curry' in name:
	print('curry')
if 'lebron' in name:
	print('lebron')
if 'jordon' in name:
	print('jordon')

curry
lebron

5.4 列表处理  

1.检查特殊元素

names=['curry','lebron','kobe']
for name in names:
	if name=='curry':
		print("Golden State")
	else:
		print("other")

Golden State
other
other

2.检查空列表

Python在列表至少包含一个元素时返回True,在列表为空时返回False。

names=['1']
if names:
	print("ok")

ok

3.使用多列表

names=['curry','jordon','bosh','lebron','kobe']
teams=['kobe','wade','jordon']
for team in teams:
	if team in names:
		print("same player")
	else:
		print("only one")

same player
only one
same player

作业

1.检查两个字符串相等和不等。

>>> a='my name is liming'
>>> b='My name is liming'
>>> a==b
False

使用函数lower() 的测试。

>>> a='my name is liming'
>>> b='My name is liming'
>>> a==b.lower()
True

检查两个数字相等、不等、大于、小于、大于等于和小于等于。

>>> 3==5
False
>>> 3!=5
True
>>> 3>5
False
>>> 3<5
True
>>> 3>=3
True
>>> 3<=5
True

使用关键字and 和or 的测试.

>>> (3<=5)and(3!=5)
True
>>> (3>=5)or(3!=5)
True

测试特定的值是否包含在列表中并测试特定的值是否未包含在列表中。

>>> a=range(1,11)
>>> b=5
>>> if b in a:
...  print("ok")
...
ok

>>> c=11
>>> if c not in a:
...  print("yes")
...
yes

2.假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 'yellow' 'red'

编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。

alien_color='green'
if alien_color=='green':
	print("you get 5 points")

you get 5 points

3.像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。

如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。

如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。

alien_color='green'
if alien_color=='green':
	print("you get 5 points")
else:
	print("you get 10 points")

4.将练习5-4中的if-else 结构改为if-elif-else 结构。

如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。

如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。

如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。

alien_color='red'
if alien_color=='green':
	print("you get 5 points")
elif alien_color=='yellow':
	print("you get 10 points")
else:
	print("you get 15 points")

you get 15 points

5.设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。

如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。

如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。

如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。

如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。

如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。

如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。

age=23
if age<2:
	print("you are baby")
elif age<4:
	print("you are walking")
elif age<13:
	print("you are child")
elif age<20:
	print("you are teenger")
elif age<65:
	print("you are adult")
else:
	print("you are older")

you are adult

6.创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。

将该列表命名为favorite_fruits ,并在其中包含三种水果。

编写5if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息

favorite_fruits=['apple','melo','pee']
if 'apple' in favorite_fruits:
	print("you like this")
if 'melo' in favorite_fruits:
	print("you like this too")

you like this
you like this too

7.创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每个用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。 如果用户名为'admin' ,就打印一条特殊的问候消息。 否则,打印一条普通的问候消息

names=['admin','lorry','brand','kite','caca']
for name in names:
	if name=='admin':
		print("hello admin")
	else:
		print("welcome")

hello admin
welcome
welcome
welcome
welcome

8.在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。 如果为空,就打印消息“We need to find some users!”。 删除列表中的所有用户名,确定将打印正确的消息。

names=[]
if names:
	for name in names:
		print('welcome')
else:
	print('we need users')

we need users

9.按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。 创建一个至少包含5个用户名的列表,并将其命名为current_users 。 再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。 遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指 出这个用户名未被使用。 确保比较时不区分大消息;换句话说,如果用户名'John' 已被使用,应拒绝用户名'JOHN'

current_users=['a','b','c','d','e']
new_users=['A','e','C','g','t']

for new_user in new_users:
	if new_user.lower() in current_users:
		print("no")
	else:
		print("yes")

no
no
no
yes
yes

10.序数表示位置,如1st2nd。大多数序数都以th结尾,只有123例外。 在一个列表中存储数字1~9。 遍历这个列表。 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 2nd 3rd 4th 5th 6th 7th 8th 9th ,但每个序数都独占一行。

numbers=range(1,10)
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值