程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

There are 20 very useful Python tips you can learn at a glance. You must try them

編輯:Python

Hello, Hello everyone , I'm Chen Chen ~

Python It's very popular now , Mainly because it's simple , Easy to learn . You can use it to accomplish many tasks , Such as data science and machine learning 、web Development 、 scripting 、 Automation, etc. .

Here is a summary of 20 A very useful tips Here you are. :

01 Pass the values in the list as parameters to the method

have access to " * " Extract all elements in the list :

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

When we want to pass all the elements in the list as method parameters , It's very useful :

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

02 Get all the intermediate elements of the list

_, *elements_in_the_middle, _ = [1, 2, 3, 4, 5, 6, 7, 8]
print(elements_in_the_middle) # [2, 3, 4, 5, 6, 7]

03 Assign multiple variables to a row

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

04 The list of deduction

You can use derivation such as , Let's take every number in the list to the second power :

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

Derivation is not limited to using lists . You can also compare them with dictionaries 、 Collections are used with generators . Let's take another example , Use dictionary derivation to raise the value of a dictionary to the second order :

![](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 multiple elements on one line

print("Hello", end="")
print("World") # HelloWorld
print("Hello", end=" ")
print("World") # Hello World
print('words', 'with', 'commas', 'in', 'between', sep=', ')
# words, with, commas, in, between

06 Do not use loops to repeat strings

name = "Banana"
print(name * 4) # BananaBananaBananaBanana

07 Print multiple values , Use a custom separator between each value

You can easily do advanced printing :

print("29", "01", "2022", sep="/") # 29/01/2022
print("name", "domain.com", sep="@") # [email protected]

08 The number... Cannot be used at the beginning of a variable name

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

09 The operator... Cannot be used at the beginning of a variable name

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

10 Reverse the order of the list

my_list = ['a', 'b', 'c', 'd']
my_list.reverse()
print(my_list) # ['d', 'c', 'b', 'a']

11 Use step Function to slice a string

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

12 Reverse slice

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

13 Only partial slices that start or end the index

The index that represents the beginning and end of the slice is optional .

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

14 You can't put 0 As the first number of numbers

number = 0110 # this doesn't work

15 Floor division

print(3/2) # 1.5
print(3//2) # 1

16 == and “is” The difference between

" is " Check whether two variables point to the same object in memory ." == " Compare whether the values of the two objects are equal .

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 memory
print(first_list is second_list)
# False, since they have same values, but in different objects in memory
third_list = first_list
print(third_list is first_list)
# True, since both point to the same object in memory

17 Change the value of a variable assigned to another variable

When a variable is assigned to another variable , Its value is actually copied into the second variable . This means that any changes after the first variable will not be reflected in the second variable :

first = "An initial value"
second = first
first = "An updated value"
print(first) # An updated value
print(second) # An initial value

18 Check whether one string is larger than another

first = "abc"
second = "def"
print(first < second) # True
second = "ab"
print(first < second) # False

19 Check if the string starts with a specific character

my_string = "abcdef"
print(my_string.startswith("b")) # False

20 Use id() Find the unique of the variable id

print(id(1)) # 4325776624
print(id(2)) # 4325776656
print(id("string")) # 4327978288

Last

I hope the above I share , Can bring some help to the little friends !

If your partner has other better suggestions or opinions , Welcome to the comments area for discussion .

If my friends like it , Just like it ~ Thank you for your support .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved