程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Getting started with Python -- control statements

編輯:Python

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved