Python for loop \while loop

Source: Internet
Author: User

A list or tuple can represent an ordered Set. What if we want to access each element of a list in turn? such as List:

L = [' Adam ', ' Lisa ', ' Bart ']print l[0]print l[1]print l[2]

If the list contains only a few elements, it is OK to write, and if the list contains 10,000 elements, we cannot write 10,000 lines of Print.

This is where the loop comes in Handy.

The python for Loop can then iterate over each element of the list or tuple in Turn:

L = [' Adam ', ' Lisa ', ' Bart '] inL:    Print name

note: Name This variable is defined in the for loop, meaning that each element in the list is taken out, the element is assigned to name, and then the For loop body is executed (that is, the indented block of code).

This makes it very easy to traverse a list or tuple.

After the class exam, the teacher to statistical average results, 4 students are known to list the results are as follows:

L = [75, 92, 59, 68]

Please use the For loop to calculate the average score. Refer to the following code:

L = [75, 92, 59, 68]
sum = 0.0
For x in L:
Sum=sum+x
Print SUM/4

Python's while loop

Another loop that differs from the For loop is the while loop , which does not iterate over the elements of the list or tuple, but rather determines whether the loop ends with an Expression.

For example, to print an integer no greater than N from 0:

n = 10x = 0 whilex < N:    print x    x = x + 1 

The while loop first evaluates x < N, if true, executes the block of code for the loop body, otherwise, exits the Loop.

In the loop,x = x + 1 causes x to increase and eventually exits the loop because x < N is not True.

Without this statement, thewhile loop always evaluates to True for x < N, it loops indefinitely and becomes a dead loop, so pay special attention to the exit condition of the while Loop.

Task

Use the while loop to calculate the number of odd and. reference codes within 100:

sum = 0
x = 1
While X<100:
Sum=sum+x
X=x+2
Print sum
Or
sum = 0
x = 1
While (x%2==1) or (x<=100):
Sum+=x
X+=1
Print sum

When using a For loop or while loop, you can use the break statement if you want to exit the loop directly inside the loop . for example, to calculate integers from 1 to 100, we use while to implement:
sum = 0x = 1 whileTrue:    sum = sum + x    x = x + 1    if x >:        breakprint sum 

At first glance, while True is a dead loop, but in the loop, we also determine that x > 100 pieces are set up, using the break statement to exit the loop, which can also achieve the end of the Loop.

Task

Use the while True infinite loop with the break statement to calculate 1 + 2 + 4 + 8 + 16 + ... The first 20 items of the AND.

sum = 0
x = 1
n = 1
While True:
If n>20:
Break
Sum=sum+x
X=x*2

Print sum

Python for loop \while loop

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.