Beautiful and concise Python (1)

1. Explanation of the "Beautiful and Concise Python" series of articles

  • This series of articles is about some very concise and very used Python that I saw when I was reading the code written by the Great God. Maybe a line of code can achieve many functions. Record it here to improve my coding level!
  • Each article will share 1-2 pieces of code, although it is very brief, but believe me, through this series of articles, you will find how beautiful Python is!
  • To summarize: Python can do everything except that it can't have children!

Two, if-else statement

  • Common writing
x = int(input('请输入x:'))
if x > 1:
    x = 1
else:
    x = 10
  • Concise writing
x = int(input('请输入x:'))
1 if x > 1 else 10

Three, exchange 2 variables

  • Common writing
a,b = 1,2
temp =a 
a = b
b = temp
print(a,b)
  • Concise writing
a,b = 1,2
a,b = b,a
print(a,b)

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/114360891