Table of Contents

    Understanding Constants in Python: A Comprehensive Guide


  • What you will learn in this section:
    1. What is constant?
    2. Declaring and assigning value to a constant.
    3. Don't changing the value of a constant.
    4. How to utilize module.

     

    Assigning value to constant in Python

    In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.

    Declaring and assigning value to a constant

    constant.py

    PI = 3.14
    INTREST = 2.8  
    LAMDA = 0.01

    main.py

    import constant
    
    print(constant.PI)
    print(constant.INTREST)
    print(constant.LAMDA)