Constants in Squirrel

Constants

Python supports constants of character, string, boolean, and numeric values. Constants in Python are typically declared using uppercase variable names, though Python itself does not enforce constants. We use naming conventions to signify that a variable should not be changed.

import math

# Declares a constant value.
S = "constant"

def main():
    print(S)

# A constant statement can appear anywhere a variable statement can.
N = 500_000_000

# 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.
print(math.sin(N))

if __name__ == "__main__":
    main()

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

$ python constants.py

You should see the following output:

constant
6e+11
600000000000
-0.28470407323754404

In this example, we have used Python’s naming convention for constants and demonstrated how constants can be used in various contexts such as arithmetic operations and type conversions.