Fundamentals 7 min read

20 Common Python Built-in Constants with Example Code

This article introduces twenty frequently used Python built-in constants—including True, False, None, NotImplemented, Ellipsis, __debug__, and various exception classes—explaining their meanings and providing clear code snippets that demonstrate how each constant can be applied in typical programming scenarios.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
20 Common Python Built-in Constants with Example Code

When discussing Python's built-in constants, we refer to fixed values defined in built-in modules that are frequently used in programming.

True and False : Boolean constants representing logical true and false.

is_valid = True
if is_valid:
print("验证通过")
else:
print("验证失败")

None : Represents the absence of a value.

result = calculate_result()
if result is None:
print("计算结果不存在")
else:
print("计算结果为:", result)

NotImplemented : Indicates a method or functionality that has not been implemented.

class BaseClass:
def process_data(self):
raise NotImplementedError("该方法尚未实现")
class DerivedClass(BaseClass):
pass
obj = DerivedClass()
obj.process_data()

Ellipsis : Used to denote an omitted piece of code or logic.

def process_data(data):
if data == Ellipsis:
print("数据待补充")
else:
print("数据完整")
# 调用函数
process_data(...)

__debug__ : Boolean constant indicating whether the Python interpreter is running in debug mode.

def make_api_request():
url = "http://api.example.com"
# 在调试模式下输出请求的URL
if __debug__:
print("请求URL:", url)
# 发送API请求的代码
# ...
# 调用函数
make_api_request()

True and False integer values : True equals 1, False equals 0.

success = True
if success == 1:
print("操作成功")
else:
print("操作失败")

NotImplementedError : Exception class indicating an unimplemented method or functionality.

def process_data(data):
if data is NotImplemented:
raise NotImplementedError("该功能尚未实现")
else:
print("数据处理完成")
# 调用函数
process_data(NotImplemented)

StopIteration : Exception class indicating that an iterator has no more items.

data = [1, 2, 3]
iterator = iter(data)
while True:
try:
value = next(iterator)
print(value)
except StopIteration:
break

OverflowError : Exception class indicating a numeric overflow.

value = 10 ** 1000
print(value)

FloatingPointError : Exception class indicating a floating‑point operation error.

result = 1.0 / 0.0
print(result)

ZeroDivisionError : Exception class indicating division by zero.

result = 1 / 0
print(result)

ArithmeticError : Base class for numeric calculation errors.

try:
result = 1 / 0
print(result)
except ArithmeticError as e:
print("数值运算错误:", str(e))

EOFError : Exception class indicating end of input stream.

try:
data = input("请输入数据:")
print("输入的数据为:", data)
except EOFError:
print("输入流已结束")

IndentationError : Exception class indicating an indentation problem.

def process_data(data):
print(data)

TabError : Exception class indicating mixed tabs and spaces.

def process_data(data):
if data is None:
print("数据为空")
else:
print("数据为:", data)
# 调用函数
process_data( None)

UnicodeError : Exception class indicating Unicode‑related errors.

data = "中文"
encoded_data = data.encode("ascii")
print(encoded_data)

FileNotFoundError : Exception class indicating a missing file.

filename = "data.txt"
try:
with open(filename, "r") as file:
contents = file.read()
print(contents)
except FileNotFoundError:
print("文件不存在:", filename)

TypeError : Exception class indicating a type mismatch.

data = 100
length = len(data)
print(length)

ValueError : Exception class indicating an inappropriate value.

data = "abc"
integer_value = int(data)
print(integer_value)

AssertionError : Exception class indicating a failed assertion.

data = 10
assert data > 100, "数据不符合要求"

These example codes demonstrate the usage of twenty common Python built-in constants in real‑world automation tasks, helping developers write more efficient and readable code.

pythonprogrammingfundamentalsexamplesBuilt-in Constants
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.