Tips for beginners of Python
The following are some Python practical skills and tools I have collected in recent years. I hope they will help you. Swap variable x = 6y = 5x, y = y, xprint x >>> 5 print y >>> 6 if statement in the row print "Hello" if True else "World" >>> the last method under the Hello connection is to bind two different types of objects appear cool. Nfc = ["Packers", "49ers"] afc = ["Ravens", "Patriots"] print nfc + afc >>> ['packers', '49ers ', 'raws', 'pattern'] print str (1) + "world" >>> 1 worldprint '1' + "world" >>> 1 worldprint 1, "world" >>> 1 worldprint nfc, 1 >>> ['packers ', '49ers'] 1 digit Tips # Round print 5.0 after division> 2 # print 2 ** 5 to the power of 2> 32 pay attention to the print division of Floating Point Numbers. 3 /. 1 >>> 2.9999999999999996 print. 3 //. 1 >>> 2.0 Value Comparison this is a simple method that is rare in many languages I have seen x = 2if 3> x> 1: print x >>> 2if 1 <x> 0: print x >>> 2. iterate two lists at the same time: nfc = ["Packers ", "49ers"] afc = ["Ravens", "Patriots"] for teama, teamb in zip (nfc, afc): print teama + ". "+ teamb >>> Packers. ravens> 49ers. patriots indexed list iteration teams = ["Packers", "49ers", "Ravens", "Patriots"] for index, team in enumerate (teams): print index, team >>> 0 Packers >>> 1 49ers >>> 2 Ravens >>> 3. The list of Patriots lists is derived to know a list. You can click here to select Method for getting an even number list: numbers = [1, 2, 3, 4, 5, 6] even = [] for number in numbers: if number % 2 = 0: even. the append (number) is converted into the following: numbers = [1, 2, 3, 4, 5, 6] even = [number for number in numbers if number % 2 = 0. Dictionary derivation is similar to list derivation. Dictionaries can do the same thing: teams = ["Packers", "49ers", "Ravens", "Patriots"] print {key: value for value, key in enumerate (teams) >>>{ '49ers': 1, 'raws': 2, 'pattern': 3, 'packers ': 0} the value of the initialization list items = [0] * 3 print items >>> [, 0] The list is converted to the string teams = ["Packers", "49ers ", "Ravens", "Patriots"] print ",". join (teams) >>> 'packers, 49ers, Ravens, Patriots' get elements from the dictionary. I admit that try/try T code is not elegant, but here is a simple method, try searching in the dictionary If the corresponding alue is not found, the second parameter is set as its variable value. Data = {'user': 1, 'name': 'max ', 'three': 4} try: is_admin = data ['admin'] doesn t KeyError: is_admin = False is replaced with data = {'user': 1, 'name': 'max ', 'three': 4} is_admin = data. get ('admin', False) gets the subset of the list. Sometimes, you only need some elements in the list. Here are some methods to get the subset of the list. X = [1, 2, 3, 4, 5, 6] # The first three print x [: 3] >>> [1, 2, 3] # Four middle print x [] >>> [,] # The last three print x [-3:] >>>>, 6] # print x [: 2]> [1, 3, 5] # print x [1: 2]> [2, 4, 6] solve FizzBuzz 60 characters ago Jeff Atwood promoted a simple programming exercise called FizzBuzz. The problem reference is as follows: write a program, print a number from 1 to, and a multiple from 3 to print "Fizz" to replace this number, and a multiple from 5 to print "Buzz". For a number that is both a multiple of 3 and a multiple of 5, print "FizzBuzz ". Here is a short and interesting way to solve this problem: for x in range (101): print "fizz" [x % 3*4:] + "buzz" [x % 5*4:] or x sets include some special use cases in the collection module in addition to the python built-in data types, counter is very useful in some cases. If you have been to Facebook HackerCup for the past year, you can even find something useful. From collections import Counterprint Counter ("hello") >>> Counter ({'l': 2, 'H': 1, 'E': 1, 'O ': 1}) Like the collections library, the iteration tool also has a library called itertools, which can effectively solve some problems. One use case is to find all the combinations. It tells you all the combinations of elements in a group that cannot be combined. from itertools import combinationsteams = ["Packers", "49ers ", "Ravens", "Patriots"] for game in combinations (teams, 2): print game >>> ('packers', '49ers') >>> ('packers ', 'raws') >>>> ('packers', 'pattern') >>> ('49ers', 'ravens') >>> ('49ers ', 'pattern') >>> ('ravens ', 'pattern') False = True this is a very interesting thing than practical technology. In python, true and False are global variables, so: False = Trueif False: print "Hello" else: print "World" >>> Hello