5、python学习之变量

变量的定义:为了方便日后调用,存储程序中的一些中间结果
变量定义的规则:
1、要具有描述性
2、变量名只能是字母、数字、下划线的任意组合,不可以是空格或特殊字符(!@#¥%*)
3、不能以中文为变量名
4、变量名的第一个字母不能是数字
5、以下关键字不能声明为变量名
['and','as','assert','break','class','continue','def','del','elif','else','except','finally','for','from','global','if','import','in','is','lambda','not','or','pass','print','paise','return','try','while','with','yield']

# python 风格指南: https://zh-google-styleguide.readthedocs.io/en/latest/


举例:
赋值
>>> x = 3
>>> y = 4
>>> z = x * y
>>> print ("x*y=", z)
x*y= 12
>>> print("z=",z)
z= 12
>>> print (3*4*5*6/2)
180.0
多次赋值
>>> a = 1
>>> b = a
>>> print(a)
1
>>> print(b)
1
>>> a = 2
>>> print(a)
2
>>> print(b)
1
删除变量-1
>>> abc = 21
>>> print(abc)
21
>>> del abc
>>> print(abc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
删除变量-2
>>> abc = 21
>>> print(abc)
21
>>> abc = 25
>>> print(abc)
25 (python内部会进行清理)
note:
1、python不区分变量和常量(使用全部大写代表常量,常量:不变的量,pie 3.1415926)
2、引号内的值为字符串,没有引号的值为变量名

 

 

# python 风格指南: https://zh-google-styleguide.readthedocs.io/en/latest/

from keyword import kwlist
for i in kwlist:
print(i)

# 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

 

posted @ 2019-05-23 22:22  hlc-123  阅读(131)  评论(0编辑  收藏  举报