if statement:
grammar:
Single branch
if the condition is determined:
Statement block
Execution process: first execution determination condition, the following condition is satisfied when the judgment condition statement block is executed,
If the condition is not satisfied is not performed
Dual Branch
if the condition is determined:
Statement block 1
else:
Statement block 2
Execution process: first execution judgment condition statement is executed if the condition is satisfied block 1, if the condition is not satisfied, the execution
Statement 2.
Multi-Branch
if Condition 1 is determined:
Statement block 1
elif judgment condition 2:
Statement block 2
Analyzing conditions elif n:
Statement block n
else:
Statement block
Implementation process:
Performing a first determination condition, if the establishment of a statement block is executed, if not satisfied, the execution judgment statement 2, if the condition is satisfied,
2 statement block is executed, if not established, and so on, to determine if all of the conditions are not established, else the following statement block is executed.
Nested if statements
grammar:
if Condition 1 is determined:
if the judgment condition 2:
Statement block 2-1
else:
Statement block 2-2
else:
Statement block 1-2
Note: use nested when we match our statement through its indentation.
Input Age:
Enter the age of [0,160]. If this is not the age range, the age of the illegal print
Less than 16-year-old
16 to 24 year olds
25 to 34 year olds
35 to 44-year-old middle-aged
Aged 45 to 59 years old
60 years older
Enter the age of less than 18 Minors are not allowed to enter, otherwise the print welcome. . .
age = int (input ( "Please enter your age:"))
if age < 18:
print ( "Minors are not allowed to enter ...")
else:
print ( "Welcome to ...")
if age>=0 and age<=160:
if age < 16:
print ( "Youth")
elif age < 25:
print ( "Teenagers")
elif age < 35:
print ( "Youth")
elif age < 45:
print ( "middle-aged")
elif age < 60:
print ( "middle-aged")
else:
print ( "aged")
else:
print ( "Age illegal")
We can write our mandatory if statement in a row, has become a pseudo three head operations.
Analyzing conditions result1 if else result2
Implementation process:
First execution judgment condition, when the judgment condition is satisfied result1 is returned if the condition is not satisfied, return result2.
Head three operations: to complete a second election results.
num1 = int (input ( "Enter first data:"))
num2 = int (input ( "Please enter the second data:"))
print(num1 if num1>num2 else num2)
The cycle while:
The basic syntax of the while loop:
while judgment conditions:
Loop
Execution process: first execution determination condition, condition is satisfied, the process proceeds to the loop, the execution code over the loop body,
Loop execution ends, proceed to determine if the condition is satisfied, the loop continues, ad infinitum, until
When the condition is not satisfied, exit the loop
Nested loop:
while judgment conditions 1:
while judgment conditions 2:
Loop 2
Loop 1
Execution: a first execution condition determination, if condition 1 is satisfied, the body enters a loop, performing determination condition 2, when the condition 2 is satisfied,
The execution cycle 2, proceed to judgment conditions 2 until the condition 2 is not satisfied within exit the loop, the loop body is executed 1
Content, execute judgment conditions 1 and true, execute judgment conditions 1 2 ... until the condition is not satisfied then exit the loop.
The outer loop is executed once, after the inner loop completes execution, and then perform a second outer loop ...
Calculation and 1 + 2 + 3 + ... + 100
res = 0 # Statistics size
an initial value n = 1 #
while n<=100:
res + = n # n value is applied to the body res
n + = 1 # n is incremented by 1 to process
print("res=",res)
1x2x3x ... x100 product of
= 1
n = 1
while n<=100:
* = n
n += 1
print("ji=",ji)
Seek n!
n = 1x2x3..xn
It requires input from the console n
n = int (input ( "Enter a positive integer:"))
= 1
i = 1
while i<= n:
* = i
i += 1
print ( "% d factorial of% d"% (n, ji))
Factorial sum
1 = nothing! + 2! + 3! + ... + n!
n = int (input ( "Enter a positive integer:"))
res = 0
j = 1
while j <= n:
# Factorial
= 1
i = 1
while i <= j:
* = i
i += 1
res + =
j += 1
print("res=",res)
n = int (input ( "Enter a positive integer:"))
= 1
i = 1
res = 0
while i<= n: # i=1 i=2
* = i # = 1x1 = 1! x2 = 2!
i += 1
res + = # = 1! res = 1! +2!
print("res=",res)
Print star No.
Console input 1
1 1
2 console input
1 1
** 2 2
Input 3
1 1
** 2 2
*** 3 3
Input 4
*
**
Input 5
*
**
n = int (input ( "Enter a positive integer:"))
i = 1
while i <= n: number of lines printed #
j = 1
while j <= i: # Number of stars printed number
print("*",end="")
j += 1
print()
i += 1
while i<=n:
print("*"*i)
i += 1
The while loop infinite loop:
Enter your user name and password to determine whether the correct user name and password,
If it has been correctly entered until the correct date
Username: admin
Password: 123456
while True:
name = input ( "Please enter your user name:")
psd = input ( "Please enter your password:")
if name == “admin” and psd == “123456”:
print ( "right")
break
else:
print ( "Please re-enter ...")
continue
break contine and use the pass:
break: jump out of the loop body [it] in the loop circulation
continue: the end of the current cycle [continue following statement will not be executed], continue to the next is not out of the loop [body]
pass: to ensure semantic integrity of itself does not make any sense
Note: break and continue can only be used within a loop.
The demand for change:
After each lottery game, suggesting not to continue, if the show continues to yes, if no input is out of the game.
import random
while 1:
Jiang = random.randrange (1.7)
print (jiang)
ya = input ( "bet big or small bet? big / small")
if ya == "大" and jiang>3 or ya == "小" and jiang<4:
print ( "banker drink ...")
else:
print ( "first for the King ...")
ji = input ( "whether to continue the game? yes / no")
if ji == "yes":
continue # end of the current cycle, continue to the next
else:
break # out of the loop
The while loop else:
while judgment conditions:
Loop
else:
Statement block
When our normal execution cycle ends, it will execute our else statement following statement block,
If the loop body code execution ends not normal, this time it will not be executed else the following block.
[When using break out of the cycle, that is, abnormal end execution of state]
If the while loop only when a statement, we can while loop with the following statements mandatory written on one line
while True: pass
1+2+..+100
n = 1
res = 0
while n<=100:
n = no +
n += 1
print(n)
if n>50:
continue
else:
print ( "loop end")
print(res)
for loop: Wuxi which hospital gynecological good https://www.bhnfk.com/
for loop syntax:
for variable in sequence:
Loop
The elements in the sequence are sequentially taken and assigned to the variable, until all elements of the sequence elements taken until the cycle is completed.
range([start,]stop[,step])
start: specified, the default 0
stop: End value must be written
step: step 1 by default
Functions: may generate a [start, stop) sequence
list1 = ["hello","hahha","hehe","heihei"]
for x in list1: # for loop iterates through the list
print(x)
1 + 2 + 3 + ... + 10 # 1 is calculated and + 10 ....
list2 = [1,2,3,4,5,6,7,8,9,10]
res = 0
for i in list2:
res += i
print(res)
print list (list (range (1,101))) # 1 and 100
res2 = 0
for j in range (100,0, -1): # 1 + .... + 100 and
print("j=",j)
res2 += j
print(res2)
For loop factorial of n -
n = int (input ( "Enter a positive integer:"))
= 1
for x in range(1,n+1):
* = x
print ( "% d factorial of% d"% (n, ji))
Nested loop:
1 for i in the sequence:
Loop 1
for j in sequence 2:
Loop 2
Implementation process:
First into the outer loop, the outer loop is executed once, into the circulation,
[Executive] round the inner loop execution ends, exit the inner loop, perform a second outer loop,
Again into the circulation, ad infinitum, until the outer loop exits, the entire cycle until the end
Factorial sum
method one:
1 = nothing! +2! +3! + ... + n!
n = int (input ( "Enter a positive integer:"))
res = 0
for i in range(1,n+1):
= 1
for x in range(1, i + 1):
* = x
res + =
print("res =",res)
Method Two:
n = int (input ( "Enter a positive integer:"))
res = 0
= 1
for x in range(1,n+1):
* = 1, x = 1! x = 2 = 1x2 = 2!
res + = # 1! +2! + ...
print("res =",res)
The for loop else:
for variable in sequence:
Loop
else:
Statement block
Execution: execution cycle, when the cycle is successfully completed, else the following block of statements is executed, if the execution end abnormal
else following block of statements are not executed.
for x in range(10):
if x>50:
break
else:
print ( "loop end")
Use a for loop to print the multiplication table
Two cycles:
1. The outer-loop control line
2. The inner loop control column
for i in range(1,10):
for j in range(1,i+1):
print("%dx%d=%d"%(j,i,j*i),end="\t")
print()
It can act on the basic data types for loop are
Strings, lists, tuples, dictionaries, set, Range ()