前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 系列文章 —— print 详解篇

Python 系列文章 —— print 详解篇

原创
作者头像
玩转编程
修改2022-01-14 14:50:21
2200
修改2022-01-14 14:50:21
举报
文章被收录于专栏:玩转编程玩转编程
  • print
代码语言:python
复制
# 改变变量的值,变量的标识也对应改变
python_variable = 10000
print("first id of python_variable: ", id(python_variable))
python_variable = 12345
print("second id of python_variable: ", id(python_variable))
print("\n\n")

# 即使变量的值不变,变量的标识也可能改变
python_variable = 10000
print("first id of 10000: ", id(python_variable))
python_variable = 10000
print("second id of 10000: ", id(python_variable))
print("\n\n")
# 注意,这一段示例最好在交互式环境中测试。在 IDE 中可能被优化。


# 对列表来讲,变量对应的值不变,变量标识依然可能改变
python_variable = [1,2]
print("first id of list: ", id(python_variable))
python_variable = [1,2]
print("second id of list: ", id(python_variable))
print("\n\n")


# 对变量进行运算并将结果赋值给原变量,会改变变量的标识
change_ref = 10000
print("first id of unchanged: ", id(change_ref))
change_ref = change_ref + 1
print("second value of change_ref: ", change_ref)
print("second id of changed: ", id(change_ref))
print("\n\n")


# 就地对变量所引用对象进行操作,不改变变量的标识
list_nonchange = [1, 2, 3]
print("first id of list_nonchange: ", id(list_nonchange))
list_nonchange[2] = 5
print("second value of list_nonchange: ", list_nonchange)
print("second id of list_nonchange: ", id(list_nonchange))
list_nonchange.append(3)
print("third value of list_nonchange: ", list_nonchange)
print("third id of list_nonchange: ", id(list_nonchange))
print("\n\n")


# Python 中,变量引用一个对象
const_ref = 10000
print("'const_ref == 10000' is: ", const_ref == 10000)
print("'const_ref is 10000' is: ", const_ref is 10000)
print("first id of const_ref: ", id(const_ref))
print("another id of 10000: ", id(10000))
print("\n\n")


from sys import getrefcount
print("the reference count of const_ref: ", getrefcount(const_ref))
print("the reference count of 10000: ", getrefcount(10000))
代码语言:python
复制
def findMaxofNumber():
    '''
    打印并返回 Python 常量池中的最大数值
    '''
    for b in range(300):
        if b is not range(300)[b]:
            print("常量池最大值为:", (b - 1))
            return (b - 1)

if __name__ == '__main__':
    findMaxofNumber()

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档