Constants in Python

Our topic will be constants. Constants in the target language can be character, string, boolean, or numeric values. Below is a more detailed explanation and the corresponding code example in Python.

import math

# Declares a constant value
S = "constant"

def main():
    print(S)

    # Constants can appear anywhere a variable declaration can
    N = 500000000

    # Constant expressions perform arithmetic with arbitrary precision
    D = 3e20 / N
    print(D)

    # To convert the value to a specific type
    print(int(D))

    # A number can be given a type by using it in a context that requires one
    # For example, math.sin expects a float.
    print(math.sin(N))

if __name__ == "__main__":
    main()

To run the program, save the code to a file named constants.py and execute it using Python.

$ python constants.py
constant
6e+11
600000000000
-0.28470407323754404

This Python program demonstrates how to declare and use constants, perform arithmetic with arbitrary precision, and handle numeric type conversions.