Constants in Scheme

Translation to Python

Constants are a key part of many programming languages. In Python, constants are typically denoted by using uppercase variable names. Python doesn’t have a constant keyword, so the constants are not strictly enforced by the compiler but are a convention.

Here’s how we can use constants in Python:

# Constants should be defined at the top level (global scope)

import math

# Declare a constant value
S = "constant"

def main():
    print(S)

    # A constant statement 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
    print(int(D))

    # A number can be given a type by using it in a context that requires one
    # 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 named constant.py and execute it using the Python interpreter.

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

Now that we can run and understand basic Python programs with constants, let’s learn more about the language.