the following are some practical Python skills and tools I have collected for a long time. I hope they can be helpful to novices who have just learned python.
1. Exchange variables
x = 6 y = 5 x, y = y, x print x >>> 5 print y >>> 6
2.if statement in line
print "Hello" if True else "World" >>> Hello
3. Connection
The last method below is cool when binding two different types of objects.
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. Digital skills
#Rounding down after division print 5.0//2 >>> 2 # The 5th power of 2 print 2**5 >> 32
5. Pay attention to the division of floating-point numbers
print .3/.1 >>> 2.9999999999999996 print .3//.1 >>> 2.0
6. Numerical comparison
This is a simple method that I have never seen so great in many languages
x = 2 if 3 > x > 1: print x >>> 2 if 1 < x > 0: print x >>> 2
7. Iterate over both lists at the same time
nfc = ["Packers", "49ers"] afc = ["Ravens", "Patriots"] for teama, teamb in zip(nfc, afc): print teama + " vs. " + teamb >>> Packers vs. Ravens >>> 49ers vs. Patriots
Python basic learning, resource sharing, part-time exchange and technical exchange are all friends who work hard together. Welcome to join qq group 806549072
8. List iteration with index
teams = ["Packers", "49ers", "Ravens", "Patriots"] for index, team in enumerate(teams): print index, team >>> 0 Packers >>> 1 49ers >>> 2 Ravens >>> 3 Patriots
9. List derivation
Given a list, we can brush out the even list method:
numbers = [1,2,3,4,5,6] even = [] for number in numbers: if number%2 == 0: even.append(number)
Change to the following:
numbers = [1,2,3,4,5,6] even = [number for number in numbers if number%2 == 0]
Isn't it great, ha ha.
10. Dictionary derivation
Similar to list derivation, dictionaries can do the same work:
teams = ["Packers", "49ers", "Ravens", "Patriots"] print {key: value for value, key in enumerate(teams)} >>> {'49ers': 1, 'Ravens': 2, 'Patriots': 3, 'Packers': 0}
11. Initialize the value of the list
items = [0]*3 print items >>> [0,0,0]
12. Convert list to string
teams = ["Packers", "49ers", "Ravens", "Patriots"] print ", ".join(teams) >>> 'Packers, 49ers, Ravens, Patriots'
13. Get elements from dictionary
I admit that the try/except code is not elegant, but here is a simple method. Try to find the key in the dictionary. If the corresponding value is not found, set the second parameter as its variable value.
data = {'user': 1, 'name': 'Max', 'three': 4} try: is_admin = data['admin'] except KeyError: is_admin = False data = {'user': 1, 'name': 'Max', 'three': 4} is_admin = data.get('admin', False)
14. Get a subset of the list
Sometimes you only need some elements in the list. Here are some ways to get a subset of the list.
x = [1,2,3,4,5,6] #First 3 print x[:3] >>> [1,2,3] #Middle 4 print x[1:5] >>> [2,3,4,5] #Last 3 print x[-3:] >>> [4,5,6] #Odd term print x[::2] >>> [1,3,5] #Even term print x[1::2] >>> [2,4,6]
15. Assembly
in addition to the built-in data types in python, the collection module also includes some special use cases. In some cases, Counter is very practical. If you have participated in Facebook hacker Cup this year, you can even find its usefulness.
from collections import Counter print Counter("hello") >>> Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
16. Iteration tools
like the collections library, there is a library called itertools, which can effectively solve some problems. One use case is to find all combinations. It can tell you all the combinations of elements in a group
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
compared with practical technology, this is a very interesting thing. In python, True and False are global variables, so:
False = True if False: print "Hello" else: print "World" >>> Hello
This article ends here. If you have any cool tricks, you can leave a message below.