- Python中区分大小写
cars = ['A']
for car in cars:
if car == 'a':
print(True)
else:
print(False)
-----------------------------------
false
- 判断两值是否相等用(!=不相等)
- 检查多个条件可以用and 或者 or (但是一个是与,一个是或)
- 判断特定的值是否已包含在列表中,用关键字in 不在列表中,则用not in
- 布尔表达式只有两种结果True /False
- Python并不要求if-elif结构后面必须要有else代码块
课后习题
car = 'subaru'
print("Is car == 'subaru'?I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'?I predict Flase.")
print(car == 'audi')
-----------------------------------------------------------------
Is car == 'subaru'?I predict True.
True
Is car == 'audi'?I predict Flase.
False
"""检查两个字符串是否相等"""
apple = "Hello Python!"
dog = "Hello worl!"
print(apple == dog)
-------------------------------------------
False
""""使用lower()的测试"""
car = 'Lucy'
if car.lower() == 'lucy':
print(True)
----------------------------
True
""""检查两个数相等,不等,大于,小于,大于等于和小于等于"""
num1 = 5
num2 = 10
if num1 == num2:
print(True)
else:
print(False)
if num1 > num2:
print(True)
else:
print(False)
if num1 >= num2:
print(True)
else:
print(False)
------------------------------------------------------------
False
False
False
alien_colors = ['green','yellow','red']
for alien_color in alien_colors:
if (alien_color == 'green'):
print("Player win 5 powers.")
else:
print("Nothing!")
-----------------------------------------------------
Player win 5 powers.
Nothing!
Nothing!
alien_colors = ['green','yellow','red']
for alien_color in alien_colors:
if (alien_color == 'green'):
print("Because of shotting the alien,player win 5 powers.")
else:
print("Player win 10 powers.")
--------------------------------------------------------------------------
Because of shotting the alien,player win 5 powers.
Player win 10 powers.
Player win 10 powers.
alien_colors = ['green','yellow','red']
for alien_color in alien_colors:
if (alien_color == 'green'):
print("green alien,player win 5 powers.")
elif(alien_color == 'yellow'):
print("yellow alien,player win 10 powers.")
else:
print("red alien,player win 15 powers.")
---------------------------------------------------------------------------
green alien,player win 5 powers.
yellow alien,player win 10 powers.
red alien,player win 15 powers.
age = 1
if age < 2:
print('你是婴儿!')
elif 2 <= age < 4:
print('你正在蹒跚学步!')
elif 4 <= age < 13:
print('你是儿童!')
elif 13 <= age < 20:
print('你是青少年!')
elif 20 <= age < 65:
print('你是成年人!')
elif age >= 65:
print('你是老年人!')
------------------------------------------------------
你是婴儿!
message = "You really like "
favorite_fruits = ['apples','bannanas','pears']
if 'apples' in favorite_fruits:
print(message+'apples'+'!')
if 'bannanas' in favorite_fruits:
print(message+'bannanas'+'!')
if 'pears' in favorite_fruits:
print(message+'pears'+'!')
if 'c' in favorite_fruits:
print(message+'c'+'!')
if 'd'in favorite_fruits:
print(message+'d'+'!')
----------------------------------------------------------------
You really like apples!
You really like bannanas!
You really like pears!
users = ['admin','ben','jone','lily','kitty']
for user in users:
if user == 'admin':
print("Hello admin,would you like to see a status report?")
else:
print("Hello,"+user+" thank you for logging in again.")
-------------------------------------------------------------------------------
Hello admin,would you like to see a status report?
Hello,ben thank you for logging in again.
Hello,jone thank you for logging in again.
Hello,lily thank you for logging in again.
Hello,kitty thank you for logging in again.
user_name = []
if len(user_name) == 0:
print('We need to find some users!')
for un in user_name:
if un == 'admin':
print('Hello admin, would you like to see a status report?')
else:
print('Hello, ' + un + ', thank you for logging in again')
----------------------------------------------------------------------------------
We need to find some users!
current_users = ['Alice','Ben','Jone','Lily','Cat']
new_users = ['Alice','Bob','Jone','Lucy','Mey']
for new_user in new_users:
if new_user in current_users:
print("We need to find new users!")
else:
print("The user "+new_user+" don't use.")
-----------------------------------------------------------
We need to find new users!
The user Lucy don't use.
The user Mey don't use.
!最后一点未实现
lists = list(range(1,10))
for list in lists:
if list == 1:
print('1st')
elif list == 2:
print('2nd')
elif list == 3:
print('3rd')
else:
print(str(list) + 'th')
------------------------------------------------------------
1st
2nd
3rd
4th
5th
6th
7th
8th
9th