2.Built in Constants
In this section, we will learn about some built in constants which are used to Python Programming. These constants are available under built-in namespace of python. Let's understand some important constants.
a. True False: In the official documentation, the True and False are listed as the first constant. These are Python Boolean values and are the instance of the int. A True has a value of 1, and False has a value 0.

Lets try out practically.
>>> True
True
>>> False
False
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
>>> int(True)
1
>>> int(False)
0
>>> True = 44
... SyntaxError: cannot assign to True
>>> False is False
True
>>> True is True
True
In python programming True and False are inbuilt constants we cannot reassign value to them if we try to reassign we will get Error.

b. Internal Dunder Names:
Python programming also has many internal dunder names that we can use as constants. Thse names always start and end with double underscore. in python There are several of these unique names, we will learn about __name__ and __file__ in this section.

__name__ :     is the Special variable in python is used to store name of the currently running python programme or script or module. The __name__ attribute is related to how to run a given piece of code. When importing a module, Python internal set the __name__ to a string containing the name of the module. Before executing a python script or a program the python interpreter assigns the name of the python script or program to a special variable known as __name__. The assigned name varies depending upon whether you are executing script though command line or importing from another module.

__file__ :    On the other hand, The __file__ special variable is an attribute of a Python module that contains the path of the script or module from which it is imported or accessed. If we use the __file__ attribute inside the file, we will get the path to the module itself.. In simple terms, __file__ is the pathname of file from which the module where __file__ is called, was loaded. Let say If you call __file__ from within second_file.py file, then __file__ will contain the path of second_file.py on your disk.

e.g. Following example show working of __file__ and __name__
1. save the following code in the first_file.py

print(f"The type of __name__ is: {type(__name__)}")
print(f"The value of __name__ is: {__name__}")

2.save the following code in second_file.py and execute the code

import first_file
print(f"The type of __file__ is: {type(__file__)}")
print(f"The value of __file__ is: {__file__}")

0utput:
The type of __name__ is:
The value of __name__ is: first_file
The type of __file__ is:
The value of __file__ is: D:\Python Project\second_file.py

These constants will play a vital role when we write math-related computational code .

c. The @property Decorator:
The @property decorator is a built in decorator in python for property function (). We can also use @property decorator to create a class that works as a namespace for constants. If we want to use a function like a property or an attribute, we can use @property decorator. The @property decorator is a built-in decorator that comes under property() function. We just need to define the constants property without providing them with a setter method. Let's understand the following example.

Example – working of property decorator in Python.
class ConstantsName:
@property
def PI(self):
  return 3.141592653589793
@property
def EULER_NUMBER(self):
 return 2.718281828459045
constant = ConstantsName()
print(constant.PI)
print(constant.EULER_NUMBER)
constant.PI = 3.14
print(constant.PI)

Output -
3.141592653589793
2.718281828459045
Traceback (most recent call last):
File "", line 13, in
AttributeError: can't set attribute

c. The namedtuple() Factory Function:
the named tuple is a factory function called namedtuple() available in Pythons collection module. Using the namedtuple() function, we can use the named fields and the dot notation to access their items.it allow you to create tuple subclasses with name fields, With namedtuple () we can create immutable sequence type that allow to access the values using field name and the dot notation. Immutable means we cannot modify an existing named tuple object.

e.g 1. use of a namedtuple() to create an un modifiable and tuple-like data structure with field names.
# Create a 2D point as a tuple
>>>point = (22, 42)
>>>point

(22, 42)

# Access coordinate x
>>>point[0]
22

# Access coordinate y
>>>point[1]
42

# Try to update a coordinate value
>>> point[0] = 32
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment

Now you have a point with two appropriately named fields, x and y. Your point provides a user-friendly and descriptive string representation (Point(x=22, y=42)) by default. It allows you to access the coordinates using the dot notation, which is convenient, readable, and explicit. You can also use indices to access the value of each coordinate.


e.g 2. use of a namedtuple() to create an un modifiable and tuple-like data structure with field names continued.
from collections import namedtuple
>>>Person = namedtuple("Person", "name children")
>>>ajay = Person("Ajay Dev", ["Tanay", "kajol"])
>>>Person(name='Ajay Dev’, children=[' Tanay ', ' kajol '])
>>>id(ajay.children)
>>>ajay.children.append("Tina")
>>>Person(name='Ajay Dev’, children=['Timmy', 'Jimmy', 'Tina'])
>>>id(ajay.children)
>>>hash(ajay)
Traceback (most recent call last):
File "", line 1, in
TypeError: unhashable type: 'list'

You can create named tuples that contain mutable objects. You can modify the mutable objects in the underlying tuple. However, this doesn’t mean that you’re modifying the tuple itself. The tuple will continue holding the same memory references.
Finally, tuples or named tuples with mutable values aren’t hashable, as you saw in the above example. Finally, since namedtuple classes are subclasses of tuple, they’re immutable as well. So if you try to change a value of a coordinate, then you’ll get an AttributeError.

d.Math Constants:

In Python programming mathematical constants can be utilized in different ways.These constants are available in predefined math Module.When These constants used in python programme, they returns the value that is equal to the standard defined value.The math module has a set of variety methods and constants that we can use frequently in our programme.
Math module has the following type of constants in python.
1.   math.e:  In math.e module 'e' is the Euler's number.
The math.e constants returns the standard eurler's number:2.71828182846.

# import the math module to use diferent constants
import math
#Display the value of Euler's number
print("Value of Eurler e =",math.e)

output:
Value of Eurler e =2.7182818284459045


2.   math.pi:  In math.e module 'pi' is the constant number. The standard value of pi is 22/7 which is approximately equal to 3.14159265359
it can be defined as the ratio of the circumference of a circle to the diameter of a circle.when we use this constant in our python program it return the float value 3.14159265359 which is known as mathematical constants PI
syntax:
math.pi
The following program returns the value of a math.pi constant using math module in python.

# import the math module to use diferent constants
import math
#Display the value of constant pi
print("The Value of PI =",math.pi)

output:
The Value of PI =3.14159265359


3.   math.tau:  In math.tau module 'tau' is the constant number. The standard value of tau is 6.283185307179586
it can be defined as the ratio of the circumference of a circle to the radius of the circle.
syntax.
math.tau
The following program returns the value of a math.tau constant using math module in python.

# import the math module to use diferent constants
import math
#Display the value of constant tau
print("The Value of tau =",math.tau)

output:
The Value of tau =6.283185307179586


4.   math.inf:  In math.inf module 'inf' is the constant number. The standard value of inf is positive infinity or negative infinity.
The value of positive infinity is returned by math.inf constant and -math.inf returns negative infinity.
The inf constant is also same as float('inf').
syntax.
math.inf
The following program returns the value of a math.inf and -math.inf constant using math module in python.

# import the math module to use diferent constants
import math
#Display the value of constnat positive infinity.
print("The Value of positive infinity =",math.inf)
#Display the value of constnat negative infinity.
print("The Value of negative infinity =",-math.inf)
output:
The Value of positive infinity =inf
The Value of negative infinity =-inf


5.   math.nan:  In math.nan module 'nan' is the floating point constant number value.This is not a valid number. The constant is equivalent to float("nan").
syntax.
math.nan
The following program returns the value of a math.nan constant.

# import the math module to use the nan constants
import math
#Display the value of constnat nan.
print("The Value of constant nan is =",math.nan)
output:
The Value of constant nan is =nan


Previous Topic:-->> Python Operators || Next topic:-->> Assignment statement