深海之战下载,摩托迷论坛,安乐窝红月
python程序是由代码块构成的,一个代码块的文本作为pythont程序执行的单元
官方文档: a python program is constructed from code blocks. a block is a piece of python program text that is executed as a unit. the following are blocks:
a module, a function body, and a class definition. each command typed interactively is a block. a script file (a file given as standard input to the
interpreter or specified as a command line argument to the interpreter) is a code block. a script command (a command specified on the interpreter command
line with the ‘-c‘ option) is a code block. the string argument passed to the built-in functions eval() and exec() is a code block. a code block is executed
in an execution frame. a frame contains some administrative information (used for debugging) and determines where and how execution continues after the code
block’s execution has completed.
一个代码块:
通过id()可以查看到一个变量表示的值在内存中的地址
s = "hello" print(id(s)) # 2305859175064
如果内存地址相同,则值一定是相等的,如果值相等,则不一定同一对象
a = 100 b = 100 print(a is b) # true print(a == b) # true
a = 1000 b = 1000 print(a == b) # true print(a is b) # false 在command命令下为false, 在.py文件中(例如pycharm中)得到的结果为true。(详情见下面)
优点:能够提高字符串、整数的处理速度。省略了创建对象的过程。
缺点:在"池"中创建或者插入新的内容会花费更多的时间。
1.整数
官方文档: the current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object. so it should be possible to change the value of 1. i suspect the behavior of python in
this case is undefined.
2.字符串
incomputer science, string interning is a method of storing only onecopy of each distinct string value, which must be immutable. interning strings makes
some stringprocessing tasks more time- or space-efficient at the cost of requiring moretime when the string is created or interned. the distinct values are
stored ina string intern pool. –引⾃自维基百科
在代码块内缓存机制是不一样的:
注意:对于同一个代码块,只针对单纯创建变量,才会采用缓存机制,对于创建变量并同时做相关运算,则无。
a = 1000 b = 1000 print(id(a)) # 2135225709680 print(id(b)) # 2135225709680 print(a is b) # true .py文件运行
a = 1000 b = 10*100 print(id(a)) # 1925536396400 print(id(b)) # 1925536643952 print(a is b) # false .py文件运行
a = 5*5 b = 25 print(id(a)) # 1592487712 print(id(b)) # 1592487712 print(a is b) # true .py文件运行
a = "incomputer science, string interning is a method of storing only onecopy of each distinct string value" b = "incomputer science, string interning is a method of storing only onecopy of each distinct string value" print(id(a)) # 2926961023256 print(id(b)) # 2926961023256 print(a is b) # true .py文件运行
# _*_ encoding:utf-8 _*_
在python3内存中,在程序运行阶段,使用的是unicode编码。因为unicode是万国码。什么内容都可以进行显示。那么在数据传输和存储时由于unicode比较浪费空间和资源。需要把unicode转存成utf-8或gbk进行存储。
编码之后的数据是bytes类型的数据。
a = "hello" b = "中" print(a.encode("utf-8")) # b'hello' print(b.encode("utf-8")) # b'\xe4\xb8\xad' print(b.encode("gbk")) # b'\xd6\xd0'
以何种编码编码,就须以该种编码解码。否则,解码不成功,得不到想要的内容。
a = "编码" print(a.encode("utf-8")) # b'\xe7\xbc\x96\xe7\xa0\x81' b = b'\xe7\xbc\x96\xe7\xa0\x81' print(b.decode("utf-8")) # 编码
不同编码之间的转换:
# gbk 转换成 utf-8 s = "我是文字" bs = s.encode("gbk") # 把gbk转换成unicode,也就是解码 s = bs.decode("gbk") # 重新编码成utf-8 bss = s.encode("utf-8") print(bss)
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论