Python small function skills accumulation, python function skills

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.