Python 基礎(四) if語句

2021-08-15 16:33:21 字數 1405 閱讀 1081

sublime text 超級好用,而且可以直接執行 python **,在編輯器頁面按下ctrl + b即可執行,厲害了我的st

cars = ['audi', 'bmw', 'subaru', 'tiyota']

for car in cars:

if car == 'bmw':

print(car.upper())

else:

print(car.title())

宣告乙個列表,通過 for 迴圈判斷列表中是否存在乙個值和bmw一樣,是的話將bmw轉為大寫bmw,否則將列表中其他的字串首字母大寫

檢查字串是否相等、不等

car = 'audi'

car == 'bmw'

輸出 false

car = 'audi'

car != 'bmw'

輸出 true

age =18

age == 18

輸出 true

age_0 = 22

age_1 = 18

age_0 >= 21

and age_1 >=21

輸出false

cars = ['audi', 'bmw', 'subaru', 'toyota']

'audi'

in cars

輸出true

cars = ['audi', 'bmw', 'subaru', 'toyota']

'mini'

notin cars

輸出true

age = 12

if age > 4

print("大於4")

elif age < 18:

print("小於18")

else:

print("大於4小於18")

cars = 

if cars:

print("列表非空")

else:

print("列表是空的")

cars = ['audi', 'bmw', 'subaru', 'toyota']

types = ['small', 'middle', 'big', 'toyota']

fortype

in types:

iftype

in cars:

print("tpyes 列表中有 cars 列表中的值")

else:

print("tpyes 列表沒中有 cars 列表中的值")

Python語法基礎(四) if else語句

注意 if else語句中,方法體需要縮排 cars bmw toyota audi subaru for car in cars if car bmw print car.upper else print car.lower 輸出 bmw toyota audi subaru1.and連線,表示 ...

Delphi基礎(四)語句

注釋 1.括號內注釋 2.行注釋 3.符號內注釋 with語句 with 物件 do begin 語句end if語句 1.if then 2.if then else if語句可以巢狀使用,當if需要跟復合語句時,語句需要用begin和end包括 例如 if x y then begin z x ...

Python基礎(四) 條件語句

1.if語句if 條件 行 必須放到乙個語句塊中 語句塊就是組合在一起的一組程式語句 在python中,空白 tab或空格 是有意義的!處於同一位置 縮排相同 的 組成乙個 塊!如果你在python互動環境下敲 還要特別留意縮排,並且退出縮排需要多敲一行回車 用於條件的符號 定義符號 等於 不等於 ...