Python Process Control-for sequence

Source: Internet
Author: User

A loop is a structure that causes the program to repeat a certain number of times.
The same is true for conditional loops, where the condition becomes false and the loop ends.

The For loop can be used in a sequence and can be iterated through, with the following syntax:

for iterating_var in sequence:    statements(s)

We've talked about sequences that have strings, tuples, and lists, like we define a string a, and then we go through the various characters of a and print out the information as follows:

In [1]: a=‘ABC‘In [2]: aOut[2]: ‘ABC‘In [3]: for i in a:   ...:     print(i)   ...:     ABC

By default, printing with print comes with a newline character, so how do we get the result without a line break, as follows:

In [6]: for i in a:    print i,      #在Python2.7.5中   ...:     A B Cfor i in a:    print(i,end=‘‘)       #在Python3.6.1中

For example, we can create a list of List1:

In [8]: list1=list()      #生成一个空列表In [9]: list1=[1,2,3,4]In [10]: for i in list1:   ....:     print i   ....:     1234

Generate a list of 1-9, with a step of 1 by default:

In [11]: range(1,10)Out[11]: [1, 2, 3, 4, 5, 6, 7, 8, 9]

If the set step is 2, 3 is:

In [13]: range(1,10,2)Out[13]: [1, 3, 5, 7, 9]In [14]: range(1,10,3)Out[14]: [1, 4, 7]

For example, we can print 10 numbers using a For loop plus range:

In [15]: for i in range(0,10):   ....:     print(i)   ....:     0123456789

To print a list of 1-10, we can also write:

#!/usr/local/python3/bin/pythonprint([i for i in range(1,11)])

The results are printed as follows:

[[email protected] ~]# python a.py[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

We can also do some operations on I, the code is as follows:

[[email protected] ~]# cat a.py   #!/usr/local/python3/bin/pythonprint([i for i in range(1,11)])print([i+1 for i in range(1,11)])     #让遍历出来的i都加上1print([i*2 for i in range(1,11)])      #让遍历出来的i都乘上2print([i-2 for i in range(1,11)])      #让遍历出来的i都减去2

The results of the operation are as follows:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10][2, 3, 4, 5, 6, 7, 8, 9, 10, 11][2, 4, 6, 8, 10, 12, 14, 16, 18, 20][-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]

For example, we can also write a program that asks for the sum of 1-100 numbers as follows:

#!/usr/local/python3/bin/pythonsum=0for i in range(1,101):    sum+=iprint(sum)

Python Process Control-for sequence

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.