Python Small function skill accumulation

Source: Internet
Author: User

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

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.