同時打印多個表達式用逗號分隔python
若是想在結果中添加逗號數組
自定義分隔符app
同時給多個變量賦值函數
交換多個變量的值工具
序列解包this
方法popitem,誰邊獲取一個鍵-值對並以元組的方式返回spa
要解包的序列包含的元素個數必須與你在等號左邊列出的目標個數相同,不然報錯3d
可以使用星號運算符(*)來收集多餘的值,這樣無需確保值和變量的個數相同code
還可將帶星號的變量放在其餘位置對象
還可用於其餘數據類型
方法name.endswith將返回True,不然執行else
name = input('what is your name?') if name.endswith('xiaoming'): print ('hello,Mr .xiaoming') else: print ('he he he')
num = int(input('Enter a number: ')) if num > 0: print('The number is positive') elif num < 0: print('The number is negative') else: print('The number is zero')
代碼塊嵌套
1 name = input('what is your name? ') 2 if name.endswith('xiaoming'): 3 if name.startswith('Mr.'): 4 print('hello,Mr. xiaoming') 5 elif name.startswith('Mrs.'): 6 print('hello,Mrs. xiaoming') 7 else: 8 print('hello, xiaoming') 9 else: 10 print('hello, stranger')
比較運算符
表達式 描述
x == y x等於y
x < y x小於y
x > y x大於y
x >= y x大於等於y
x <= y x小於等於y
x != y x不等於y
x is y x和y是同一個對象
x is not y x和y是不一樣的對象
x in y x是容器(如序列)y的成員
x not in y x不是容器(如序列)y的成員
== 用來檢查兩個對象是否相等,而is用來檢查兩個對象是否相同(是同一個對象)
1 name = input('what is your name?') 2 if 's' in name: 3 print('Your name contains the letter "s".') 4 else: 5 print('Your name does not contail the letter "s".')
布爾運算符
1 number = int(input('Enter a number between 1 and 10: ')) 2 if number <=10 and number >= 1: 3 print('Great!') 4 else: 5 print('wrong!')
斷言
1 x = 1 2 while x <=100: 3 print(x) 4 x += 1
1 words = ['this','is','an','ex','parrot'] 2 for word in words: 3 print(word)
range不包含結束位置
利用range打印1--100
1 for number in range(1, 101): 2 print(number)
1 d = {'x': 1, 'y': 2, 'z': 3}
2 for key in d: 3 print(key, 'corresponds to', d[key])
以元組的方式返回鍵-值對
1 d = {'x': 1, 'y': 2, 'z': 3}
2 for key, value in d.items(): 3 print(key, 'corresponds to', value)
並行迭代
1 names = ['tom','bob','jak','jary'] 2 ages = [10,11,12,13] 3 for i in range(len(names)): 4 print(names[i],'is', ages[i], 'years old'
一個頗有用的並行迭代工具是內置函數zip,它將兩個序列「縫合」起來,並返回一個有元組組成的序列。
1 names = ['tom','bob','jak','jary'] 2 ages = [10,11,12,13] 3 list(zip(names,ages)) 4 for name , age in zip(names,ages): 5 print(name, 'is' ,age, 'years old')
反向迭代和排序後在迭代
1. break (找出小於100的最大平方值)
1 from math import sqrt 2 for n in range(99, 0, -1): 3 root = sqrt(n) 4 if root == int(root): 5 print(n) 6 break
2.continue
1 for n in range(0, 10): 2 if n == 5: 3 continue 4 print(n)
執行結果
3.while True/break 成例
1 while True: 2 word = input('Please enter a word: ') 3 if word == '123': 4 print('The word was ', word) 5 break
執行結果
1 from math import sqrt 2 for n in range(99, 81, -1): 3 root = sqrt(n) 4 if root == int(root): 5 print(n) 6 break 7 else: 8 print("Did't find it!")
1 result = [] 2 for x in range(3): 3 for y in range(3): 4 result.append((x,y)) 5 print(result)
執行結果
1 name = input('please input your name: ') 2 if name == 'xiaoming': 3 print('welcome') 4 elif name == 'Enid': 5 # ...... (這些代碼不能運行,由於python中代碼塊不能爲空。加一個pass便可) 6 elif name == 'haha': 7 print('123 123')
1 name = input('please input your name: ') 2 if name == 'xiaoming': 3 print('welcome') 4 elif name == 'Enid': 5 # ...... 6 pass 7 elif name == 'haha': 8 print('123 123')
1.exec
函數exec將字符串做爲代碼執行
可能帶來的破壞代碼並不是覆蓋函數sqrt.函數sqrt該怎樣還怎樣,而經過exec執行賦值語句建立的變量位於scope中。
2.eval
與exec同樣,也可向eval提供一個命名空間,雖然表達式一般不會像語句那樣給變量從新賦值。
淺談做用域
向exec或eval提供命名空間時,可在使用這個命名空間錢在其中添加一些值
一樣,同一個命名空間可用於屢次調用exec或eval
函數 描述
chr(n) 返回一個字符串,其中只包含一個字符,這個字符對應於傳入的順序值n(0≤n<256)
eval(source[,globals[,locals]]) 計算並返回字符串表示的表達式的結果
enumerate(seq) 將字符串做爲語句執行
ord(c) 接受一個只包含一個字符的字符串,並返回這個字符的順序值(一個整數)
range([start,] stop[, step]) 建立一個由整數組成的列表
reversed(seq) 按相反的順序返回seq中的值,以便用於迭代
sorted(seq[,cmp][,key][,reverse]) 返回一個列表,其中包含seq中的全部值且這些值是通過排序的
xrange([start,] stop[, step]) 建立一個用於迭代的xrange對象
zip(seq1, seq2,...) 建立一個適合用於並行迭代的新序列