How can Python code be written more gracefully

First and foremost, forget about writing in other languages and try to code in Python style. Once you’re good at it, you’ll think it’s beautiful!

1. Initialize multiple values

# > yes
s1,s2,s3 = [],[],0 
# > No
s1 = []
s2 = []
s3 = []
Copy the code

2. The ternary operator

# Not in Python? : expression, used if else instead
def func() :
	return return_value if True else other_value

def fib(n) :
	return n if n<2 else fib(n-1)+fib(n-2)
Copy the code

3. List generation

# > yes

[i for i in range(1.100)]
	# > Further filter
	# is_prime() --> Bool added to list if True
	[i for i in range(2, n+1) if isPrime(i)]
# > no

res = []
for i in range(1.100):
	res.append(i)
Copy the code

4. Invert the list and STR elements

# For some students, I'm sure you want to encapsulate one by yourself. Actually, you don't need to use slices directly.
>>> s = '123'
>>> s[-1: : -1]
'321'

>>> a = [1.2.3]
>>> a[-1: : -1]
[3.2.1]

#! And this one up here, which is pretty easy to remember, put a minus 1 on both sides, and nothing in between.
#! Simple and convenient, wonderful! beautiful!
Copy the code

5. Sorting

#! Non-business imperative, do not write your own sorting algorithm, write slow, but also easy to error
>>> a = [10.12.11]
>>> sorted(a)
[10.11.12]

# > I know, my readers are not happy to see this, think you fool us, this is how we achieve the requirements.
Don't worry, let's do a problem first
Copy the code

Subject: Chinese + math, in descending order of total score. If the total score is the same, the students will be ranked in descending order according to their Chinese score. If the Chinese score is the same, the student number will be ranked in ascending order.

Test data N = 4 Student ID Chinese Math 10000001 64 90 10000002 90 60 10000011 85 80 10000003 85 80

# Although I seem to write a long code, the actual, core, sorting processing code is only one line.
# sorted(stus, key=lambda i: (i[1]+i[2], i[1], -i[0]), reverse=True)
If {total score descending order, Chinese score descending order, student number ascending order} nested if estimate headache death individual.

n = int(input())

stus = []
for i in range(n):
    l = list(map(int.input().split("")))
    stus.append(l)

res = sorted(stus, key=lambda i: (i[1]+i[2], i[1], -i[0]), reverse=True)

print(res)

Results: [[10000003, 85, 80], [10000011, 85, 80], [10000001, 64, 90], [10000002, 90, 60]

# How about time complexity?
Sorted: # sorted, using timSort, has an average time complexity of O(nlogn) that arguably outperforms most sorting algorithms.
Copy the code

6. Format the output

There are so many ways to format output in python.
# I know of at least five. But in fact, the real use and use of the following two.

# 1. F-string format output
>>> h = "hello"; w ="world"
>>> print(f"{h} {w}")
hello world

# 2. format output
>>> h = "hello"; w ="world"
>>> print("{} {}".format(h,w))
hello world

# can also be written fancy
>>> h = "hello"; w ="world"
>>> x = [h,w]
>>> print("{} {}".format(*x))
hello world
Copy the code

7. The tail

I know you have your own tips on how to write Python code more elegantly.

Feel free to leave a comment in the comments section so I can learn.