In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "what simple and practical Python code", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what simple and practical Python code" bar!
1. Swap two variables
# a = 1; b = 2 a, bb = b, a # print (aforme b) > > 2 1
Let's start with a classic: simply swapping assignment positions to swap the values of variables-in my opinion, this is the most intuitive way. You do not need to use temporary variables. It even applies to more than two variables.
two。 Multiple variable assignment
Print b, * c = [1, 2, 3, 4, 5] # (a) > 12 [3, 4, 5]
Swapping variables is actually a special case where python can allocate more than one variable at a time. Here, you can use it to assign list elements to a given variable, also known as unpacking. * the remaining values will be packaged again, which will result in a sublist of c. It can even be used in other locations of * (such as the beginning or middle of the list).
3. Sum every two elements of the list
# a = [1 print 2 3 7 4 5 5 6] s = sum (a [1:: 2]) # print (s) > > 12
There is no need for a special reduce function here, sum just adds items for each given iteration. The extended slice syntax [:] is used here to return the second element. You can read it as [start: stop: step], so [1DV] translates from the element of index 1 (the second element) to the end of the list (the second parameter does not give an argument), and always takes two steps.
4. Delete multiple elements of the list
# a = [1 del 2, 3 4] del a [:: 2] # print (a) > > [2,4]
Extended slicing syntax can also be used to delete multiple list elements at a time.
5. Read a file into a line array
C = [line.strip () for line in open ('file.txt')] # print (c) > > [' test1', 'test2',' test3', 'test4']
Using the python inline for loop, you can easily read files into an array of lines. You need to use strip () to remove the subsequent disconnects. If you want to keep them or if they are not important to you, you can use a shorter line:
C = list (open ('file.txt')) # print (c) > > [' test1\ n', 'test2\ n', 'test3\ n', 'test4\ n']
It's really easy to read files in Python. Note: you can also use the readlines () method if you prefer.
6. Write a string to a file
With open ('file.txt',' a') as f: f.write ('hello world') # print (list (open (' file.txt') > > ['test1\ n', 'test2\ n', 'test3\ n', 'test4\ n', 'hello world']
With the help of the With statement, you can write the content directly to the file. Be sure to open the file in the correct mode (where "a" means additional content).
7. Create a list
L = [('Hi'+ x) for x in ['Alice',' Bob', 'Pete']] # print (l) > > [' Hi Alice','Hi Bob','Hi Pete']
You can use inline for loops to dynamically create lists from other lists. You can modify the value directly, just like the string concatenation in this example.
8. List mapping
L = list (map (int, ['1percent,' 2percent,'3']) # print (l) > > [1,2,3]
You can also use the Pythons map () function to cast each list element to another type.
9. Collection creation
Squares = {Xerox 2 for x in range (6) if x
< 4 } # print(squares) >> {0,1,4,9
The collection is the same. In addition to inline for loops, you can even add conditions directly!
10. Palindrome check
# phrase = 'deleveled' isPalindrome = phrase = = phrase [::-1] # print (isPalindrome) > > true
Palindromes are a series of characters that read the same characters forward and backward. If a given string is a palindrome, you usually need some loops and conditions to check. In Python, you only need to compare the string with its reverse string. In addition to using the slicing operator [::-1], you can also use the reverse () function to reverse strings.
Thank you for your reading, the above is the content of "what simple and practical Python code", after the study of this article, I believe you have a deeper understanding of what simple and practical Python code is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.