- Booleans布爾運算:布爾運算僅有兩個布爾值True和False。
- 兩個等于號==表示“是否相等”拴疤,例如:
1==2
False
又如:
"hello" == "hello"
True
- 嘆號加等于號!=表示不等于宏榕,例如:
1 != 1
False
"eleven" != "seven"
True
2 != 10
True
- 大于等于和小于等于表示為:> = 和< =
- if條件語句
- if治力,例如:
num = 12
if num > 5:
print("Bigger than 5")
if num <=47:
print("Between 5 and 47")
結(jié)果輸出為:
Bigger than 5
Between 5 and 47
- if...else,例如:
x = 4
if x == 5:
print("Yes")
else:
print("No")
輸出結(jié)果為:
No
- if...elif,其中elif表示else if块请,例如:
num = 7
if num == 5:
print("Number is 5")
elif num == 11:
print("Number is 11")
elif num == 7:
print("Number is 7")
else:
print("Number isn't 5, 11 or 7")
輸出結(jié)果為:
Number is 7
- Boolean Logic布爾邏輯娜氏,and、or墩新、not
- and贸弥,兩者都滿足時才為True,例如:
1 == 1 and 2 == 2
True
1 == 1 and 2 == 3
False
1 != 1 and 2 == 2
False
2 < 1 and 3 > 6
False
- or抖棘,有一個滿足時就為True茂腥,例如:
1 == 1 or 2 == 2
True
1 == 1 or 2 == 3
True
1 != 1 or 2 == 2
True
2 < 1 or 3 > 6
False
- not,取反切省,例如:
not 1 == 1
False
not 1 > 7
True
- 運算符優(yōu)先級
- ==高于or最岗,例如:
False == False or True
True
False == (False or True)
False
(False == False) or True
True
-
python中運算優(yōu)先級(從上到下):
python運算優(yōu)先級
- while循環(huán)
- 如果滿足條件,則只執(zhí)行一次if后面的語句朝捆,而while語句則是滿足條件時般渡,循環(huán)執(zhí)行while后面的語句,直至不滿足條件芙盘,例如:
i = 1
while i <=5:
print(i)
i = i + 1
print("Finished!")
輸出結(jié)果為:
1
2
3
4
5
Finished!
- infinite loop無限循環(huán)驯用,程序始終循環(huán)運行,輸入Ctrl + C或關(guān)閉程序才能終止儒老,例如:
while 1==1:
print("In the loop")
這一程序會一直輸出In the loop
- 終止循環(huán)(要嵌套在循環(huán)內(nèi))break蝴乔,程序在滿足條件處終止,跳出循環(huán)驮樊,不在執(zhí)行下面的語句薇正,例如:
i = 0
while 1==1:
print(i)
i = i + 1
if i >= 5:
print("Breaking")
break
print("Finished")
如果不添加if語句,該循環(huán)將一直進行下去囚衔,1==1為開啟循環(huán)挖腰,結(jié)果輸出為:
0
1
2
3
4
Breaking
Finished
- 繼續(xù)循環(huán)continue,滿足條件時练湿,程序不再執(zhí)行下面的語句猴仑,返回到循環(huán)的開頭,開始下一次循環(huán)肥哎,例如:
i = 0
while True:
i = i +1
if i == 2:
print("Skipping 2")
continue
if i == 5:
print("Breaking")
break
print(i)
print("Finished")
輸出結(jié)果為:
1
Skipping 2
3
4
Breaking
Finished
- List列表辽俗,使用方括號,并用逗號隔開項[ 篡诽, 榆苞, ,]霞捡,最后一項后面加不加逗號都可以,調(diào)用其中的項時也用方括號薄疚,例如:
words = ["Hello", "world", "!"]
print(words[0])
print(words[1])
print(words[2])
輸出結(jié)果為:
Hello
world
碧信!
空列表使用一對里面沒有內(nèi)容的方括號[]表示赊琳,例如:
empty_list = []
print(empty_list)
[]
列表內(nèi)的項可以是不同類型的,如整數(shù)砰碴、小數(shù)躏筏、字符串、變量等呈枉,例如:
number = 3
things = ["string", 0, [1, 2, number], 4.56]
print(things[1])
print(things[2])
print(things[2][2])
輸出結(jié)果為:
0
[1, 2, 3]
3
索引除了可以調(diào)用列表內(nèi)的項趁尼,還可調(diào)用字符串中的項,但整數(shù)不可以猖辫,例如:
str = "Hello world!"
print(str[6])
輸出結(jié)果為:
w
對列表中的某一項進行更改酥泞,例如:
nums = [7, 7, 7, 7, 7]
nums[2] = 5
print(nums)
輸出結(jié)果為:
[7, 7, 5, 7, 7]
列表也可以像字符串一樣進行加、乘運算啃憎,例如:
nums = [1, 2, 3]
print(nums + [4, 5, 6])
print(nums * 3)
輸出結(jié)果為:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
使用in來檢查某一項是否在列表中芝囤,例如:
words = ["spam", "egg", "spam", "sausage"]
print("spam" in words)
print("egg" in words)
print("tomato" in words)
輸出結(jié)果為:
True
True
False
其中in還可用于檢查一個字符串是否是另一個字符串的子集。
使用not來檢查某一項是否不在列表中辛萍,例如:
nums = [1, 2, 3]
print(not 4 in nums)
print(4 not in nums)
print(not 3 in nums)
print(3 not in nums)
輸出結(jié)果為:
True
True
False
False
若要在列表末尾加上一項悯姊,可在列表變量名后加上.append(),例如:
nums = [1, 2, 3]
nums.append(4)
print(nums)
輸出結(jié)果為:
[1,2,3,4]
所要獲得列表中項的個數(shù)贩毕,可用len函數(shù)悯许,例如:
nums = [1, 3, 5, 2, 4]
print(len(nums))
輸出結(jié)果為:
5
若要在列表的任意位置插入一項,可在列表變量名后加上.insert()辉阶,用index指示插入位置先壕,例如:
words = ["Python", "fun"]
index = 1
words.insert(index, "is")
print(words)
輸出結(jié)果為:
['Python', 'is', 'fun']
若要輸出列表中某一項是第幾項,可在變量名后加上.index睛藻,若該項不在列表中則報錯启上,例如:
letters = ['p', 'q', 'r', 's', 'p', 'u']
print(letters.index('r'))
print(letters.index('p'))
print(letters.index('z'))
輸出結(jié)果為:
2
0
ValueError: 'z' is not in list
- range函數(shù)
- 如果range()函數(shù)的括號中僅指定一個數(shù),那么則生成從0開始至小于指定數(shù)的最大整數(shù)的連續(xù)整數(shù)店印,例如:
numbers = list(range(10))
print(numbers)
輸出結(jié)果為:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- 如果range()函數(shù)的括號中指定有兩個數(shù)冈在,那么則生成從小數(shù)到大數(shù)的連續(xù)整數(shù),不包括大數(shù)按摘,例如:
numbers = list(range(3, 8))
print(numbers)
print(range(20) == range(0, 20))
輸出結(jié)果為:
[3, 4, 5, 6, 7]
True
- 如果range()函數(shù)的括號中指定有三個數(shù)包券,則第一個和第二個數(shù)表示范圍,第三個數(shù)表示間隔且必須為整數(shù)炫贤,例如:
numbers = list(range(5, 20, 2))
print(numbers)
輸出結(jié)果為:
[5, 7, 9, 11, 13, 15, 17, 19]
- 列表中項的迭代循環(huán)
- 使用while循環(huán)實現(xiàn)溅固,例如:
words = ["hello", "world", "spam", "eggs"]
counter = 0
max_index = len(words) - 1
while counter <= max_index:
word = words[counter]
print(word + "!")
counter = counter + 1
輸出結(jié)果為:
hello!
world!
spam!
eggs!
- 使用更簡潔的for循環(huán)實現(xiàn),例如:
words = ["hello", "world", "spam", "eggs"]
for word in words:
print(word + "!")
輸出結(jié)果為:
hello!
world!
spam!
eggs!
- for循環(huán)通暢用于確定次數(shù)的循環(huán)迭代兰珍,例如:
for i in range(5):
print("hello!")
輸出結(jié)果為:
hello!
hello!
hello!
hello!
hello!