1. Swap variables
Copy the Code code as follows:
x = 6
y = 5
X, y = y, X
Print X
>>> 5
Print Y
>>> 6
2.if statements within a row
Copy the Code code as follows:
Print "Hello" if True else "world"
>>> Hello
3. Connect
The last way to do this is to be cool when binding two different types of objects.
Copy the Code code as follows:
NFC = ["Packers", "49ers"]
AFC = ["Ravens", "patriots"]
Print NFC + AFC
>>> [' Packers ', ' 49ers ', ' Ravens ', ' patriots ']
Print str (1) + "World"
>>> 1 World
print ' 1 ' + "world"
>>> 1 World
Print 1, "World"
>>> 1 World
Print NFC, 1
>>> [' Packers ', ' 49ers '] 1
4. Calculation techniques
Copy the Code code as follows:
#向下取整
Print 5.0//2
>>> 2
# 2 of the 5-time Square
Print 2**5
>> 32
Note the division of floating-point numbers
Copy the Code code as follows:
Print. 3/.1
>>> 2.9999999999999996
Print. 3//.1
>>> 2.0
5. Numerical comparison
Copy the Code code as follows:
x = 2
If 3 > x > 1:
Print X
>>> 2
If 1 < x > 0:
Print X
>>> 2
6. Two lists simultaneous iterations
Copy the Code code as follows:
NFC = ["Packers", "49ers"]
AFC = ["Ravens", "patriots"]
For TeamA, teamb in Zip (NFC, AFC):
Print TeamA + "vs." + Teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
7. Indexed list iterations
Copy the Code code as follows:
Teams = ["Packers", "49ers", "Ravens", "patriots"]
For index, team in enumerate (teams):
Print index, Team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
8. List derivation
A list of known, brush to select even list method:
Copy the Code code as follows:
numbers = [1,2,3,4,5,6]
even = []
For number in numbers:
If number%2 = = 0:
Even.append (number)
Replace it with the following
Copy the Code code as follows:
numbers = [1,2,3,4,5,6]
even = [number for number in numbers if number%2 = = 0]
9. Dictionary derivation
Copy the Code code as follows:
Teams = ["Packers", "49ers", "Ravens", "patriots"]
Print {Key:value for value, key in enumerate (teams)}
>>> {' 49ers ': 1, ' Ravens ': 2, ' Patriots ': 3, ' Packers ': 0}
10. Initialize the value of the list
Copy the Code code as follows:
Items = [0]*3
Print items
>>> [0,0,0]
11. Converting a list to a string
Copy the Code code as follows:
Teams = ["Packers", "49ers", "Ravens", "patriots"]
Print ",". Join (teams)
>>> ' Packers, 49ers, Ravens, Patriots '
12. Getting elements from a dictionary
Do not use the following methods
data = {' user ': 1, ' name ': ' Max ', ' Three ': 4}
Try
Is_admin = data[' admin ']
Except Keyerror:
Is_admin = False
Replaced by
Copy the Code code as follows:
data = {' user ': 1, ' name ': ' Max ', ' Three ': 4}
Is_admin = data.get (' admin ', False)
13. Get Sub-list
Copy the Code code as follows:
x = [1,2,3,4,5,6]
#前3个
Print X[:3]
>>> [About]
#中间4个
Print X[1:5]
>>> [2,3,4,5]
#最后3个
Print X[-3:]
>>> [4,5,6]
#奇数项
Print X[::2]
>>> [1,3,5]
#偶数项
Print X[1::2]
>>> [2,4,6]
14.6 Character Resolution Fizzbuzz
A short time ago Jeff Atwood promoted a simple programming exercise called Fizzbuzz, which referred to the following questions:
Write a program that prints the number 1 to 100, 3 multiples print "Fizz" to replace this number, 5 multiples print "Buzz", for a number that is both a multiples of 3 and a multiples of 5 to print "Fizzbuzz".
Here is a short way to solve this problem:
For x in range (101):p rint "Fizz" [x%3*4::]+ "Buzz" [X%5*4::]or X
15. Collection
Use the Counter library.
Copy the Code code as follows:
From collections Import Counter
Print Counter ("Hello")
>>> Counter ({' L ': 2, ' h ': 1, ' E ': 1, ' O ': 1})
16. Iterative Tools
Like the collections library, there's a library called Itertools.
Copy the Code code as follows:
From Itertools import combinations
Teams = ["Packers", "49ers", "Ravens", "patriots"]
For-game in combinations (teams, 2):
Print game
>>> (' Packers ', ' 49ers ')
>>> (' Packers ', ' Ravens ')
>>> (' Packers ', ' patriots ')
>>> (' 49ers ', ' Ravens ')
>>> (' 49ers ', ' patriots ')
>>> (' Ravens ', ' patriots ')
17.False = = True
In Python, true and false are global variables, so:
Copy the Code code as follows:
False = True
If False:
Print "Hello"
Else
Print "World"
>>> Hello
The above is a personal summary of some common Pyton tips, more suitable for beginners, I hope you can like.