Python basics-loop

Source: Internet
Author: User
For loop: a [11,22, 33,44] foriina: print (I) result: 11223344while loop: i0whilei & amp; lt; 3: # I & amp; lt; 3 is the cyclic condition, if the condition is true, execute the following loop body. if the condition is false, print (I) I + 1 is not executed. Result: 012break and Con... for loop:

A = [11,22, 33,44]

For I in:
Print (I)

Result:

11
22
33
44

While loop:

I = 0
While I <3: # I <3 is a loop condition. when the condition is true, the loop body after execution is not executed. if the condition is false
Print (I)
I + = 1

Result:

0
1
2

Break and Continue:

I = 0
While I <10:
Print ("I->:", I)
If I = 5:
Break # exit all loops when I = 5
I + = 1

Result:

I->: 0
I->: 1
I->: 2
I->: 3
I->: 4
I->: 5
X = 0
While x <9:
For x in range (0, 10 ):
If x = 6:
Continue # when x = 6, jump out of the current loop and continue the next loop
Print ("x->:", x)

Result:

X->: 0
X->: 1
X->: 2
X->: 3
X->: 4
X->: 5
X->: 7 # missing x->: 6
X->: 8
X->: 9

Others:

Enumrate:

A = ["aa", "bb", "cc", "dd"]

For I in enumerate (a, 5): # the default number starts from 0 and can be changed manually.
Print (I)

Result:

(5, 'A ')
(6, 'BB ')
(7, 'CC ')
(8, 'DD ')

Range:

1,

For n in range (1, 10 ):
Print (n)

Result:

1
2
3
4
5
6
7
8
9

2,

For x in range (0, 20, 4 ):
Print (x)

Result:

0
4
8
12
16
# Print in sequence with 4 as the step

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.