1. 程式人生 > >Python編程入門到實踐 - 筆記( 5 章)

Python編程入門到實踐 - 筆記( 5 章)

python if 判斷

第 5 章練習了以下內容

簡單的 if 判斷語句

判斷字符串是否相等,還是不等

進行數字的大小比較

and,or 比較

檢查列表中是否存在指定的元素

if,if-else,if-elif-else 語句寫法

if 判斷列表是否為空

使用多個列表進行比較判斷

這一章的內容也比較簡單,感覺和 shell 差不多,但還是多練習吧。

希望路過的大牛指出不足,小弟在此謝過了。



一個簡單的 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!

本文出自 “LULU” 博客,請務必保留此出處http://aby028.blog.51cto.com/5371905/1964929

Python編程入門到實踐 - 筆記( 5 章)