python 语法糖

  • Post author:
  • Post category:python




什么是语法糖?

语法糖是编程的一种更加简洁的特殊写法。



1.交换两个变量的值
a,b = b,a


2.判断变量是否在范围内
a = 97
if 90 < a < 100:
	print("a在90-100范围内")


3.快速构造字符串
# print('------------------------------')
print('-'*60)
print("*"*20)


4.列表拼接
a = [1,2,3,4]
b = [5,6,7,8]
a + b 


5.列表切片
a=[1,2,3,4,5,6,7,8,9]
a[3,-2]
a[:3]
a[-3:]


6.打包与解包
a=(1,2,3)
x,y,z = a
b = (x,y,z)


7.with语句
with open("file1.txt",'r') as f:
	data = f.read()


8.列表推导式
a = [1,2,3,4]
b = [i + 233 for i in a]



版权声明:本文为chenhua_19891230原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。