Python基础之Python语句
Posted gdy1993
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=‘... ‘)
hello|world...
>>> print(a,b,end=‘... ‘,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语句的主要内容,如果未能解决你的问题,请参考以下文章