Skip to content

Python constants file

  • by

You can create a constant name file with a constant variable but it is not truly Constant File because there is no strict concept of constant variables like in other languages in Python.

Python constants file example

A simple example code creates constant.py a Python file that contains only constants and then imports them into your main.py.

constant.py

PI = 3.14159
GRAVITY = 9.81
SPEED_OF_LIGHT = 299792458

main.py

import constants

print(constants.PI) # 3.14159
print(constants.GRAVITY) # 9.81
print(constants.SPEED_OF_LIGHT) # 299792458

Output:

Python constants file

Do comment if you have any doubts or suggestions on this Python constant topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading