Python Python basis of statements

Python statements

Assignment

  >>> (x,y) = (5,10)
>>> x
5
>>> y
10
>>> x,y = 5,10
>>> x,y
(5, 10)
>>> [x,y,z] = [1,2,3]
>>> x,y,z
(1, 2, 3)
>>> x,y = y,x
>>> x,y
(2, 1)
>>> [a,b,c] = (1,2,3)

  #序列赋值
>>> a,b,c = 'abc'
>>> a
'a'
>>> b
'b'
>>> c
'c'
#左右两边不一致
>>> a,b,*c = 'abcdefgh'
>>> a
'a'
>>> b
'b'
>>> c
['c', 'd', 'e', 'f', 'g', 'h']
>>> a,*b,c = 'abcdefgh'
>>> a
'a'
>>> b
['b', 'c', 'd', 'e', 'f', 'g']
>>> c
'h'

>>> s = 'abcderf'
>>> a,b,c = s[0],s[1],s[2:]
>>> a
'a'
>>> b
'b'
>>> c
'cderf

>>> a,b,c,*d = 'abc'
>>> a
'a'
>>> b
'b'
>>> c
'c'
>>> d
[]


>>> a = b = c = 'hello'
>>> a
'hello'
>>> b
'hello'

  
>>> a = 'hello'
>>> b = 'world'
>>> print(a,b)
hello world
>>> print(a,b,sep='|')
hello|world
>>> print(a,b,sep='|',end='...\n')
hello|world...
>>> print(a,b,end='...\n',file=open('result.txt','w'))

Conditional statements

  
= 75 Score
IF Score> = 90:
  Print ( 'excellent')
elif Score> = 80:
  Print ( 'good')
elif Score> = 60:
  Print ( "pass")
the else:
  Print ( 'fail')
   

  
the Add DEF (X):
  Print (X + 10) Operation = {   'the Add': the Add,   'Update': the lambda X: Print (X * 2),   'Delete': the lambda X: Print (X *. 3) } operation.get ( 'the Add') (10) operation.get ( 'Update') (10) operation.get ( 'Delete') (10) # ternary expressions with result = 'pass' if score> = 60 else 'fail'












loop statement

Guess you like

Origin www.cnblogs.com/gdy1993/p/12104847.html