Python learning notes (11) Python statements (3) and python statements

Source: Internet
Author: User

Python learning notes (11) Python statements (3) and python statements

While loop statement

It is used to execute a program cyclically under a certain condition to process the same task that needs to be processed repeatedly.

Syntax:

While judgment condition: execution statement ......

The execution statement can be a single statement or statement block. The condition can be any expression, and any non-zero or non-null value is true.

When the condition is false, the loop ends.

Example: for Loop game

1 #! /Usr/bin/env python 2 #! Coding: UTF-8 3 4 5 "6 a word game: a random number is generated. Each time you enter a number, you can guess whether the number is a random number. 7 for 8 "9 10 import random11 12 num = random. randint (1,100) # return a int [1,100] 13 # Note: The for loop has a problem, so you have to know how many times the loop is 14 for I in range (0,100 ): 15 input_num = int (raw_input ("Please input a int:") 16 if input_num = num: 17 print "OK, you are right "18 print num19 break # Jump out of 20 elif input_num> num: 21 print" input number is larger than int from the current loop body. "22 else: 23 print" input number is smaller than int."

While game

1 #! /Usr/bin/env python 2 #! Coding: UTF-8 3 4 5 "6 while expression: 7 do something 8" 9 10 import random11 12 num = random. randint (1,100) # return a int [1,100] 13 14 15 I = 016 while I <10: # This can also be changed to 1, 17 print i18 input_num = int (raw_input ("Please input a int:") 19 if input_num = num: 20 print "OK, you are right "21 print num22 break # Jump out of the current loop body and execute the content 23 after the loop body. elif input_num> num: 24 print" input number is larger than int. "25 else: 26 print" input number is smaller than int. "27 28 I + = 1

Break

The break statement is used to terminate a loop statement. That is, if the loop condition does not have the False condition or the sequence is not completely recursive, the execution of the loop statement is also stopped.

The break statement is used in the while and for loops.

Continue

The continue statement jumps out of this loop, while the break jumps out of the entire loop.

The continue statement is used to tell Python to skip the remaining statement of the current loop and then continue the next loop.

The continue statement is used in the while and for loops.

Example: continue

1 #! /Usr/bin/env python 2 #! Coding: UTF-8 3 4 a = 9 5 while a: 6 if a % 2 = 0: 7 a-= 1 8 continue # if it is an even number, return the start of the loop 9 print a 10 else: 11 print "% d is odd number" % a # if it is an odd number, print it out 12 a-= 113

While... else

1 #! /usr/bin/env python2 #! coding:utf-83 4 count = 05 while count < 5:6       print count," is less than 5"7       count =count+18 else:9       print count," is not less than 5"

For... else

1 #! /Usr/bin/env python 2 #! Coding: UTF-8 3 4 # square 5 from math import sqrt 6 7 for n in range (99,1,-1): 8 root = sqrt (n) 9 if root = int (root): 10 print n11 break12 else: 13 print "Nothing."

 

 

 

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.