Introduction to conditional selection and cyclic statement usage in Python _python

Source: Internet
Author: User
Tags in python
Like C and Java, there are conditional selection and looping statements in Python, similar in style to C and Java, but there are some differences in writing and usage. Let's take a look at it today.
I. Conditional SELECTION statement
The key word for a conditional selection statement in Python is: If, elif, else three. The basic form is as follows:
Copy Code code as follows:

If condition:
Block
Elif Condition:
Block
...
Else
Block

Where the elif and else statement blocks are optional. The spoke statement executes only if condition is true for if and elif, and the Else branch is executed only if the IF and all elif condition are false. Note the difference between conditional selection statements in Python and C, where condition must be enclosed in parentheses, not in Python, but be aware that there is a colon behind the condition.
Here's an example of grading grades.
Copy Code code as follows:

Score=input ()
If score<60:
Print "D"
Elif score<80:
Print "C"
Elif score<90:
Print "B"
Else
Print "A"

Two. Circular statements
Like the C language, Python provides a for loop and a while loop (there is no do in Python). While loop) two kinds. But the for loop usage in Python is very different from the C language (similar to the for loop usage in Java and C #), while loop usage is roughly similar to the C language.
The basic form for a For loop is as follows:
Copy Code code as follows:

For variable in list:
Block

For example, calculate from 1 plus to 100 and:
Copy Code code as follows:

Sum=0
for Var in range (1,101):
Sum+=var
Print sum

Range () is a built-in function that can generate a list of numbers in a range. For example, range (1,6) generates a list of [1,2,3,4,5], and range (8) generates a list of [0,1,2,3,4,5,6,7].
Of course there can be nested loops, such as having a list of ' list=[', ' England ', ' America ', to traverse the output of each letter.
Copy Code code as follows:

list=[' ', ' England ', ' America '
For I in range (len (list)):
Word=list[i]
For j in range (Len (word)):
Print Word[j]

The built-in function len () can be used not only to calculate the length of a string, but also to find the number of members in a list or collection.
Here's a look at the basic form of the while loop:
Copy Code code as follows:

While condition:
Block

The loop is executed only if condition is true. Once the condition is false, the loop terminates.
As an example:
Copy Code code as follows:

count=2
While count>0:
Print "I love python!"
Count=count-1

If you want to terminate the loop in the statement block process, you can use break or continue. The break is out of the loop, and the continue is out of the loop.
Copy Code code as follows:

Count=5
While True:
Print "I love python!"
Count=count-1
If count==2:
Break

Copy Code code as follows:

Count=5
While count>0:
Count=count-1
If count==3:
Continue
Print "I love python!"

The basic usage of conditional statements and circular statements is so much more so. If you are interested, you'd better do your own practice on the machine.

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.