Control statement : The order , choice , loop
Selection structure ( Conditional judgment structure ):
By judging whether the condition is true , To decide which branch to take , Divided into single branches , Double branch , Multiple branches
Single branch selection structure :
If Conditional expression : # Logical expression , Relationship expression , Arithmetic expressions
sentence / Sentence block
Two branch selection structure :
If Conditional expression :
sentence / Sentence block
else:
sentence / Sentence block
Ternary conditional operators :
Grammar format : Value when condition is true if( Conditional expression )else The value when the condition is false
Num=input(‘ Please enter a number :’)
print(Num if int(Num)<10 else ‘ The number is too big ’)
Multi branch selection structure :
If Conditional expression 1:
sentence 1/ Sentence block 1
elif Conditional expression 2:
sentence 2/ Sentence block 2
.........
elif Conditional expression n:
sentence n/ Sentence block n
[else:
sentence n+1/ Sentence block n+1]
Select nesting of structure :
The selection structure can be nested , Note the amount of indentation between different code blocks
While loop :
While Conditional expression :
Loop body statement
num=0
sum=0
while num<=100: # Calculation 1~100 And
sum+=num
num+=1
print(sum)
for Loop and iteratable object traversal :
for Loops are usually useful for iterating over iteratible objects :
for Variable in Iteratable object :
Iteratable object
for i in range(5):
print(i)
Ergodic dictionary :
a = {'name': 'gaoqi', 'age': 18, 'job': 'student'}
for i in a:
for i in a.keys():
for i in a.values():
break sentence :
stay while and for Use in loop break sentence , To end the cycle , When there are nested loops , You can only jump out of the loop of the nearest layer
continue sentence :
continue Used to end this cycle , Go on for the next time , When multiple loops are nested , Apply to the most recent loop
else sentence :
One can be attached to the loop else sentence ( Optional ), If the loop statement is not break end , Will perform else sentence , Otherwise, do not execute
Loop code optimization :
Minimize unnecessary calculations inside the loop
Nested loop , Minimize the calculation of inner circulation
Local variable query is faster , Try to use local variables
send zip() Parallel iteration :
We can go through zip() Function iterates over multiple functions in parallel , Iterations are stopped when the shortest sequence runs out
name = ('1', '2', '3', '4')
age = ('1', '2', '3', '4')
job = ('1', '2', '3')
for names, ages, jobs in zip(name, age, job):
print('{0}--{1}--{2}'.format(names, ages, jobs))
for i in range(3):
print('{0}--{1}--{2}'.format(name[i], age[i], job[i]))
Inferentially create sequences :
List objects are generated by list derivation :
[ expression for item in Iteratable object if conditional ]
# You can use two loops , Use zip Parallel iteration
cells=[(row,col)for row,col in zip(range(1,10),range(101,110))]
for cell in cells:
print(cell)
Dictionary derivation :
{key:value for expression in Iteratable object }
citys = [' Beijing ', ' Shanghai ', ' Shenzhen ', ' Guangzhou ']
s = {id * 100: city for id, city in zip(range(1, 5), citys)}
print(s)
Set derivation :
{ expression for item in Iteratable object if conditional }
Generator derivation ( Do not generate tuples directly ):
A generator can only run once , The first iteration can get the data , The second time the data is gone
( expression for item in Iteratable object if conditional )
Draw concentric circles of different colors :
import turtle
p = turtle.Pen()
radius = [x * 10 for x in range(1, 11)]
p.width(4)
my_color = ('red', 'yellow', 'blue', 'black')
for r, i in zip(radius, range(len(radius))):
p.penup()
p.goto(0, -r)
p.pendown()
p.color(my_color[i % len(my_color)])
p.circle(r)
turtle.done()
author :small_monsters
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .