数值的并行赋值
x, y, z = 1, 2, 3
print('x:', x)
# output: x: 1
print('y:', y)
# output: y: 2
print('z:', z)
# output: z: 3
字符串的并行赋值
x, y, z = 'abc'
print('x:', x)
# output: x: abc
print('y:', y)
# output: y: abc
print('z:', z)
# output: z: abc
列表的赋值
a = b = c = [1, 2, 3]
x, y, z = [a, b, c]
print('x:', x)
# output: x: [1, 2, 3]
print('y:', y)
# output: y: [1, 2, 3]
print('z:', z)
# output: z: [1, 2, 3]
z[1] = 0
print('x:', x)
# output: x: [1, 0, 3]
print('y:', y)
# output: y: [1, 0, 3]
print('z:', z)
# output: z: [1, 0, 3]
变量交换
x, y, z = 1, 2, 3
print('x:', x)
# output: x: 1
print('y:', y)
# output: y: 2
print('z:', z)
# output: z: 3
x, y, z = y, z, x
print('x:', x)
# output: x: 2
print('y:', y)
# output: y: 3
print('z:', z)
# output: z: 1
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
x, y, z = [a, b, c]
print('x:', x)
# output: x: [1, 2, 3]
print('y:', y)
# output: y: [4, 5, 6]
print('z:', z)
# output: z: [7, 8, 9]
x, y, z =y, z, x
print('x:', x)
# output: x: [4, 5, 6]
print('y:', y)
# output: y: [7, 8, 9]
print('z:', z)
# output: z: [1, 2, 3]
解包与赋值
dict1 = {'name': 'python', 'version': '310'}
key, value=dict1.popitem()
print('key:{0},value:{1}'.format(key, value))
# output: key:version,value:310
星号运算符*来收集多余的值
x, y, *z = 1, 2, 3, 4
print('x:', x)
# output: x: 1
print('y:', y)
# output: y: 2
print('z:', z)
# output: z: [3, 4]
链式赋值
x = y = z = [1, 2, 3]
print('x:', x)
# output: x: [1, 2, 3]
print('y:', y)
# output: y: [1, 2, 3]
print('z:', z)
# output: z: [1, 2, 3]
print('x is y:', x is y)
# output: x is y: True
print('y is z:', y is z)
# output: y is z: True
print('x is z:', x is z)
# output: x is z: True
print('\n')
z = [1, 2, 3]
y = z
x = y
print('x:', x)
# output: x: [1, 2, 3]
print('y:', y)
# output: y: [1, 2, 3]
print('z:', z)
# output: z: [1, 2, 3]
print('x is y:', x is y)
# output: x is y: True
print('y is z:', y is z)
# output: y is z: True
print('x is z:', x is z)
# output: x is z: True
print('\n')
z = [1, 2, 3]
y = [1, 2, 3]
x = [1, 2, 3]
print('x:', x)
# output: x: [1, 2, 3]
print('y:', y)
# output: y: [1, 2, 3]
print('z:', z)
# output: z: [1, 2, 3]
print('x is y:', x is y)
# output: x is y: False
print('y is z:', y is z)
# output: y is z: False
print('x is z:', x is z)
# output: x is z: False
增强赋值
x = 2
x += 2
print('x:', x)
# output: 4
x *= 2
print('x:', x)
# output: 8
str1 = 'abc'
str2 = str1 * 10
print('x:', str2)
# output: bcabcabcabcabcabcabcabcabcabc
pass
pass_stmt ::= "pass"
pass 是一个空 *** 作 --- 当它被执行时,什么都不发生。
它适合当语法上需要一条语句但并不需要执行任何代码时用来临时占位,例如:
def f(arg): pass # a function that does nothing (yet)
class C: pass # a class with no methods (yet)
del
del_stmt ::= "del" target_list
删除是递归定义的,与赋值的定义方式非常类似。
此处不再详细说明,只给出一些提示。
目标列表的删除将从左至右递归地删除每一个目标。
名称的删除将从局部或全局命名空间中移除该名称的绑定,具体作用域的确定是看该名称是否有在同一代码块的 global 语句中出现。
如果该名称未被绑定,将会引发 NameError。
属性引用、抽取和切片的删除会被传递给相应的原型对象;删除一个切片基本等价于赋值为一个右侧类型的空切片(但即便这一点也是由切片对象决定的)。
在 3.2 版更改: 在之前版本中,如果一个名称作为被嵌套代码块中的自由变量出现,则将其从局部命名空间中删除是非法的。
exec
exec
(object[, globals[, locals]])
该函数支持 Python 代码的动态执行。
object必须是字符串或代码对象。
如果它是一个字符串,则该字符串被解析为一组 Python 语句,然后执行(除非出现语法错误)。
如果只提供了 globals,则必须为字典对象(而不能是字典的子类),同时用于存放全局变量和局部变量。
如果提供了 globals 和 locals,则将分别用于全局变量和局部变量。
locals 可以是任意字典映射对象。
请记住,在模块级别,globals 和 locals 是同一个字典。
exec("print('python')")
# output: python
添加第二个参数——字典,用作代码字符串的命名空间。
import math
dict1 = {'ma': math}
exec("print(ma.pi)", dict1)
# output: 3.141592653589793
eval
eval
(expression[, globals[, locals]])
实参是一个字符串,以及可选的 globals 和 locals。
globals 实参必须是一个字典。
locals 可以是任何映射对象。
表达式解析参数 expression 并作为 Python 表达式进行求值(从技术上说是一个条件列表),采用 globals 和 locals 字典作为全局和局部命名空间
eval计算用字符串表示的Python表达式的值,并返回结果(exec什么都不返回,因为它本身是条语句)。
x = 1
print(eval('x+1'))
# output: 2
import math
dict1 = {'ma': math, 'x': 2, 'y': 3}
print(eval('ma.pow(x,y)',dict1))
# output: 8.0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)