1、print函数:
print (value , ... , sep=”, end= ’\n’, file=sys.stdout , flush=False)
print函数输出之后总会换行,这是因为 print函数的 end 参数的默认值是“\n”,改变end参数即可不换行。
2、长字符串(''' ''')
在长字符串中可以放置任何内容,包括放置单引号、双引号都可以,如果所定义的长字符串没有赋值给任何变量,那么这个字符串就相当于被解释器忽略了,也就相当于注释掉了。
3、换行
可以使用转义字符(\)对换行符进行转义:
num = 20 + 3 + 4 + \
2 * 3
()[]{}内换行不需要转义字符
4、将 bytes 对象解码成字符串,使用decode,默认使用UTF-8进行解码
bytes('我是',encoding='utf-8').decode('utf-8')
5、*号赋值
Python 允许在左边被赋值变量之前添加“*”,那么该变量就代表一个列表,可以保存多个集合元素。
first, *mid, last =range(10)
print(mid) #[1, 2, 3, 4, 5, 6, 7, 8]
6、循环使用 else
for循环中使用,最后输出结果等于循环体内最后结果
counti = 0
while counti < 5 :
print('counti 小于5 :', counti)
counti += 1
else:
print ('counti 大于或等于 5: ',counti)
out:
counti 小于5 : 0
counti 小于5 : 1
counti 小于5 : 2
counti 小于5 : 3
counti 小于5 : 4
counti 大于或等于 5: 5
for counti in range(6):
print('counti 小于 :', counti)
else:
print ('counti 大于或等于 5: ',counti)
out:
counti 小于 : 0
counti 小于 : 1
counti 小于 : 2
counti 小于 : 3
counti 小于 : 4
counti 小于 : 5
counti 大于或等于 5: 5
7、for循环简写
这两种方式都可以:
[x*x if x%2==0 else x for x in range(10)]
[x*x for x in range(10) if x%2==0]
但是这样写不可以:[x*x if x%2==0 for x in range(10)]
8、函数参数收集
*收集多个参数值,**收集多个关键词参数值
def test(x,y,z=3,*a,**b):
print('x,y,z分别为%s %s %s'%(x,y,z))
print('a = ',a)
print('b = ',b)
test(1,2,3,4,5,6,m=1,n=2)
out:
x,y,z分别为1 2 3
a = (4, 5, 6)
b = {'m': 1, 'n': 2}