Python is a very concise and elegant programming language. In other programming languages, you can do things in one line that require tedious code logic.
In this way, it not only reduces the development effort, but also greatly improves readability and avoids the pitfalls of developing complex logic.
In this article, I’ll show you how powerful 10 one-liners can be!
1. Swap variables
If you have learned C/C++ programming courses in college, you will be familiar with swapping variables, which is the most basic and very common.
If you use C/C++ to exchange variables, you need something like this:
int a, b, c;
c = a;
a = b;
b = c;
Copy the code
In Python, you can swap variables in a single line of code.
# a = 1; b = 2
a, b = b, a
# print(a,b) >> 2 1
Copy the code
This way, we don’t need to define an additional temporary variable, which is simple and intuitive.
2. Multivariable assignment
If you include more than one type of variable when assigning a variable, this is not possible in many programming languages, but it can be done in Python with one line of code:
A, b, * c = [1, 2, 3, 4, 5] # print (a, b, c) > > 1 2 [3, 4, 5]Copy the code
In Python, you can assign list elements to a specified variable with the symbol *, also called unpacking.
3. Sum 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 = [6] s = sum (a / 1:2) # print (s) > > 12Copy the code
Here, no complicated logic is required, just the proper use of Python slicing, the [1::] identifier, from the second element to the end of the list.
4. Delete multiple elements
# a = [1,2,3,4] del a[::2] # print(a) >> [2, 4]Copy the code
The powerful slicing syntax can also be used to delete multiple list elements at once.
5. Read the file line into the array
If conditions or loops are simple, column expressions can be used to simplify the code:
c = [line.strip() for line in open('file.txt')]
Copy the code
6. Write the string to a file
Python’s with method is simpler than the open and close methods used by other programming languages to read and write files, and it doesn’t have to close files:
with open('file.txt', 'a') as f: f.write('hello world')
Copy the code
7. Create a checklist
You can use inline for loops to create lists dynamically from other lists. You can modify the values directly, for example, string concatenation in the following example:
l = [('Hi ' + x) for x in ['Alice', 'Bob', 'Pete']]
# print(l) >> ['Hi Alice', 'Hi Bob', 'Hi Pete']
Copy the code
8. List mapping
You can use Python’s map() function to convert each list element to another type:
l = list(map(int, ['1', '2', '3']))
# print(l) >> [1, 2, 3]
Copy the code
9. Collection creation
Column expressions can be used in conjunction with conditional statements in addition to the simple for loop:
squares = { x**2for x in range(6) if x < 4 }
# print(squares) >> {0, 1, 4, 9}
Copy the code
Palindrome check
The palindrome check is an example of how to read the same character backwards and forwards in a single line of code:
# phrase = 'deleveled'
isPalindrome = phrase == phrase[::-1]
# print(isPalindrome) >> true
Copy the code
conclusion
There are a lot more programming skills involved in Python than that, and it requires continuous learning and summarizing to become proficient in Python.
The simplest and most effective way to improve your coding skills can be summed up in two things: “Look at other people’s code, and let them look at yours.”
Different people have different ideas and implementation methods. In the process of looking at other people’s code, you can see different coding methods and draw some advantages from them.
Let others review their own code, can continue to spur, motivate yourself to get rid of bad habits in the process of coding, wrong usage.
Dry recommended
In order to facilitate everyone, I spent half a month’s time to get over the years to collect all kinds of iso technical finishing together, content including but not limited to, Python, machine learning, deep learning, computer vision, recommendation system, Linux, engineering, Java, content of up to 5 t +, I put all the resources download link to a document, The directory is as follows:
All dry goods to everyone, hope to be able to support it!
Pan.baidu.com/s/1eks7CUyj… (Extraction code: 0000)