一看就會的20個“非常有用”的python小技巧,你一定要試試

Hello,大家好,我是程式汪小成~

Python現在非常流行,主要是因為它簡單,容易學習。你可以用它來完成很多工,比如資料科學和機器學習、web開發、指令碼編寫、自動化等。

這裡總結了20條很有用的tips給你:

01把列表中的值作為引數傳遞給方法

可以使用“ * ”提取列表中的所有元素:

my_list = [1, 2, 3, 4]print(my_list) # [1, 2, 3, 4]print(*my_list) # 1 2 3 4

當我們想將列表中的所有元素作為方法引數傳遞時,這很有用:

def sum_of_elements(*arg): total = 0 for i in arg: total += i return totalresult = sum_of_elements(*[1, 2, 3, 4])print(result) # 10

02獲取列表的所有中間元素

_, *elements_in_the_middle, _ = [1, 2, 3, 4, 5, 6, 7, 8]

print(elements_in_the_middle) # [2, 3, 4, 5, 6, 7]

03一行賦值多個變數

one, two, three, four = 1, 2, 3, 4

04列表推導

你可以使用推導如,讓我們將列表中的每個數字都取二次方:

numbers = [1, 2, 3, 4, 5]squared_numbers = [num * num for num in numbers]print(squared_numbers)

推導不僅僅侷限於使用列表。還可以將它們與字典、集合和生成器一起使用。讓我們看另一個例子,使用字典推導將一個字典的值提升到二階:

![](100 Helpful Python Tips You Can Learn Before Finishing Your Morning Coffee。assets/1_4D3OCbHOCfHiI8A3ru4xRQ。png)

Comprehensions are not just limited to working with lists。 You can also use them with dictionaries, sets, and generators as well。

dictionary = {‘a’: 4, ‘b’: 5}squared_dictionary = {key: num * num for (key, num) in dictionary。items()}print(squared_dictionary) # {‘a’: 16, ‘b’: 25}

05一行列印多個元素

print(“Hello”, end=“”)print(“World”) # HelloWorldprint(“Hello”, end=“ ”)print(“World”) # Hello Worldprint(‘words’, ‘with’, ‘commas’, ‘in’, ‘between’, sep=‘, ’)# words, with, commas, in, between

06 不使用迴圈來重複字串

name = “Banana”print(name * 4) # BananaBananaBananaBanana

07列印多個值,每個值之間使用自定義分隔符

你可以很容易地做高階列印:

print(“29”, “01”, “2022”, sep=“/”) # 29/01/2022print(“name”, “domain。com”, sep=“@”) # name@domain。com

08 不能在變數名的開頭使用數字

four_letters = “abcd” # this works4_letters = “abcd” # this doesn’t work

09 不能在變數名的開頭使用運算子

+variable = “abcd” # this doesn’t work

10顛倒列表的順序

my_list = [‘a’, ‘b’, ‘c’, ‘d’]my_list。reverse()print(my_list) # [‘d’, ‘c’, ‘b’, ‘a’]

11 使用step函式對字串切片

my_string = “This is just a sentence”print(my_string[0:5]) # This# Take three steps forwardprint(my_string[0:10:3]) # Tsse

12 反向切片

my_string = “This is just a sentence”print(my_string[10:0:-1]) # suj si sih# Take two steps forwardprint(my_string[10:0:-2]) # sjs i

13 只有開始或結束索引的部分切片

表示切片的開始和結束的索引都是可選的。

my_string = “This is just a sentence”print(my_string[4:]) # is just a sentenceprint(my_string[:3]) # Thi

14你不能把0作為數字的第一個數字

number = 0110 # this doesn‘t work

15 Floor 除法

print(3/2) # 1。5print(3//2) # 1

16 == 和 “is” 的差別

“ is ”檢查兩個變數是否指向記憶體中的同一個物件。“ == ”比較這兩個物件的值是否相等。

first_list = [1, 2, 3]second_list = [1, 2, 3]# Is their actual value the same?print(first_list == second_list) # True# Are they pointing to the same object in memoryprint(first_list is second_list) # False, since they have same values, but in different objects in memorythird_list = first_listprint(third_list is first_list) # True, since both point to the same object in memory

17 更改分配給另一個變數的變數的值

當一個變數被賦值給另一個變數時,它的值實際上被複制到第二個變數中。這意味著第一個變數之後的任何變化都不會反映在第二個變數中:

first = “An initial value”second = firstfirst = “An updated value”print(first) # An updated valueprint(second) # An initial value

18 檢查一個字串是否大於另一個字串

first = “abc”second = “def”print(first < second) # Truesecond = “ab”print(first < second) # False

19 檢查字串是不是從特定字元開始的

my_string = “abcdef”print(my_string。startswith(“b”)) # False

20 使用id()找到變數的唯一id

print(id(1)) # 4325776624print(id(2)) # 4325776656print(id(“string”)) # 4327978288

最後

希望以上我所分享的這些,能夠給小夥伴們帶來一些幫助!

若小夥伴有其它更好的建議或意見,歡迎在評論區進行討論。