【Python】【基础知识】【内置常量】

Python的内置常量有:

False、True、None、NotImplemented、Ellipsis、__debug__

由 site 模块添加的常量:quit、exit、copyright、credits、license

内置常量

有少数的常量存在于内置命名空间中。 它们是:

False

bool 类型的假值。 给 False 赋值是非法的并会引发 SyntaxError

True

bool 类型的真值。 给 True 赋值是非法的并会引发 SyntaxError

None

NoneType 类型的唯一值。 None 经常用于表示缺少值,当因为默认参数未传递给函数时。 给 None 赋值是非法的并会引发 SyntaxError

NotImplemented

二进制特殊方法应返回的特殊值(例如,__eq__()__lt__()__add __()__rsub__() 等)表示操作没有针对其他类型实现;为了相同的目的,可以通过就地二进制特殊方法(例如,__imul __()__ rightnd__()等)返回。 它的逻辑值为真。

注解

当二进制(或就地)方法返回``NotImplemented``时,解释器将尝试对另一种类型(或其他一些回滚操作,取决于运算符)的反射操作。 如果所有尝试都返回``NotImplemented``,则解释器将引发适当的异常。 错误返回的``NotImplemented``将导致误导性错误消息或返回到Python代码中的``NotImplemented``值。

参见 实现算数运算 为例。

注解

NotImplementedError 和 NotImplemented 不可互换,即使它们有相似的名称和用途。 有关何时使用它的详细信息,请参阅 NotImplementedError

Ellipsis

与省略号文字字面 “...” 相同。 特殊值主要与用户定义的容器数据类型的扩展切片语法结合使用。

__debug__

如果 Python 没有以 -O 选项启动,则此常量为真值。 另请参见 assert 语句。

注解

变量名 NoneFalseTrue 和 __ debug__ 无法重新赋值(赋值给它们,即使是属性名,将引发SyntaxError ),所以它们可以被认为是“真正的”常数。

由 site 模块添加的常量

site 模块(在启动期间自动导入,除非给出 -S 命令行选项)将几个常量添加到内置命名空间。 它们对交互式解释器 shell 很有用,并且不应在程序中使用。

quit(code=None)
exit(code=None)

当打印此对象时,会打印出一条消息,例如“Use quit() or Ctrl-D (i.e. EOF) to exit”,当调用此对象时,将使用指定的退出代码来引发 SystemExit

copyright
credits

打印或调用的对象分别打印版权或作者的文本。

license

当打印此对象时,会打印出一条消息“Type license() to see the full license text”,当调用此对象时,将以分页形式显示完整的许可证文本(每次显示一屏)。

————————(我是分割线)————————

一般的内置常量

False、True、None、NotImplemented、Ellipsis、__debug__

False、True 属于bool类型

None 属于NoneType 类型

NotImplemented  二进制特殊方法应返回的特殊值

Ellipsis  与省略号文字字面 “...” 相同。

__debug__

>>> __debug__
True

如果 Python 没有以 -O 选项启动,则此常量为真值。 另请参见 assert 语句。

————————(我是分割线)————————

由 site 模块添加的常量

quit()

(在Windows环境执行,直接退出PythonIDLE,不过退出之前会弹出确认对话框)

exit()

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> i = 1
>>> print(i)
1
>>> exit
Use exit() or Ctrl-Z plus Return to exit
>>> exit()

执行后,弹出确认对话框;

copyright

版权/著作权显示:

>>> copyright
Copyright (c) 2001-2018 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>> 

credits

信誉

>>> credits
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information.
>>> 

感谢 CWI, CNRI, BeOpen.com, Zope Corporation 和数以千计的支持python开发的人员。有关详细信息,请参见www.python.org。

license()

>>> license()
A. HISTORY OF THE SOFTWARE
==========================

Python was created in the early 1990s by Guido van Rossum at Stichting
Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands
as a successor of a language called ABC.  Guido remains Python‘s
principal author, although it includes many contributions from others.

In 1995, Guido continued his work on Python at the Corporation for
National Research Initiatives (CNRI, see http://www.cnri.reston.va.us)
in Reston, Virginia where he released several versions of the
software.

In May 2000, Guido and the Python core development team moved to
BeOpen.com to form the BeOpen PythonLabs team.  In October of the same
year, the PythonLabs team moved to Digital Creations, which became
Zope Corporation.  In 2001, the Python Software Foundation (PSF, see
https://www.python.org/psf/) was formed, a non-profit organization
created specifically to own Python-related Intellectual Property.
Zope Corporation was a sponsoring member of the PSF.

All Python releases are Open Source (see http://www.opensource.org for
the Open Source Definition).  Historically, most, but not all, Python
Hit Return for more, or q (and Return) to quit: quit
Hit Return for more, or q (and Return) to quit: q
>>> 

————————(我是分割线)————————

参考:

1. RUNOOB.COM:https://docs.python.org/zh-cn/3/library/constants.html

备注:

初次编辑时间:2019年9月30日17:55:21

环境:Windows 7   / Python 3.7.2

原文地址:https://www.cnblogs.com/kaixin2018/p/11613737.html

时间: 2024-08-06 04:49:50

【Python】【基础知识】【内置常量】的相关文章

Python基础:内置常量

本文根据Python 3.6.5的官文Built-in Constants编写,官文比较短,大家可以直接看原文. 有一些存在于 内置名称空间(the built-in namespace) 的常量,如下: False True 上面两个都是bool型,不可以被赋值. None 类型NoneType的唯一的值.None经常被用于表示一个值得缺席,被作为默认参数时不会被传送给函数. 也不可以被赋值. NotImplemented 这个常量的解释较多,也应该更复杂一些咯.孤的理解就是,字面意思,提示

python基础知识内置函数(二)、装饰器

一.内置函数 1.chr()  ord() r= chr(65) #ASCII对应的字母 print (r) n= ord("a") #ASCII对应的数字 print(n) #以下为执行结果 A 97 可以利用此函数随机生成验证码: import random li=[] for i in range(6): r = random.randrange(0,5) if r ==2 or r==4: num = random.randrange(0,10) li.append(str(n

Python基础day-11[内置函数(未完),递归,匿名函数]

内置函数: abs() : 返回数字的绝对值.参数可以是整数或浮点数,如果参数是复数,则返回复数的模. print(abs(0.2)) print(abs(1)) print(abs(-4)) print(abs(-0.2)) print(abs(3+4j)) 执行结果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 0.2 1 4 0.2 5.0 Process finished with exit code 0 all():

第六篇:python基础_6 内置函数与常用模块(一)

本篇内容 内置函数 匿名函数 re模块 time模块 random模块 os模块 sys模块 json与pickle模块 shelve模块 一. 内置函数 1.定义 内置函数又被称为工厂函数. 2.常用的内置函数 (1)abs() #!/usr/binl/env python #encoding: utf-8 #author: YangLei print(abs(-1)) (2)all() #!/usr/binl/env python #encoding: utf-8 #author: Yang

python 基础 学习 内置函数

内置函数       例:如果返回数字的绝对值 ,写函数是非常不方便的 [[email protected] tools]# python fa.py  10 [[email protected] tools]# cat fa.py  #!/usr/bin/python def a(x):     if x < 0 :         return -x      return x  n = a(-10) print n  #帮助查看# >>> help(len) 用法: help

Python 基础5:内置函数一

===========内置函数=========== 1.abs绝对值 #abs() i = abs(-123) print(i) #结果:123 2.all与any #all 循环参数,如果每个元素都为真,那么all的返回值为真 #any,只要有一个是真的,则为真 r = all([True,True,False]) print(r) #结果:False #元素为假的有:0,None,空的字符串.列表.元组.字典 3.ascii,对象的类中找__repr__,获取齐返回值 # class Fo

python基础学习-内置函数

#!/usr/bin/env python # -*- coding:utf-8 -*- 系统内置函数 n =abs(-1) #绝对值 print(n) #bytes()函数 s="离开" re= bytes(s,encoding="utf-8")  # bytes() 把字符串 转换成字节 print(re) res = str(re,encoding="utf-8") #转换回字符串 print(res) re= bytes(s,encodi

Python基础编程 内置函数

内置函数 内置函数(一定记住并且精通) print()屏幕输出 int():pass str():pass bool():pass set(): pass list() 将一个可迭代对象转换成列表 tuple() 将一个可迭代对象转换成元组 dict() 通过相应的方式创建字典. # 创建字典的几种方式 #直接创建 dic = {1: 2} #字典推导式 print({i: 1 for i in range(3)}) dict() #dict创建 dic = dict(one=1, two=2,

Python标准库 -- 内置常量

Built-in Constants 在内置的空间中仅有很少的常量被定义.如:False.True.None Constants added by the site module site模块会在程序执行之前自动加载常量 False.True.None. quit(code=None)  exit(code=None) 来退出程序  或者直接使用Ctrl+D

学习PYTHON之路, DAY 4 - PYTHON 基础 4 (内置函数)

注:查看详细请看https://docs.python.org/3/library/functions.html#next 一 all(), any() False: 0, Noe, '', [], {}, () all()  全部为真是, 才为真 any() 任何一个为真, 都为真 二 bin(), oct(),hex() bin(), 接收十进制转二进制 (0b) oct(), 接收十进制转八进制 (0o) hex(), 接收十进制转十六进制 (0x) 三 bytes() bytes(只要转