Python Basics (3)--Program structure

Source: Internet
Author: User

Python, like other programming languages, also has three program structures. Sequential structure, select structure, loop structure.

1. Sequential structure

The sequential structure executes the program in sequence and does not explain too much.

2. Select structure

2.1 If statement

If condition:

Expression

Example:

[Email protected]]# cat if.py

#!/usr/bin/env python

If 3 < 5:

Print "3 less than 5" # statement block inside can be multiple statements if 3 > 4:

Print "3 greate than 4"

[Email protected] python]#./if.py

3 Less than 5

2.2else clause

If condition:

Expression

Else

Expression

An If statement can also nest nested If.python that allow the statement to be nested. The ELSE clause is always the last branch of the IF statement. Cannot appear in front of the ELIF clause.

2.3 elif Sentence

If condition:

Expresssion

Elif Condition:

Expression

# there can be more than one elif clause, but once one of the branches is not executed, it will not go down to match execution.

There are no switch statements in Python. You can only simulate switch with elif.

2.4 Logical value (BOOL)

Used to denote concepts such as: Right and wrong, true and false, empty and non-empty

The logical value consists of two values:

True: indicates non-empty amount (e.g. string,tuple,list,set,dictonary, etc all non-0)

False: Table shows 0,none, empty amount, etc.

Function: Mainly used in judging statements, used to judge

Whether a string is empty

Whether the result of an operation is zero

Whether an expression is available

>>> if True:

... print "OK"

...

Ok

>>> if False:

... print "OK"

...

>>> if 1:

... print "OK"

...

Ok

>>> If 0:

... print "OK"

...

>>>

Example 1:

#!/usr/bin/python

def fun ():

Return 1

If Fun ():

print "OK"

Else:print "No"

No other unrelated code can appear in between if and else

Example 2:


#!/usr/bin/python

X=int (raw_input ("Please input x:"))

Y=int (raw_input ("Please input y:"))

If x>=90: multiple criteria to judge

If y>=90:

Print "A"

Elif x>=80:

Print "B"

Elif x>=70:

Print "C"

Else

Print "No"

Example 3: Logical structure and, or, not

#!/usr/bin/python

X=int (raw_input ("Please input x:"))

Y=int (raw_input ("Please input y:"))

If x>=90 and Y>=90:print "A"

Elif x>=80:

Print "B"

Elif x>=70:

Print "C"

Else

Print "No"


3. Cyclic structure

3.1while Cycle

While loop

#!/usr/bin/python

While True:

print "Hello" #死循环

The while loop must have conditions

#!/usr/bin/python

While True:

print "Hello"

x = raw_input ("Please input Q for quit")

if x = = "Q":

Break

Or

#!/usr/bin/python

Loop ends when x=q

The exit () function can jump out of the entire program x = ""

While x! = "Q":

print "Hello"

x = raw_input ("Please input Q for quit")

Or

#!/usr/bin/python

x = ""

While x! = "Q":

print "Hello"

x = raw_input ("Please input Q for quit")

If not x: Direct Enter exit

Break

#!/usr/bin/python

x = ""

While x! = "Q":

print "Hello"

x = raw_input ("Please input Q for quit")

If not x: Direct Enter exit

Break

if x = = "C": enter C to continue looping

Continue

print "Hello World" if none of the two times is satisfied with print

Else

Print "Ending ..."

While condition judgment failed execution else if it is break do not do else

3.2for Cycle

For loop

For x in [1,2,3,4]

Print X


Iteration Sequence Index (index)

For x in range (100) default starting from 0

Print X


For x in range (1,11) does not contain the last value 11

Print X


For x in range (1,11,2) 2 is a stepping value and does not refer to the default of 1

Print X


#!/usr/bin/python

Num=0

For x in range (1,101)

num + = X

Print num


Traversing a sequence

S= "Willis"

For x in range (len (s))

Print S[x]


l=[1,2,3, ' A ', ' B ']

For X in L:

If x>=2:

Print X


D={1:111,2:222,3:333}for x in D:

Print D[x]


For k,v in D.items ():

Print K

Print V


For x in range (1,11):

Print X

If x==2:

Pass #代码桩, stance

If x==3:

print "Hello"

Continue

If x==6:

#continue skips the remaining statements of the second loop into the next loop

Break

#break End this cycle

Print "#" *50

Else

print "Ending"

This article from the "Technology life, Simple not simple" blog, please be sure to keep this source http://willis.blog.51cto.com/11907152/1854703

Python Basics (3)--Program structure

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.