Python Learning (iii) Process Control

Source: Internet
Author: User
Tags switch case

Python Process Control

This chapter describes Python's underlying process control. Includes if condition statement, for Loop and while Loop statement, continue and break usage, and so on. The basic usage is similar to C and Java, without a switch statement.

If statement

The following example is an example of if (...) The parentheses are an expression, and when True (that is), execute the corresponding code snippet, noting that all conditional statements must be preceded by a colon:

1 " "determines the number of digits entered by the user (regardless of user input non-integer)2     for zero output It 's zero.3 Single digit output It ' s a number below4 two-digit output It s a two-digit number5 three-digit output It's digit more than two6 " "7User_input = input ("Please input a number:")8n = Int (user_input)#the type of user input is a string, converted to an integer type9 if(n==0):Ten     Print("It ' s zero.") One elif(n<10): A     Print("It ' s a number below") - elif(n<100): -     Print("It ' s a two-digit number") the Else: -     Print("It's digit is more than")

There may be 0 to more elif sections, and else is optional. The keyword "elif" is the abbreviation for "else if", which can effectively avoid too deep indentation. If ... elif elif ... Sequences are substituted for switch case statements in other languages.

For statement

The For statement in Python differs from C Pascal. The usual loops may be used by the user to define iteration steps and abort conditions (such as C), and the Python for statement iterates over the subkeys in any sequence (linked list or string), in the order in which they are in the sequence.

1 #For Statement instance2String ="python"3  forIinchstring:4     Print(i)#string Processing5List = ["L","I","s","T"]6  forIinchlist:7     Print(i)#List Processing8  forIinchRange (5):9     Print(i)#the range () function, range (5) represents 0 to 4

The example above only senses the wording of the For statement, which is detailed in the subsequent sections of the data structure.

While statement

while (...) if the expression is True in parentheses, the code snippet continues to execute, or false to stop execution

1 # While statement instance 2 i=13while (i<10):4     print(i)5      i + = 1

Be aware of the infinite loops caused by the error code, such as:

1 i=12while (i>1):3     i + = 14      Print(i)

Break, continue statement

Python Learning (iii) Process Control

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.