In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the strength of one line of Python code". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the strength of one line of Python code".
1. Exchange variable
If the university students who have studied Cramble programming +, they must be familiar with exchanging variables, which is the most basic and very commonly used.
If you exchange variables with C _ variables +, you need to do this:
Int a, b, c; c = a; a = b; b = c
In Python, variables can be exchanged with a single line of code.
# a = 1; b = 2 a print b = b, a # and (a) > 2 1
In this way, we do not need to define an additional temporary variable, which is not only simple, but also very intuitive.
two。 Multivariable distribution
If you assign variables with multiple types of variables at the same time, this is not possible in many programming languages, but it can be done with one line of code in Python:
Print b, * c = [1, 2, 3, 4, 5] # (a) > 12 [3, 4, 5]
In Python, list elements can be assigned to specified variables through the symbol *, which is also called unpacking.
3. The sum of every other element
If you have a list [1, 2, 3, 4, 5, 6] and you want to sum every other element [2, 4, 6], you can use it like this:
# a = [1 print 2 3 7 4 5 5 6] s = sum (a [1:: 2]) # print (s) > > 12
Here, you don't need to make use of complex logic, you just need to make rational use of Python slices, [1] identification, from the second element to the end of the list.
4. Delete multiple elements
# a = [1 del 2, 3 4] del a [:: 2] # print (a) > > [2,4]
Powerful slicing syntax can also be used to delete multiple list elements at a time.
5. Read file lines into an array
If the conditional or loop statement is simple, you can use column expressions to simplify the code:
C = [line.strip () for line in open ('file.txt')]
6. Write a string to a file
Compared with open and close, which are commonly used in other programming languages, the with method provided by Python is simpler and does not have to close the file:
With open ('file.txt', 'a') as f: f.write ('hello world')
7. Create a list
You can use inline for loops to dynamically create lists from other lists. You can modify the value directly, for example, the string concatenation in the following example:
L = [('Hi'+ x) for x in ['Alice',' Bob', 'Pete']] # print (l) > > [' Hi Alice','Hi Bob','Hi Pete']
8. List mapping
You can use the map () function of Python to convert each list element to another type:
L = list (map (int, ['1percent,' 2percent,'3']) # print (l) > > [1,2,3]
9. Collection creation
In addition to using simple for loop statements, column expressions can be used in conjunction with conditional statements:
Squares = {x**2for x in range (6) if x
< 4 } # print(squares) >> {0,1,4,9}
10. Palindrome check
Palindrome checking is a kind of problem that often occurs in written test questions. To put it simply, it reads the same characters forward and backward, which also baffles many fresh graduates. Using Python, one line of code can be completed:
# phrase = 'deleveled' isPalindrome = phrase = = phrase [::-1] # print (isPalindrome) > > Thank you for your reading. The above is the content of "the strength of a line of Python code". After the study of this article, I believe you have a deeper understanding of the strength of a line of Python code, 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.