Title here
Summary here
Our example demonstrates the use of constants and how they function. Here’s the full source code translated to Python.
Python supports constants for various data types including strings, integers and floating point numbers, although Python does not have a const
keyword. Constants in Python are conventionally represented by naming them in all uppercase. This example demonstrates constants and some operations with them.
import math
# Constants are declared with all uppercase variable names by convention
S = "constant"
def main():
print(S)
N = 500000000
# Constant expressions perform arithmetic with arbitrary precision
D = 3e20 / N
print(D)
# A numeric constant has no type until it's given one by being used
print(int(D))
# Math.sin expects a float
print(math.sin(N))
if __name__ == "__main__":
main()
To run the program, save the code to a file named constant.py
and run it using the Python interpreter.
$ python constant.py
constant
6e+11
600000000000
-0.28470407323754404
Now that we can run and utilize constants in basic Python programs, let’s learn more about the language.