Python基础之Python语句

Python语句

赋值语句

  >>> (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'))

条件语句

  
score = 75
if score >= 90:
  print('优秀')
elif score >= 80:
  print('良好')
elif score >= 60:
  print('及格')
else:
  print('不及格')
   

  
def add(x):
  print(x + 10)

operation = {
  'add': add,
  'update': lambda x: print(x * 2),
  'delete': lambda x: print(x * 3)
}

operation.get('add')(10)
operation.get('update')(10)
operation.get('delete')(10)

#三元表达式
result = '及格' if score >= 60 else '不及格'

循环语句

猜你喜欢

转载自www.cnblogs.com/gdy1993/p/12104847.html
今日推荐