Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the important skills that Python beginners must learn?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the important skills that Python beginners must learn". In daily operation, I believe many people have doubts about the important skills that Python beginners must learn. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the important skills that Python beginners must learn?" Next, please follow the editor to study!

1. Exchange value

Digital swapping usually involves values stored in temporary variables. However, we can do this by using a single line of code from the Python trick without using transient variables.

"" valueswapping "" a, baked 5,10 print (a, b) a, bb= b, a print (a, b) output 10,5

two。 A string of all items in the list

When you have to convolution a list of strings, you can update each item through a for loop to perform this operation continuously. However, this is troublesome, especially if the list is long. In Python, strings are immutable. Therefore, in every two concatenations, the left and right strings should be copied into a new string.

As shown below, using the join () function is a more concise solution:

P = ["Python", "is", "a", "popular", "language"] print ("" .join (p)) output Python is a popular language

3. The most common elements in the list

Determine the values that appear most frequently in the list. If different items appear in the same way, print one of them. Create a list set to delete redundant values. So you can find the maximum number of events for each item in the collection, and then consider the maximum.

List1 = [0,1,2,3,3,2,3,1,4,5,4] print (max (set (list1), key = list1.count)) output 3

4. Test whether two strings are words with the same letter and different order

Defanagram (string_1,string_2): "Test if the stringsare anagrams. String_1: string string_2: string returns: boolean"

Solve the above problems, so as to find out whether the two strings are the same letter disordered words. Given two strings string_1 and string_2, test whether the two strings are the same alphabetical disordered words.

From collections importCounter defanagram (string_1,string_2): returnCounter (string_1) = = Counter (string_2) anagram ('pqrs','rqsp') True anagram (' pqrs','rqqs') False

5. Reverse string

Slicing is a convenient technique in Python, and it can also be used to reverse the order of items in a string.

# with slicing str = "PQRST" reverse_str = str [::-1] print (reverse_str) Output TSRQP

6. Reverse the list

Use this method to create a copy of the list, and the list is not sorted in order. To create a copy, you need more space to accommodate all existing elements.

# using slicing approach defReverse (lst): lstlst1 = lst [::-1] return lst1 lst = [5,6,7,8,9,10] print (Reverse (lst)) output [10,9,8,7,6,5]

7. Transpose matrix

Transposing a matrix means transforming columns into rows and vice versa. Using Python, you can combine the following code with the zip function and use the * tool to extract the transpose list of the matrix.

Mat= [(5, 6, 7), (8, 9, 10), (11, 12, 13) For row in mat: print (row) print ("\ n") t_mat=zip (* mat) For row int _ mat: print (row) output (5 6,7) (8,9,10) (11,12,13) (14,15,16) (5,8,11) 14) 6, 9, 12, 15) (7, 10, 13, 16)

8. Chain comparison

In programming, it is normal to test more than two conditions. Suppose you need to test the following:

P < Q < r

It's really smarter to write it chained in Python. The task chain is represented as follows:

If p < Q < r: {.}

Return a Boolean value to compare whether it is correct or not. Examples are as follows:

# chaining comparison a = 3 print (1 < a 10 11 12) at this point, the study of "what are the important skills that beginners must learn in Python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report