码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础之Python语句

时间:2019-12-26 22:55:22      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:pos   tab   左右   position   false   ant   add   abc   ola   

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 ‘不及格‘

循环语句

Python基础之Python语句

标签:pos   tab   左右   position   false   ant   add   abc   ola   

原文地址:https://www.cnblogs.com/gdy1993/p/12104847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!