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