This beautiful and concise Python mainly introduces two ways of writing, which will greatly save the time and cost of writing code and improve your Python writing level.
1. Create a list quickly
- Example 1: Create a list quickly
ls = [i for i in range(0,10)]
print(ls)
- Example 2: Of course, we can also use this method to quickly create a list of strings
lst_str = [('hi '+i) for i in ['dongjk','ki','fi']]
print(lst_str)
Second, calculate the sum of the even numbers in the list
ls = [i for i in range(0,10)]
print(ls)
s = sum([num for num in ls if num % 2 == 0])
print(s)
3. How to quickly create a collection
set1 = {
x ** 2 for x in range(10) if x % 2 == 0}
print(set1)