python自学计划-声明变量-03

2018-12-11  本文已影响1人  你缺少想象力

python的变量声明真是简单到令人发指,我的天,居然有这么简单的变量声明方式

# 声明变量
name = 'zhangsan'
age = 18

# 连续声明变量
a = b = c = 1
# 同时声明变量
d, e, f = 1, 2, 'as'

print(name)
print(age)

print(a)
print(b)
print(c)

print(d)
print(e)
print(f)

运行结果:

zhangsan
18
1
1
1
1
2
as

声明的变量可以通过一下方式去除掉

del 变量名

如果打印被去除掉的变量,代码在运行的时候会报错
比如:

name = 'zhangsan'
age = 18

a = b = c = 1

d, e, f = 1, 2, 'as'

print(name)
print(age)

print(a)
print(b)
print(c)

print(d)
print(e)
print(f)

# 删除变量
del f
print(f)

运行结果:

zhangsan
Traceback (most recent call last):
18
1
1
  File "/Users/qixuefeng/PycharmProjects/Demo1/src/Learn03.py", line 21, in <module>
1
1
2
as
    print(f)
NameError: name 'f' is not defined

再运行一次可能会变成这样:

zhangsan
Traceback (most recent call last):
18
1
1
1
1
2
  File "/Users/qixuefeng/PycharmProjects/Demo1/src/Learn03.py", line 21, in <module>
as
    print(f)
NameError: name 'f' is not defined

说明这里可能是用的多线程

上一篇下一篇

猜你喜欢

热点阅读