Second, the Python Foundation---cycle--condition judgment (While-for-if-elif-else)

Source: Internet
Author: User

!!! Statement!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1. code example: #if语句

#!/usr/bin/python3

#_ *_coding:utf-8_*_

num = 10

if num = = 10:

Print (' pair ')

Operation Result:

[email protected] ~]# python if.py

Right


2. Complex if statement #注意事项: Be sure to pay attention to the Chinese and English format, statement indentation, indentation for 4 spaces, remember!!!

Code instance: if statement

#!/usr/bin/python3

#_ *_coding:utf-8_*_

XGJ = ' HTUIDC '

if xgj = = ' HTUIDC ':

xuesheng={' QQQ ': ' 1001 ', ' wwww ': ' 1002 ', ' eee ': ' 1003 '}

Print (' Number of dictionary elements:%d '%len (Xuesheng) ')

Xuesheng.clear ()

Print (' Number of elements after dictionary deletion:%d '%len (Xuesheng))

Operation Result:

Number of dictionary elements: 3

Number of elements after dictionary deletion: 0


3.else Statement #我理解的else的意思是 Otherwise, if how what happens otherwise else what happens, else is not a standalone statement, only as part of the IF statement.

code example:

#!/usr/bin/python3

#_ *_coding:utf-8_*_

num = 10

If num > 10:

Print (' wrong ')

Else

Print (' other ')

Operation Result:

Other


A 4.elif statement #elif是else与if的简写, meaning the conditional else statement, which needs to be judged again.

code example:

#!/usr/bin/python3

#_ *_coding:utf-8_*_

num = 10

If num > 10:

Print (' wrong ')

elif 0 <= Num >=10:

Print (' In an interval ')

Else

Print (' other ')

Operation Result:

In an interval


Nested code blocks

code example:

#!/usr/bin/env Python3

#!/-*-coding-utf-8-*-

num = 10

If num%2==0:

If num%3==0:

Print (' The number you enter can be divisible by 2 and 3 ')

Elif num%4==0:

Print (' The number you enter can be divisible by 2 and 4 ')

Else

Print (' The number you enter can be divisible by 2, but not divisible by 3 and 4 ')

Else

If num%3==0:

Print (' The number you enter can be divisible by 3, but not divisible by 2 ')

Else

Print (' The number you entered cannot be divisible by 2 and 3 ')

Operation Result:

The number you enter can be divisible by 2, but not divisible by 3 and 4.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

is identity operator

>>> x=y=[1,2]

>>> z=[1,2]

>>> x==y

True

>>> x==z

True

>>> y==z

True

>>> x is y

True

>>> X is Z

False

# # #虽然值相同, but not in an object

>>> x is not Z

True




!!!!!! Cycle!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1.while Cycle

code example:

#!/usr/bin/python3

#!_*_coding:utf-8_*_

N=1

While n<=1000:

Print (' Current number is: ', n ')

N +=1

Operation Result:

The current number is: 1

The current number is: 2

The current number is: ...

The current number is: 999

The current number is: 1000


2.for Cycle

code example:

#!/usr/bin/python3

#!-*-coding:utf-8-*-

N=0 #进行赋值

Fields=[' A ', ' B ', ' C '] #进行赋值

While N<len (fields): #条件判断---If n is less than the length of the fields

Print (Fields[n]) #打印fields的0索引位置

N+=1 #加法赋值运算符 n=n+1

Operation Result:

A

B

C

code example:

htuidc=[' 1 ', ' 2 ', ' 3 '] #赋值

For DL in HTUIDC: #for in loop printing

Print (DL) #进行打印

Operation Result:

1

2

3

# # #注意事项: If you can use a for loop, try not to use the WHILE loop!!!



!!! Iterative TOOLS!!!!!

1. Parallel iterations

code example:

#!/usr/bin/python3

#!-*-coding:utf-8-*-

student1=[' qqq ', ' www ', ' eee ']

NUMBER=[1001,1002,1003]

For I in range (len (student1)):

Print (Number[i])


htu=[' d ', ' l ']

IDC=[1001,1002,1003]

For XGJ,DH in Zip (HTU,IDC):

Print (XGJ)


xgj=[' 1 ', ' 2 ']

DH=[1001,1002]

For HTU,IDC in Zip (XGJ,DH):

Print (IDC)


Operation Result:

1001

1002

1003

D

L

1001

1002


# # #注意: The ZIP function can act on any number of sequences.


!!!!!!! Jump out of the loop!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1.break statements are used in the while and for loops

code example:


#!/usr/bin/python3

#!/-*-coding:utf-8-*-

For letter in ' Hello ':

If letter = = ' L ': #遍历打印, know loop print to L bounce loop

Break

Print (' Current letters: ', letter)


num = 10

While num > 0:

Print (' Output number: ', num)

num-= 1

if num = = 8: #遍历打印输出, know equals 8 jumps out of the loop

Break

Operation Result:

The current letter is: H

The current letter is: E

Output numbers are: 10

Output numbers are: 9


2.continue #跳出当前的循环, then the next round of the loop----can only jump out once

code example:

#!/usr/bin/python3

#!/-*-coding:utf-8-*-

For letter in ' Hello ':

If letter = = ' L ': #遍历打印, know loop print to L bounce loop, then out L resume print loop

Continue

Print (' Current letters: ', letter)

Operation Result:

H

E

O


Else clause in a loop

1. Using else in while

code example:

num = 0

While num < 3:

Print (num, ' less than 3 ')

num = num + 1

Else

Print (num, ' greater than or equal to 3 ')

Print (' End Loop ')

Operation Result:

0 less than 3

1 less than 3

2 Less than 3

3 is greater than or equal to 3

End Loop

2. Using else in for

code example:

#!/usr/bin/python3

#!/-*-coding:utf-8-*-

names = [' Xiaomeng ', ' Xiaozhi ']

For name in Names:

If name = = ' Xiao ':

Print (' name: ', name)

Break

Print (' Circular Name list ' +name)

Else

Print (' No looping data ')

Print (' End Loop ')

Operation Result:

Circular Name List Xiaomeng

Circular Name List Xiaozhi

No cyclic data

End Loop


!!! PASS STATEMENT!!!!!!

Pass is an empty statement #就是什么都不做的语句, only a placeholder statement.

#!/usr/bin/python3

#!/-*-coding:utf-8-*-

XGJ = ' HTUIDC '

if xgj = = ' HTUIDC ':

Print (' pair ')

elif Xgj = = ' Idchtu ':

Pass # # #如果没有这个占位语句就会出错

Else

Print (' wrong ')

Operation Result:

Right

To be Continued ... This chapter is chapter II

Follow-up continue chapter III

Two, the Python foundation---loop--condition judgment (while-for-if-elif-else)

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.