Understanding Constants in Python: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-24 05:03:08   1006  Share
Subject Syllabus DetailsSubject Details Login to Open Video
☰ TContent
☰Fullscreen

Table of Content:


  • 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)
    


    Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.