python learning notes Day04

Posted by Fawkman on Wed, 05 Feb 2020 13:00:21 +0100

Article directory

Process control

Classification of python statements

Single line code: one line of python code

Code block: a group of codes composed of multiple lines of statements

Process control

  • Control of computer code execution

classification

1. Sequence structure

  • System default program structure, top-down execution

2. Branch structure / selection structure

Single branch
  • When the condition expression is true, the content in the code group is executed. If it is false, the content is not executed

  • General structure

if condition expression (boolean result):
	A piece of python code
	A piece of python code
	A piece of python code
	......

Sample code

if 55 > 13:
	print("This is right")
	print("This is right")
	print("This is right")

Operation result

This is right
This is right
This is right
Bidirectional branch
  • When the result of the conditional expression is true, the content of the code group after if is executed; if false, the content of the code group after else is executed
  • True interval: when the expression result is true, the executed region is the true interval, also known as if interval
  • False interval: when the expression result is false, the executed region is the true interval, also known as else interval
  • General structure
if condition expression (boolean result):
	A piece of python code
	A piece of python code
	A piece of python code
	......
else:
	A piece of python code
	A piece of python code
	A piece of python code
	......

Sample code

if 55 > 13:
	print("This is right")
	print("This is right")
	print("This is right")
else:
	print("This is wrong")
	print("This is wrong")
	print("This is wrong")

Operation result

This is right
This is right
This is right
Multiple branches
  • No matter how many branches there are, only one branch can be taken, and once a branch is taken, the structure of the branch ends
  • General structure
if condition expression (boolean result):
   A piece of python code
   A piece of python code
   A piece of python code
   A piece of python code
   ...... 
elif condition expression:
   A piece of python code
   A piece of python code
   ......
elif condition expression:
   A piece of python code
   A piece of python code
   ......
elif condition expression:
   A piece of python code
   A piece of python code
   ......
......
else:
   A piece of python code
   A piece of python code
   A piece of python code
   A piece of python code
   ......     

Sample code

week = 3
if week == 1: # Judge if today is Monday
    print('Today is Monday')
elif week == 2:
    print('Today is Tuesday')
elif week == 3:
    print('Today is Wednesday')
elif week == 4:
    print('Today is Thursday')
elif week == 5:
    print('Today is Friday')
elif week == 6:
    print('Today is Saturday')
else:
    print('Today is Sunday')

Operation result

Today is Wednesday
nested structure
  • Nesting of branches
if condition expression:
	Code statement
	if condition expression:
		Code statement
	else:
		Code statement
else:
	Code statement

Sample code

xiaoqu = True
loumen = True
jiamen = True


print("We got to the community")


if xiaoqu == True:#Check whether the cell door is open
    print("Cell door opening")
    if loumen == True:#Check whether the building door is open
        print("Building door opens")
        if jiamen == True:#Check whether the door is open
            print("Family door opens")
        else:
            print("Family gate")
    else:
        print("Lou men Guan")
else:
    print("Community gate") 

3. Circulation structure

while loop
  • Judgment before execution
Basic structure

General format

while conditional expression: execute if the result of conditional expression is true
	Code part
	Code part
	......
Structure with changes

General format

Declare var
 When enter the judgment of var:
	Code content
	Code content
	......
	The operation of variable var changing

Sample code

var = 0#initialize variable
while var <= 10:#Judgement condition
    print("Letter")#Cyclic code
    var += 1#The self increasing operation of variable
for... in cycle
  • Mainly used to traverse container class data
  • General structure
for custom variable in container class data:
	Code content, variable can be customized
	......

Be careful:
All data is traversed when the for is terminated
When traversing sets, tuples, lists, and strings, all data will be output one by one, and the dictionary will only output keys

#list
fruit = ['Apple','Durian','Cherry','Honey peach','Kiwifruit','watermelon']

for i in fruit:
	print(i)

Result:

Apple
 Durian
 Cherry
 Honey peach
 Kiwifruit
 watermelon
#Character string
fruit = 'Apple, durian, cherry, peach, kiwi, watermelon'

for i in fruit:
	print(i)

Result:

Apple
 fruit
 Durian
 lotus
 Sakura
 peach
 water
 honey
 peach
 Rhesus
 Monkey
 peach
 west
 melon
#tuple
fruit = ('Apple','Durian','Cherry','Honey peach','Kiwifruit','watermelon')
for i in fruit:
    print(i)

Result:

Apple
 Durian
 Cherry
 Honey peach
 Kiwifruit
 watermelon
#aggregate
fruit = {'Apple','Durian','Cherry','Honey peach','Kiwifruit','watermelon'}
for i in fruit:
    print(i)

Result:

watermelon
 Honey peach
 Cherry
 Durian
 Apple
 Kiwifruit

Dictionary of Chinese characters

#Directly traverse the dictionary, variables only get the key (directory)
fruit = {1:'Apple',2:'Durian',3:'Cherry',4:'Honey peach',5:'Kiwifruit',6:'watermelon'}
for i in fruit:
    print(i)

Result:

1
2
3
4
5
6 
#Special traversal dictionary value (data)
fruit = {1:'Apple',2:'Durian',3:'Cherry',4:'Honey peach',5:'Kiwifruit',6:'watermelon'}
for i in fruit.values():
	print(i)

Result:

Apple
 Durian
 Cherry
 Honey peach
 Kiwifruit
 watermelon
							
#Special traversal dictionary key (directory)
fruit = {1:'Apple',2:'Durian',3:'Cherry',4:'Honey peach',5:'Kiwifruit',6:'watermelon'}
for i in fruit.keys():
	print(i)

Result:

1
2
3
4
5
6
#Traversing keys and values at the same time
fruit = {1:'Apple',2:'Durian',3:'Cherry',4:'Honey peach',5:'Kiwifruit',6:'watermelon'}
for k,v in fruit.items():
print(k,v)

Result:

1 apples
 2 Durian
 3 cherry
 4 peach
 5 kiwi fruit
 6 watermelon 

4. Application of else statement

Branch

Connect with if

In circulation
Use else statement in while statement

General structure

while condition expression:
	Loop statement
	Loop statement
	...
else:
	Code statement
	Code statement
	...

Sample code

i = 0
while i <= 10:
	print("a",i)
	i += 1
else:
	print('end') 
In for... Use else statement in in statement

General structure

for variable in container:
	Cyclic code
	......
else: 
	Code statement

Sample code

fruit = {1:'Apple',2:'Durian',3:'Cherry',4:'Honey peach',5:'Kiwifruit',6:'watermelon'}
for i in fruit:
    print(i)
else:
    print("not fruit") 

5. Other process control statements

break statement
  • Mainly used in circular statements
  • Role: end cycle
    Sample code
#Output a number of 1-100, and calculate to 44 to stop the cycle
num = 1
while num <= 100:
    #Judge whether it is 44
    if num == 44:
        break 
    print(num)
    num += 1 
continue Statement
  • Mainly used in circular statements
  • Function: skip this cycle and start the next cycle
    Sample code
#Output 1-100 numbers without 4 numbers
num = 1
while num <= 100:
    if num % 10 == 4 or 40 <= num <= 49:
        num += 1 #Pay attention to add one statement, otherwise the program will run all the time
        continue
    print(num)
    num += 1 
pass statement
  • Placeholder to ensure syntax is correct

Sample code

if Ture:
	pass
else:
	print("A")
Published 4 original articles, praised 0, visited 78
Private letter follow

Topics: Python