Derivation Iterative Parallel Condition

>>> Print ( ' name ' ) # only a parameter 
name
 >>> Print ( ' name ' , ' Age ' ) # space splicing 
name Age
 >>> Print ( ' Hi ' + ' , ' , ' Tom ' ) # want a value no spaces separated by 
hi, tom
 >>> Print ( ' name ' , ' Age ' ,'home' , On Sep = ' _ ' ) # specify the delimiter 
name_age_home
 >>> Print ( ' name ' , ' IS ' , ' A ' , ' Good ' , ' Thing ' , End = ' ! ' ) # Specify end symbol 
name IS Good Thing A!


 >>> X, Y, Z = l, 2,3 # batch assignment 
>>> X, Y = Y, X # exchange value, do not think to x = y, y = x are operating 
>> > x
2
>>> y
. 1 
>>> Z
 . 3 
>>> 

sequence unpacking (or iterables unpack): one sequence (or any iterables) unpacked, and the obtained value is stored into a series of variables. To unpack the number of elements of the sequence must contain the same number of goals you listed on the left side of the equal sign, otherwise Python will raise an exception. Examples will be explained below with:
 >>> VS = l, 2,3 
>>> X, Y, Z = VS # unpacked 
>>> X
 . 1 
>>> Y
 2 
>>> Z
 . 3 

may use an asterisk operator ( * ) to collect excess value so not necessary to ensure the same value and the number of variables, as shown in the following example:
 >>> Q, W, E * = [1,2,3,4 ]
 >>> E 
[ . 3 ,. 4 ]
 >>> Q, W *, E = [1,2,3,4] # put to rest position 
>>> W 
[ 2,. 3 ]
 >>>4 

block: at the beginning of the colon, the same indentation, the indentation is not the same, the code block. 

Age = int (the INPUT ( ' Please enter your age: ' ))
 if Age> 81 :
 Print ( ' old ' )
 elif Age> 18: # the else and if the merger 'elif' 
Print ( ' youth ' )
 the else :
 Print ( ' minor ' ) 


Print ( " grown up "  IF Age> 18 is the else  ' minor ' ) # trinocular operation

 
>>> x = y = [1,Z = >>> [1,2 ]
 >>> X is Y 
True
 >>> X == Z # summary, == used to check whether the two objects are equal, and is used to check whether the two objects are the same (YES the same object). 
True
 >>> X is Z # because is checked whether the two objects are the same (not equal). Variables x and y refer to the same list, and the z pointing to another list (which contains the value and the order of these values are the same as the previous list). Although the two lists are equal, but not the same object. 
False 
strings are compared according to the alphabetical order of the characters. Order to learn the value of the letter, the function may be used ord. The role of this function is a function chr opposite. 

Age = 100 >>> 
>>> Assert Age <100 # assertion, inspection conditions. If not met, immediately error 
Traceback (MOST recent Results Last Call): 
File " <pyshell # 6> " , Line 1, in <Module>
 the Assert Age <
Age = 100 >>> 
>>> Assert Age> 90, ' must be greater than 90 ' # prompt given when 

 

X = 10
 the while X <100 :
 Print (X) 
X +. 1 = 


names = [ ' QQ ' , ' WW ' , ' EE ' , ' RRR ' ]
 for name in names:
 Print (name)

 >>> Range (. 5) # Range (. 5) for the range (0,5) shorthand 
Range (0,. 5 )
 >>> a = range (0,5) # chaui
>>> list(a)
[0, 1, 2, 3, 4]


>>> abs=['1','b','c']
>>> qwe=[1,2,3]
>>> a=zip(abs,qwe)#并行迭代
>>> a
<zip object at 0x036F3580>
>>> list(a)
[('1', 1), ('b', 2), ('c', 3)]


for index, string in enumerate(strings):This function allows you to iteration index  value pairs, where the index is automatically provided.#
if 'xxx' in string: 
strings[index] = '[censored]'


#列表推导
>>> [x*x for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

[(x,y) for x in range(10) for y in range(11) if x %3==0]

#字典推导
>>> a={i:'{} squared is {}'.format(i,i**2) for i in range(10)}
A >>> [. 9 ]
 ' . 9 Squared IS 81 ' 
>>> A [. 11 ] 
Traceback (MOST Recent Last Call): 
File " <pyshell # 18 is> " , Line. 1, in <Module1> 
A [ . 11 ] 
a KeyError: . 11 IF n-> 10 :
 Print ( ' > 10 ' )
 elif n->. 5 :
 Pass # code thought good how to run because in Python code block can not be empty. To fix this problem, simply add a statement to pass in the middle of the block. the else :
 Print ( " <=. 5 " )

 


 

Guess you like

Origin www.cnblogs.com/wwz-wwz/p/11129963.html