Python Learning Record (10)--------Looping statements

Source: Internet
Author: User
Tags for in range

Looping statements

One of the loops in Python:

For....in ... Cycle:

Print the elements in the list or tuple in turn:

# Looping Statements for...inclassmates=['name1','name2','  name3','name4']for in  Classmates:    print(classmate)

The results of the operation are as follows:

d:\python>learn.pyname1name2name3name4

Calculates the addition of the number 0-10:

# Loop statement for x in .... sum=0 for in [0,1,2,3,4,5,6,7,8,9,10]:
#将打印语句写在代码块中, there will be a loop printing effect

sum=sum+i
Print (sum)

The printing results are as follows:

d:\python>learn.py013610152128364555

If you write a print statement outside the code block, only the final result is printed:

# Loop statement for x in .... sum = 0 for in [0,1,2,3,4,5,6,7,8,9,10]:    = sum + xprint(sum)

The printing results are as follows:

D:\python>learn.py55

A small number of elements of the loop can be all the elements listed, but if the number of elements is very many, such as the calculation of the sums 0-101, we have all the elements listed is not realistic, so you can use the range () function, he can generate an integer sequence, and then use the list function to turn it into a list, For example, Range (5) represents a positive integer starting from 0 that is less than 5, and the following example is the code from the positive integer sums of 0-100:

# Loop statement for x in .... sum = 0 for in range (101):    = sum + xprint(sum)

The printing results are as follows:

d:\python>learn.py5050

While loop

The while loop is an infinite loop, so long as the condition in the while is satisfied, it loops until the condition is not satisfied and then exits the loop.

Exercise: Calculate the sum of all even numbers within 100, implemented in a while loop:

# Loop statementwhile sum = 0i=2while (i<101):    = sum + i    i=i+2print (SUM)

Here's a second exercise to print the elements in the list in turn:

#Loop Statement while#Practice#please use the loop to print a hello to each name in the list in turn, xxx!:l=['name1','name2','Name3','Name4']l=Len (L) I=0 while(i<l):Print(L[i]) I=i+1

The printing results are as follows:

d:\python>learn.pyname1name2name3name4

Break

In a looping statement, break is used to end the loop prematurely, for example, we want to print a positive integer of 0-100, and stop printing at >18 and print out the end of the word:

# break using i=0while (i<101)    :print(i)    i=i+1    if(i>18):        break print (' print end  ')

The printing results are as follows:

d:\python>learn.py0123456789101112131415161718 Print End

Continue

In the loop, if we need to skip this loop, continue with the following loop, then use the Continue function, such as to print a positive integer from 1-10, but skip 8 does not print, the code can be as follows:

# Break using i=0while (i<10):    i=i+1    if(i==8):         continueprint (l    )print (' end  ')

The printing results are as follows:

d:\python>learn.py1234567910 print End

For example, print the odd number in 1-10:

# Break is used i=0while (i<10):    i=i+1    if(i%2==0):         continueprint (l    )print (' end  ')

The printing results are as follows:

d:\python>learn.py13579 print End

Python learning Record (10)--------Looping statements

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.