Three basic structure statements in Python

Source: Internet
Author: User
Tags terminates

SELECT statement

If condition judgment: # conditions can be parentheses or without parentheses

......

Else

......

There is no switch statement in Python This can be used if Exp: .... elif Exp: to replace

1 if Judging condition 1: 2     EXECUTE statement 1 ... 3 elif Judging condition 2: 4     EXECUTE statement 2 ... 5 elif Judging condition 3: 6     EXECUTE statement 3 ... 7 Else : 8     Execute Statement 4 ...

Python Looping Statements
While loop Executes the loop body when the given judgment condition is true, otherwise exits the loop body.
For loop Repeating statements
Nested loops You can nest A for loop in the while loop body

loop control statements can change the order in which statements are executed. Python supports the following loop control statements:

Control Statements Description
Break statement Terminates the loop during statement block execution and jumps out of the loop
Continue statements Terminates the current loop during statement block execution, jumps out of the loop, and executes the next loop.
Pass Statement Pass is an empty statement in order to maintain the integrity of the program structure.

Looping with Else statements

In Python, for ... else means that the statement in for is no different from normal, while the statement in else is executed when the loop is executed normally (that is, for not breaking out by break), while ... else is the same.

1 count = 02 while count < 5:3    print" is less   than 5"4    count = Count + 15Else: 6    Print " Is isn't less than 5 "

A Python for loop can traverse any sequence of items, such as a list or a string.

Syntax 1:

The syntax format for the For loop is as follows:

For in sequence: statements(s)      

is very similar to the foreach statement in Java

Syntax 2:

Another way to perform a loop is through an index, as in the following example:

1Fruits = ['Banana','Apple','Mango']2  forIndexinchrange (len (fruits)):3    Print 'Current Fruit:', Fruits[index]4  5 Print "Good bye!"

Three basic structure statements in Python

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.