Python syntax base 03 (if statement, while loop and for loop)

if statement:
Syntax:
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
the else:
statement block 2
during the execution of: performing first determination condition, if the condition is satisfied is executed statement block 1, if the condition is not satisfied, executes
the statement 2.

Multibranched
if determination condition 1:
a block of statements 1
elif determination condition 2:
a block of statements 2
elif determination condition n:
statement block n-
the else:
statement block
execution process:
first execution determination condition 1, if the establishment of the execution statement block 1, if established, judgment statement 2 is executed, if the condition is satisfied,
the execution statement block 2, if established, and so, if all the determination conditions are not established, else the following block of statements is executed.

Nested if statement
syntax:
if the determination condition. 1:
if the judgment condition 2:
a block of statements 2-1
the else:
statement block 2-2
the else:
a block of statements 1-2

Note: use nested when we match our statement through its indentation.

Input Age:
Enter the age [0,160], if not in this age-range, the illegal printing of age
less than 16-year-old
16 to 24 year olds
25 to 34 year olds
35 to 44-year-old middle-aged
45 to 59 years old age
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 ...")
the else:
Print ( "Welcome to ...")

) . 1 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 10 . 11 12 is 13 is 14 15 16 . 17 18 is . 19 20 is 21 is that we can write our mandatory if statement in line, the dummy three head became operational. result1 if the judgment condition 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 (IF num1 num1> num2 the else num2)

The while loop:
the basic syntax of the while loop:

while judgment conditions:
loop

Implementation process: first execution judgment condition, condition is satisfied, then enter the body of the loop, executing code again body of the loop, the
loop execution ends, proceed to determine if the condition is satisfied, to continue the loop, ad infinitum, until the
condition is not satisfied when exit the loop

Nested loop:
the while judgment conditions 1:
the while judgment conditions 2:
loop 2
loop 1

Implementation process: first execution judgment condition 1, if condition 1 is fulfilled, then enter the body of the loop, a determining condition 2. If condition 2 is satisfied,
the execution cycle 2, proceed to judgment conditions 2 until the condition 2 does not hold up, within the loop exits , the execution cycle 1
of the content, a determining condition 1, the condition is satisfied, a determining condition 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 # Statistical size
n = 1 # initial value of
the while n <= 100:
res # n value is applied to n = + body res
n + = 1 # n is incremented by 1 to process

print("res=",res)

1x2x3x ... x100 product of
JI. 1 =
n-=. 1
the while n-<= 100:
JI * = n-
n-+ =. 1
Print ( "JI =", JI)

 

! Seek n
n = 1x2x3..xn
requires input from the console n
n = int (input ( "Enter a positive integer:"))
JI. 1 =
I =. 1
the while I <= n:
JI = I *
I + = . 1
Print ( "% D factorial of% D"% (n-, JI))
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
factorial of with

1 = nothing! + 2! + 3! + ... + n!

n = int (input ( "Enter a positive integer:"))
RES = 0
J =. 1
the while J <= n-:
# factorial
JI =. 1
I =. 1
the while I <= J:
JI * = I
I + = . 1
RES = JI +
J + =. 1
Print ( "RES =", RES)

n = int (input ( "Enter a positive integer:"))
JI. 1 =
I =. 1
RES = 0
the while I <= n-: # I = I = 2. 1
JI * 1x1-JI = = = I #ji. 1! 2 = X2!
I +. 1 =
RES + = JI = #res. 1! = RES. 1! +2!
Print ( "RES =", RES)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
Print No. stars
console input 1

11
console input 2
11
** 22
Input 3
11
. 1
** 22
*** 33
input 4
*
**

Enter the 5
*
**

n = int (input ( "Enter a positive integer:"))
I =. 1
the while I <= n-: # number of lines printed
J =. 1
the while J <= I: # Number of stars printed number of
print ( "* ", End =" ")
J + =. 1
Print ()
I =. 1 +

I the while <= n-:
Print ( "*" I *)
I = +. 1
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
the while loop of an endless loop:

Enter your user name and password to determine whether the correct user name and password,
if correct, has been entered until the correct date
username: admin
password: 123456

True the while:
name = the INPUT ( "Please enter your user name:")
PSD = the INPUT ( "Please enter your password:")
IF name == "ADMIN" and PSD == "123456":
Print ( "right" )
BREAK
the else:
Print ( "Please re-enter ...")
the 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 itself is not 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.

Random Import
the while 1:
jiang random.randrange = (1, 7)
Print (jiang)
Ya = the INPUT ( "? bet big or small bet big / small")
IF Ya == "big" and jiang> 3 or ya == "small" and jiang <4:
Print ( "banker drink ...")
the else:
Print ( "first for the King ...")
JI = the INPUT ( "? whether to continue the game yes / NO")
iF JI == "Yes":
continue the current cycle # end, continue to the next time
the else:
BREAK # out of the loop
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
the while loop of the else:

while judgment conditions:
loop
else:
a block of statements
when we performed the loop ended normally, it is executed to our else statement following statement block,
the end of the loop body if not normal code execution, it does not execute this time else 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

+ + 2 + ... 1 100
n-=. 1
RES = 0
the while n-<= 100:
RES = + n-
n-+ =. 1
Print (n-)
IF n-> 50:
Continue
the else:
Print ( "loop end")
Print (RES )
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
for loop:
for loop syntax:
for the variables in sequence:
the loop
sequence in the sequence the elements removed and assigned to the variable, until the sequence elements of all the elements taken up , the loop ends.

Range ([Start,] STOP [, STEP])
Start: start specified, the default 0
STOP: End Value must be written
STEP: step 1 Default
functions: it may generate a [start, stop) sequence

= List1 [ "Hello", "hahha", "Hehe", "heihei"]
for X in List1: # for loop through the list
print (x)

1 + 2 + 3 + ... # 10 + .... + 1 and the calculation 10
List2 = [1,2,3,4,5,6,7,8,9,10]
RES = 0
for I List2 in:
RES = I +
Print (RES)


print (list (range (1,101) )) # 1 to 100 listing
RES2 = 0
for J in Range (100,0, -1): and # 1 + .... + 100
print ( "j =", J)
RES2 = J +
Print (RES2)

For loop find the factorial of n
n = int (input ( "Enter a positive integer:"))
JI =. 1
for X in Range (. 1, n +. 1):
JI * = X
Print ( "% D factorial is D% "% (n-, JI))
1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
nested loop:
for I 1 in the sequence:
loop 1
for j in the sequence 2:
loop 2

Implementation process:
first into the outer loop, the outer loop is executed once, into the loop,
the inner loop [Executive] round to finish, and exit the inner loop, perform a second outer loop,
re-enter the circulation, ad infinitum, until the outer loop exit, the entire cycle until the end

And the factorial
method:
RES =. 1 +2 +3 + ... + n-!!!!

n = int (input ( "Enter a positive integer:"))
RES = 0
for I in Range (. 1, n-+. 1):
JI =. 1
for X in Range (. 1, I +. 1):
JI * = X
JI + = RES
Print ( "RES =", RES)

 

Method two:
n-int = (INPUT ( "Please enter a positive integer:"))
RES = 0
JI =. 1
for X in Range (. 1,. 1 n-+):
JI = X * = #x is JI. 1. 1 = X! JI = 1x2 = 2 = 2!
RES # + = JI. 1! +2! + ...
Print ( "RES =", RES)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
for the loop else:
for the variables in sequence:
loop
else:
statement block

Execution: execution cycle, if the cycle ends normally performed, else the following block of statements is executed, if the execution end abnormally
else the following block of statements are not executed.

Range in X for (10):
IF X> 50:
BREAK
the else:
Print ( "loop end")
. 1
2
. 3
. 4
. 5
using the multiplication table for printing cycle
two cycles:
1. an outer loop control line
within the loop 2 control column

I in Range for (1,10):
for J in Range (. 1,. 1 + I):
Print ( "% D% =% D DX"% (J, I, I * J), End = "\ T" )
Print (http://www.my516.com)
. 1
2
. 3
. 4
basic types of data can be applied to the for loop are
strings, lists, tuples, dictionaries, set, Range ()
-------- -------------

Guess you like

Origin www.cnblogs.com/ly570/p/11106223.html