Introduction to how conditional selection and looping statements are used in Python

Source: Internet
Author: User
As in C, Java, Python also has conditional selection and looping statements, the style and C, Java is very similar, but there are some differences in writing and usage. Let's take a look at it today.
I. Conditional selection statements
The keywords for conditional selection statements in Python are: If, elif, else three. Its basic form is as follows:
Copy CodeThe code is as follows:


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


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


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


two. Looping statements
Like the C language, Python also 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, C #), while the loop usage is roughly similar to the C language.
The basic form of a For loop is as follows:
Copy CodeThe code is as follows:


For variable in list:
Block


For example, calculate from 1 plus to 100 and:
Copy CodeThe code is 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 a range (8) generates a list of [0,1,2,3,4,5,6,7].
Of course there can be nested loops, for example there is a list list=[' China ', ' England ', ' America ', to traverse the output of each letter.
Copy CodeThe code is as follows:


list=[' China ', ' 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 CodeThe code is 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 CodeThe code is as follows:


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


If you want to terminate a loop during a block of statements, you can use break or continue. Break is jumping out of the loop, and continue is jumping out of the loop.
Copy CodeThe code is as follows:


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


Copy CodeThe code is 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 that they are so much the same. If you are interested, you'd better start your own machine practice.
  • 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.