Python small function skills accumulation, python function skills
Preface: accumulation of various python small function skills on the way forward.
Enumerate: Enumeration
Format: Format the output.
When a string is output, print can be broken by commas (,), but when the string variable is increased and put together with the string constant, the comma increases. Not convenient. The format class is used for formatting. variables are put together to control the output format more conveniently. The specific fomat syntax indicates that there are a lot of blogs on the Internet, such as http://www.2cto.com/kf/201312/262068.html, which are slightly mentioned. Of course, there are also the most important official website materials: https://docs.python.org/2/library/string.html. For more information, see.
Age = 25 name = 'caroline 'print ('{0} is {1} years old. '. format (name, age) # output parameter print ('{0} is a girl. '. format (name) print ('{0 :. 3} is a decimal. '. format (1.0/3) # print ('{0: _ ^ 11} is a 11 length. '. format (name) # Use _ to fill the blank print ('{first} is as {second }. '. format (first = name, second = 'wendy ') # print ('My name is 0.name+'.format(open('out.txt', 'w '))) # Call method print ('My name is {0: 8 }. '. format ('fred ') # specify the width of 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():print numberif number > min_number:print number,min_numberbreak<generator object fib_generator at 0x0000000002173090>01123581321345589144144 100
It can be seen that a function with yield has iteration capability and is an iterator. Previously, when the data volume increases, it becomes very useful. For details, refer.
There are some tips for reference: https://www.airpair.com/python/posts/python-tips-and-traps.