Python Development (Basic): Common functions

Source: Internet
Author: User


    • Enumerate

function definition:

def enumerate (sequence, start=0):
n = Start
For Elem in sequence:
Yield N, Elem
n + = 1

Examples of functions:
>>> seasons = [' Spring ', ' Summer ', ' Fall ', ' Winter ']>>> list (enumerate (seasons)) [(0, ' Spring '), (1, ' Summer '), (2, ' Fall '), (3, ' Winter ')]>>> list (Enumerate (seasons, Start=1)) [(1, ' Spring '), (2, ' Summer '), (3, ' Fa ll '), (4, ' Winter ')]


Usage Note: Enumerate (list,start=n) generates a list of tokens, and the starting value of the subscript is determined by N, incrementing

  • range(stop)

  • range(start, stop[, step])

  • xrange(stop)

  • xrange(start, stop[, step])

    Start: Start range (incl.)

    Stop: End range (not included)

    Step: Each increment range

Cases:

>>> range [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range (1, one) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> R Ange (0, 5) [0, 5, ten,, 25]>>> range (0,, 3) [0, 3, 6, 9]>>> range (0,-10,-1) [0,-1,-2,-3, -4, -5,-6,-7,-8, -9]>>> range (0) []>>> range (1, 0) []
the difference between range and xrange:

The range in Python2 is created with the values listed in memory, and Xrange is only created in memory when used, so xrange performance is higher than range,

The xrange is removed from the Pyhton3, the range is preserved, and the range in Python3 is equivalent to xrange in Python2

Both have the same usage

This article is from the "Xiaoice" blog, be sure to keep this source http://wbb827.blog.51cto.com/6948425/1932986

Python Development (Basic): Common functions

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.