Python Route-basics-8 loops

Source: Internet
Author: User

There are two loops in Python, namely A For loop and a while loop, which iterate over the data of the sequence:

For loop

The For loop iterates through each element of a list, tuple, or string, for example:

names = ["zhangcong""lisanjiang" "Pangzhiguo  "] for in names:    print  name # Execution Results Zhangconglisanjiangpangzhiguo

So for the X in ... A loop is a statement that assigns each element to a variable x and then executes the indentation.
Example: You need to figure out 1-10

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten= 0 for in numbers:    + = i    #< /c7> sum + = i   = =   sum = sum + i  it's just shorthand. print  sum#  Execution Result 55

The list can also be replaced with a range, and the range () function can generate an integer sequence, such as a range (5) that generates a sequence that is less than 5 of the integer starting at 0:

sum =0 forIinchRange (1, 11): Sum+=IPrintsum#Execution Results55that's what the authorities are explaining. Range (...) Range (stop)-List of integers range (start, stop[, step])-List of integers Return a list containing an arithmetic progression of integers. #returns a list of integers that contain a arithmetic progression. Range (I, j) returns [I, I+1, i+2, ..., j-1];    Start (!) defaults to 0. When step isGiven, it specifies the increment (ordecrement). For example, range (4) returns [0, 1, 2, 3]. The end point isomitted! These is exactly the valid indices forA list of 4 elements.

While Loop

While loop: As long as the conditions on the cycle, the general use of the dead loop will be used, such as we want to calculate the sum of all the odd within 100, can be implemented with a while loop:

# the loop internal variable n is continuously self-reducing until it becomes 1, and the while condition is no longer satisfied, and the loop exits sum == round while n > 0:    = sum +  n     = n-2print sum

Break

Break: Jump out of the current loop, end of loop
Example: Print 1-100, when I > 50 exits the Loop

i = 1 while True:        if i >:        breakPrint  i    + = 1 output result:12345678 ... 474849

Continue

Continue: Jump out of this loop for the next cycle
For example: printing 1-10, but not including 5

 for  in range    :if i = = 5:        continue    print  i  #  execution Results 1234678910

Python Route-basics-8 loops

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.