Preface: Python has accumulated a variety of small function tricks encountered on the way forward.
Enumerate: Enumeration
format: formatted output.
When you output a string, the print plus comma breaks, but when the string variable gets up, the same string constants are added, and the comma becomes a lot more. Not very convenient. Formatting with the format class and putting variables together makes it easier to control the output format. The specific Fomat Grammar explains that the online blog is also a big pile, such as http://www.2cto.com/kf/201312/262068.html, slightly mentioned some. Of course, there are the most important information of the official website: https://docs.python.org/2/library/string.html. can refer to the reference.
Age = 25name = ' Caroline ' Print (' {0} was {1} years old. '. Format (name, age) #输出参数print (' {0} is a girl. '. Format (name)) print (' {0:.3} is a decimal. '. Format (1.0/3)) #小数点后三位print (' {0:_^11} is a one-length. '. Format (name) #使用_补齐空位print (' {first} is as {second}. '. Format (first=name, second= ' Wendy ')) #别名替换print (' My name is {0.name} '. Format (open (' OUT.txt ', ' W ')) #调用方法print (' My Name is {0:8}. '. Format (' Fred ')) #指定宽度drinks ={"Coffee", "tea", "milk", "water"}for index,drink in Enumerate (drinks): s= "Item:" +str ( Index) + ' is ' +drinkprint Sprint ' item: ', index, ' is ', Drinkprint ' item: {} is {} '. Format (Index,drink)
yield: Generator
Def fib_generator (): A=0b=1while True:yield aa,b=b,a+bmin_number=100print fib_generator () for number in Fib_generator () :p rint numberif number > Min_number:print Number,min_numberbreak<generator object fib_generator at 0X0000000002173090>01123581321345589144144 100
As you can see, a function with yield, with iteration capability, is an iterator. Before, when the amount of data is large, it becomes very useful. Specific reference: http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/, did not carefully find the introduction of the official website.
There are a few tips for reference: Https://www.airpair.com/python/posts/python-tips-and-traps.
Python Small function skill accumulation