文章目录
- 1 变量的定义和方法
 - 2 Python是动态型语言
 - 3 Python是强类型语言
 - 4 Python中变量名是引用
 - 5 Python中区分大小写
 - 6 变量命名规范
 - 7 iPython中的特殊变量
 - 8 查看Python中关键字的方法
 - 9 查看已定义的所有变量
 - 10 删除变量
 
1 变量的定义和方法
运行程序:
testBool=True
testInt=20
testFloat=10.6
testStr="ssddd"
testBool,testInt,testFloat,testStr
 
运行结果:
(True, 20, 10.6, 'ssddd')
 
2 Python是动态型语言
运行程序:
x=10
x="testme"
x
 
运行结果:
'testme'
 
3 Python是强类型语言
运行程序:
##python中除了int、float、bool、complex之间不能进行数据类型转换
#"3"+2  #报错
3+2   #不报错
3+3.3#不报错
3+(1+3j)#不报错
 
运行结果:
5
6.3
(4+3j)
 
4 Python中变量名是引用
运行程序:
i=20
i="mysql"
i=30.1 
i
 
运行结果:
30.1
 
5 Python中区分大小写
运行程序:
i=20
I
 
运行结果:
NameError                                 Traceback (most recent call last)
Cell In[1], line 2
      1 i=20
----> 2 I
NameError: name 'I' is not defined
 
6 变量命名规范
运行程序:
#1.字母、数字、下划线组成
#2.可以由字母或下划线开头,不能数字开头
#3.要用python中的关键字作为变量名
#2_cc=0  #无效变量
#print=0  #不报错
x=0
print(x)  #报错:重新启动“会话”,删除所有变量
 
运行结果:
0
 
7 iPython中的特殊变量
运行程序:
x=12+13
x
#In[13]  #iPython编辑器中的特殊变量
_  #临时变量,最近一个out变量
 
运行结果:
25
 
8 查看Python中关键字的方法
运行程序:
import keyword
keyword.kwlist
 
运行结果:
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'async',
 'await',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']NameError                                 Traceback (most recent call last)
Cell In[1], line 2
      1 i=20
----> 2 I
NameError: name 'I' is not defined
 
9 查看已定义的所有变量
运行程序:
dir()
 
运行结果:
['In',
 'NamespaceMagics',
 'Out',
 '_',
 '_2',
 '_6',
 '_7',
 '_8',
 '_Jupyter',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_getshapeof',
 '_getsizeof',
 '_i',
 '_i1',
 '_i2',
 '_i3',
 '_i4',
 '_i5',
 '_i6',
 '_i7',
 '_i8',
 '_i9',
 '_ih',
 '_ii',
 '_iii',
 '_nms',
 '_oh',
 'exit',
 'get_ipython',
 'getsizeof',
 'json',
 'keyword',
 'np',
 'quit',
 'var_dic_list',
 'x']
 
10 删除变量
运行程序:
i=20
print(i)
del i  #删除变量i
 
运行结果:
20










