第 5 章練習瞭如下內容python
簡單的 if 判斷語句shell
判斷字符串是否相等,仍是不等app
進行數字的大小比較ide
and,or 比較post
檢查列表中是否存在指定的元素spa
if,if-else,if-elif-else 語句寫法字符串
if 判斷列表是否爲空it
使用多個列表進行比較判斷io
這一章的內容也比較簡單,感受和 shell 差很少,但仍是多練習吧。class
但願路過的大牛指出不足,小弟在此謝過了。
一個簡單的 if 判斷語句
循環打印 cars 列表中的元素,若是其中的元素等於 bmw,就所有大寫打印
不然只是將元素的首字母大寫
-------------------------------------------------
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
-----------------------------------------------------
Audi
BMW
Subaru
Toyota
判斷是否相等
大小寫不同,也會不等
--------------------------
car = 'Audi'
print(car == 'Audi')
--------------------------
True
--------------------------
car = 'Audi'
print(car == 'audi')
---------------------------
False
轉換大小寫進行比較
------------------------------------
car = 'Audi'
print(car.lower() == 'audi')
------------------------------------
True
檢查是否不相等
不過兩個值不相等,就打印
-----------------------------------------------
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")
------------------------------------------------
Hold the anchovies!
數字的比較
----------------------
age = 18
print(age == 18)
----------------------
True
進行 if 語句判斷,若是兩個數字不等,就打印
-----------------------------------------------------------------------------
answer = 17
if answer != 18:
print("That is not the correct answer. Please try again!")
-----------------------------------------------------------------------------
That is not the correct answer. Please try again!
不光能夠進行比較是否相等,還能夠比較大小
直接在 python 的 IDE 下進行比較是否是看着更方便。哈哈哈
>>> age = 19
>>> age < 21
True
>>> age <= 21
True
>>> age > 21
False
>>> age >= 21
False
檢查多個條件
and 當兩個條件都知足的狀況下,打印 True 不然打印 False
>>> 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
or 至少知足一個條件,打印 True 不然打印 False
>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 or age_1 >= 21
True
>>> age_0 = 18
>>> age_0 >= 21 or age_1 >= 21
False
檢查指定的值是否包含在列表中
----------------------------------------------------------------------------
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requested_toppings)
print('pepperoni' in requested_toppings)
----------------------------------------------------------------------------
True
False
給要查找的值指定一個變量並查找,若是不存就打印出來
----------------------------------------------------------------------------
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(user.title() + ", you can post a response if you wish.")
----------------------------------------------------------------------------
Marie, you can post a response if you wish.
簡單的 if 語句
設定年齡爲19,進行 if 語句判斷,若是大於18就打印
---------------------------------------------------
age = 19
if age >= 18:
print("You are old enough to vote!")
----------------------------------------------------
You are old enough to vote!
if-else 語句
設定年齡爲 17,與 18 進行比較,若是大於等於 18 就打印 if 下的語句,
不然打印 else 中的語句
----------------------------------------------------------------------------
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
------------------------------------------------------------------------------
Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!
if-elif-else 語句
年齡設定爲 17,分別進行判斷,小於 4 多少錢,小於 18 多少錢,其餘多少錢進行打印
--------------------------------------------------
age = 17
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) + ".")
-----------------------------------------------------------------
Your admission cost is $5.
使用多個 elif 代碼塊
判斷條件固然不止3個,這時候就用到了多個 elif 代碼塊
python 並不要求必須有 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.
檢查特殊元素
判斷列表中的元素並指定某個元素,進行判斷
-------------------------------------------------------------------------------------------
requsested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requsested_topping in requsested_toppings:
if requsested_topping == 'green peppers':
print("Sorry, we are out of green peppers right now.")
else:
print("Adding " + requsested_topping + ".")
print("\nFinished making your pizza!")
-------------------------------------------------------------------------------------------
Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.
Finished making your pizza!
在 循環以前先進行一個 if 判斷
if requested_toppings 意識是對列表進行判斷,列表中至少有一個元素時,返回 True,
如今這個列表爲空,返回值爲 False,打印 else 代碼塊中的內容
--------------------------------------------------------------
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
----------------------------------------------------------------
Are you sure you want a plain pizza?
使用多個列表
對 requested_toppings 進行遍歷,和 available_toppings 列表中的元素進行比較
-------------------------------------------------------------------------------------
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', '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 fries.
Adding extra cheese.
Finished making your pizza!