Usage of the "Python" range

Source: Internet
Author: User

Usage of range:

>>> Range (1,5) #代表从1到5 (not including 5)
[1, 2, 3, 4]
>>> Range (1,5,2) #代表从1到5, Interval 2 (not including 5)
[1, 3]
>>> Range (5) #代表从0到5 (not including 5)
[0, 1, 2, 3, 4]

List operations:

array = [ 1 , 2 , 5 , 3 , 6 , 8 , 4 ] #其实这里的顺序标识是 [ 1 , 2 , 5 , 3 , 6 , 8 , 4 ] ( 0 1 2 3 4 5 6 ) ( - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 ) >>> array[ 0 :] #列出0以后的 [ 1 , 2 , 5 , 3 , 6 , 8 , 4 ] >>> array[ 1 :] #列出1以后的 [ 2 , 5 , 3 , 6 , 8 , 4 ] >>> array[: - 1 ] #列出-1之前的 [ 1 , 2 , 5 , 3 , 6 , 8 ] >>> array[ 3 : - 3 ] #列出3到-3之间的 [ 3 ]

The swap variable in Python does not require a temporary variable! Such as:

A, b, c = B, c, a

Bubble Sort:

array=[1,2,5,3,6,8,4] for in range (len (array) -1,1,-1):    for in  Range (0,i):        if array[j]>array[j+1]:            array[j],array[j+1]=array[j+1 ],array[j]print array

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.