Constants in Fortress

Constants in Python

Python supports constants of character, string, boolean, and numeric values. Here’s how you can declare and use constants in Python.

# Although Python does not have built-in constant types, by convention, 
# constants are usually declared and assigned in a module.

# Defining constants
S = "constant"

def main():
    print(S)

    # Constants can appear anywhere a variable statement can.
    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, such as by an explicit conversion.
    print(int(D))

    # A number can be given a type by using it in a context that requires one, 
    # such as a variable assignment or function call. For example, here math.sin expects a float.
    import math
    print(math.sin(N))

if __name__ == "__main__":
    main()

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

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

Now that we understand how to use constants in Python, let’s explore more about the language.