Python Basic--list Conversion range () print results

Source: Internet
Author: User
Tags for in range

A function that is common in the For Loop is range (), but sometimes when you print a range directly, the result is not what we want, and at this point we can use the list to convert the result of range so that we clear out the elements in range.

For example:

s=10d=5 for in range (s,s+D):    s=s+1    print(s)     Print (i)     Print (Range (s, S + D, 1))     Print (List (range (s,s+d,1)))

Operation Result:

11
10
Range (11, 16)
[11, 12, 13, 14, 15]
12
11
Range (12, 17)
[12, 13, 14, 15, 16]
13
12
Range (13, 18)
[13, 14, 15, 16, 17]
14
13
Range (14, 19)
[14, 15, 16, 17, 18]
15
14
Range (15, 20)
[15, 16, 17, 18, 19]

Here you can see clearly the different results of two outputs, such as the result range (11, 16) and [11, 12, 13, 14, 15], in the statement

Print (range (s, S + D, 1)), the last parameter 1 is to indicate that the element step in range is 1, if write 2, meaning is one element to take one, so, modify the code:
s=10d=5 for in range (s,s+D):    s=s+1    print(s)     Print (i)     Print (Range (s, S + D, 1))     Print (List (range (s,s+d,1)))     Print (List (range (s,s+d,2)))

Operation Result:

11
10
Range (11, 16)
[11, 12, 13, 14, 15]
[11, 13, 15]
12
11
Range (12, 17)
[12, 13, 14, 15, 16]
[12, 14, 16]
13
12
Range (13, 18)
[13, 14, 15, 16, 17]
[13, 15, 17]
14
13
Range (14, 19)
[14, 15, 16, 17, 18]
[14, 16, 18]
15
14
Range (15, 20)
[15, 16, 17, 18, 19]
[15, 17, 19]

Python Basic--list Conversion range () print results

Related Article

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.