Analyzing a .if
determining if the action:
The computer is good or bad things, the feasibility of a genuine right or wrong judgment, etc., in order to make different treatment.
determining if the basic format:
if Condition:
block 1
block 2
block 3
....
if statement executes the basic situation:
The condition is first determine if the condition is met if the subordinate block of code will be executed, if the match is not performed. In the following example: Advanced condition judging, when the conditions are met, block of code will be 1,2,3. If the condition does not meet the code blocks will be 4.
eg:
if Condition: block 1 block 2 block 3 code blocks 4
ps:. 1 indention in python with the home code, the same code is called code blocks indented
2. The same level of code that run from top to bottom
determining if an extension:
1.if....else
if Condition: block 1 block 2 block. 3 the else : block 4 block 5 block 6
ps:. 1 when performed only if and else where a combination of
2.else not be used alone, else, and only if, while, for use with
2.if ..... elif ..... elif .... (there can be any number of elif)
if Condition: block 1 block 2 block 3 elif Condition: block 1 block 2 block 3
And wherein if the same elif indent code determination performed sequentially from top to bottom, and will only perform one of them
3.if....elif.....else
= Gender ' FEMALE ' Age = 34 is is_beautiful = True IF Gender == ' FEMALE ' and Age> 18 is and Age <30 and is_beautiful: Print ( ' can add a micro-channel ah, I favored you ' ) elif Gender == ' FEMALE ' and is_beautiful: Print ( ' think about it ' ) the else : Print ( ' What the hell ' )
In this case if .... elif ..... else is so judged at the same level from top to bottom and will only execute one of them
4.if nesting
Supplementary: 1 when making judgments, 0 ',' [] ',' {} ',' ', None corresponding bool value is False
2. The variable name can also be determined, as a value corresponding to the variable name is True or False point
3. Analyzing Boolean value can be used directly
two. while loop
while the basic syntax:
while conditions: block 1 block 2 ......
while statement execution order: first the judgment of the condition in the while, when the conditions are met will be sequentially executed while a subordinate block, while when the condition after the execution of the subordinate block will again be determined while, execution and so forth, until the conditions are not meet the program terminates.
while the expansion of the use of:
1.while....break
while conditions: block 1 block 2 ...... BREAK
break: break when used in conjunction with and while acting as a full termination cycle (while for the break belongs)
2.while....continue
while conditions: block 1 block 2 ...... Continue
continue: while conjunction and continue, continue to the end of the role of loop layer, behind the loop continues.
3.while.....else
. 1 = n- the while n-<. 5 : IF n-==. 3 : BREAK Print (n-) n- + =. 1 the else : Print ( ' the while loop has been completed normally ' )
ps: when a while and when used in conjunction else, else while normally been executed when the middle is not terminated by a break, then the latter will be executed
supplement:
1. Define a global flag to end the program by changing the flag
= Tag True the while Tag: block 1 block 2 ... the while Tag: block 1 block 2 the while Tag: block 1 ...
By defining a flag tag = True, in the late obtain tag = false by changing the program to end
2. Do not write an infinite loop in the program
while True: 1+2+2
Because the conditions while always true, the program has been executed without error and continue
three. for loop
The basic syntax for loop structure:
for the variable name in the type of container: for the loop body code. 1, for the loop body code 2, ...
for loop values for:
1. The value from the list
l = [1, 2, 3, 4, 5] for a in l: print(a)
Wherein l is the one element in the assignment to a, then the output a.
2. The value from the dictionary
d = {'name':'zhang','password':'123','hobby':[1,2,3,4]} for i in d: print(d)
print(d[i])
ps: we are exposed to the dictionary can operate only key, so the output is the key, If you need to know is how much the final output value shall be changed print (d [i])
The for loop does not depend on the index value, and the while loop an index value dependence.
3. The range of values (range)
for a in range(1,10): print(a)
ps:. 1 at the range value in the range of care regardless of the end to take, i.e., a can 10, but fail to.
2. python2 python3 and the range () are different. In python2 the range reflects the form of a list of all values, accounting for a larger memory, python3 is an iterator, small footprint, high efficiency (only when you need to you). Function and have the same range xrange python3 in the python2 in, but there is a range.
python2 and 3 are different: range (xrange), input (raw_input)
4.for .... break for .... continue for ... else and while loops function is the same.
5.for nested loop
eg: multiplication tables
for I in Range (1,10 ): for J in Range (. 1,. 1 + I): # Range condition memory cycle is determined according to outer loop Print ( ' %% S = S * S% ' % (I , J, I * J), End = ' ' ) Print ()
Today's job:
1. restrict user login attempts error
use_info = { 'name':'zhang','pwd':'123' } a = 1 while a < 4: name = input('please input your name>>>:') pwd = input('please input your pwd>>>:') if name == use_info['name'] and pwd == use_info['pwd']: print( ' Thank flop ' ) BREAK A +. 1 =
2. The user three attempts not to end the program directly after three attempts user input after not prompt the user whether to continue to try, if you give three chances to enter y, if n directly enter the program ends
use_info = { 'name':'zhang','pwd':'123' } a = 1 while a < 4: name = input('please input your name>>>:') pwd = input('please input your pwd>>>:') if name == use_info['name'] and pwd == use_info['pwd']: Print ( ' Thank you flop ' ) BREAK the else : A + = 1 IF A == 4 : Print ( ' if you want to continue Login Please enter "y", leave to enter "the n-" ' ) the SELECT = the INPUT ( ' Please the INPUT "Y" or "n-" >>>: ' ) IF SELECT == ' Y ' : A =. 1 the else : BREAK
use_info = { 'name':'zhang','pwd':'123' } a = 1 while a < 4: name = input('please input your name>>>:') pwd = input('please input your pwd>>>:') if name == use_info['name'] and pwd == use_info['pwd']: print('谢谢翻牌') while True: cmd = input('please input cmd>>>:') if not cmd:continue if cmd == 'q':break else:print('run <%s>' %cmd) break else: a += 1 if a == 4: print('If you want to continue Login Please enter "y", leave to enter "the n-" ' ) the SELECT = the INPUT ( ' Please the INPUT "the y-" or "the n-" >>>: ' ) IF the SELECT == ' the y- ' : A = 1