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. :
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
_, *elements_in_the_middle, _ = [1, 2, 3, 4, 5, 6, 7, 8] print(elements_in_the_middle) # [2, 3, 4, 5, 6, 7]
one, two, three, four = 1, 2, 3, 4
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}
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
name = "Banana" print(name * 4) # BananaBananaBananaBanana
You can easily do advanced printing :
print("29", "01", "2022", sep="/") # 29/01/2022 print("name", "domain.com", sep="@") # [email protected]
four_letters = “abcd” # this works4_letters = “abcd” # this doesn’t work
+variable = “abcd” # this doesn’t work
my_list = ['a', 'b', 'c', 'd'] my_list.reverse() print(my_list) # ['d', 'c', 'b', 'a']
my_string = "This is just a sentence" print(my_string[0:5]) # This # Take three steps forward print(my_string[0:10:3]) # Tsse
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
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
number = 0110 # this doesn't work
print(3/2) # 1.5 print(3//2) # 1
" 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
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
first = "abc" second = "def" print(first < second) # True second = "ab" print(first < second) # False
my_string = "abcdef" print(my_string.startswith("b")) # False
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 .