Getting started with Python--4--and loops

Source: Internet
Author: User

1, the use of elif comparison of the province CPU:

The first method, using the IF

score = Int (input (' Please enter your score: '))

if (score <=) and (Score >= 90):
Print (' A ')
if (score <=) and (Score >= 80):
Print ("B")
if (score <=) and (score >= 70):
Print ("C")
if (score <=) and (score >= 60):
Print ("D")
if (score <=) or (score >=101):
Print ("Input Error! ")

The second method, using Elif

score = Int (input (' Please enter your score: '))

if (score <=) and (Score >= 90):
Print (' A ')
Elif (Score <=) and (Score >= 80):
Print ("B")
Elif (Score <=) and (score >= 70):
Print ("C")
Elif (Score <=) and (score >= 60):
Print ("D")
else:
Print ("Input Error! ")

Use Elif to save CPU more than if

2, assert: When the condition behind this keyword is false, the program automatically crashes and throws Assertionerror exception

In order to prevent the programmer from finding the following exception, so in front of the judgment when the problem of the first stop with him

Later it will say: How to capture this anomaly and handle

3, a small trick:

1, how to let X, Y, Z This three value fast interchange

X, y, z = y,z,x

2. Membership Operator: in

Xname= "Beijing Tian ' an gate"
' Door ' in XName #返回TRUE

4, ternary operator: if use method

  

X, y, z = 6, 5, 4 #这个程序可以变为:

If x < Y:small = x if (x < y and x < Z) Else (y if y < Z else z)

small = X
If z < small:
small = Z
Elif y < z:
small = y
Else
small = Z

5. While

Usage: While condition:

The loop body #直到条件为假 Otherwise has been circulating like condition 1 = 1 This is the endless loop

6. For

Usage: for-target in expression:

Loop body

Give me a chestnut.

name = ' Xiaowangba '

For I in Name:

Print I

Give me another chestnut.

name = [' Xiaowangba ', ' 123 ', ' 456 ', ' We are Coming ']
For each in name:
Print (Each,len (each))

7, Range (): Built-in function BIM generates a sequence of numbers that can be used with the for

Range (5) #生成从0到4这五个数

Range (1,10) #生成从0到10这是个数

Range (1,10,2) #范围从0到10 interval is 2

8, break: Can terminate the current cycle, no longer cycle down, immediately jump out of the loop, execute the next statement outside the loop

9, continue: Terminate the cycle, and start the next round cycle

Getting started with Python--4--and loops

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.