Python 4 Immutable Variables and Numerical Types #atharhashmisir
Immutable Variables and Numerical Types
________________________________________
What Are Immutable Variables?
Immutable variables are variables whose values cannot be changed once assigned. In Python, strings, tuples, and numbers are immutable. This means that once you assign a value to a variable, you cannot alter its value directly. For example:
name = "Aarav"
name[0] = "T" # Error
In the above code, trying to modify the first character of the string "Aarav" results in an error. This is because strings are immutable in Python. Immutability ensures that data remains consistent and prevents accidental changes.
________________________________________
Why Immutability Matters?
Immutability is important for various reasons:
1. Security: Immutable variables prevent accidental or malicious modification of data. For example, once you assign a sensitive value like an Aadhaar number to a variable, it cannot be changed without explicitly creating a new variable. This ensures that sensitive data remains protected.
2. Multithreading: In applications where multiple processes or threads access the same data, immutability prevents data inconsistencies. For instance, in financial applications, multiple threads may access the same account balance. Using immutable variables ensures that the balance is not carelessly changed during processing.
________________________________________
Understanding Numerical Types
In Python, numerical types represent different kinds of numbers. There are three primary types:
1. Integer (int): Represents whole numbers. Example: 25 (age of Priya).
2. Float (float): Represents decimal numbers. Example: 75.5 (height of Aman in cm).
3. Complex (complex): Represents complex numbers, often used in advanced engineering calculations. Example: 2 + 3j.
Example code:
python
Copy code
age = 25 # Integer
height = 165.5 # Float in cm
distance = 2 + 3j # Complex number for engineering
print(type(age), type(height), type(distance))
Output:
arduino
Copy code
class 'int' class 'float' class 'complex'
________________________________________
Working with Numerical Types
Understanding numerical types is crucial for performing arithmetic operations and type conversions in programming.
Example 1: Type Conversion
Type conversion is often required when you need to convert a value from one type to another. For instance, you might need to convert an integer into a float or vice versa.
python
Copy code
x = 100 # Total marks
y = float(x) # Convert to percentage (out of 100.0)
z = int(99.99) # Round-off a value
print(y, z)
Example 2: Arithmetic Operations
Numerical types are used in arithmetic operations. For instance, you can calculate savings or yearly salary using integers and floats.
python
Copy code
salary = 50000 # Monthly salary of Aman
expenses = 20000 # Monthly expenses of Priya
savings = salary - expenses
print(savings) # Savings
print(salary * 12) # Annual salary
print(salary // expenses, salary % expenses) # Floor division and remainder
________________________________________
Immutable Numerical Types
Numerical types in Python are immutable. This means when you modify the value of a variable, Python creates a new object with the updated value rather than modifying the original one.
For example, let’s consider a scenario where Aman’s salary is ₹30,000 and he gets a ₹5,000 raise. Instead of changing the original salary, Python creates a new object with the updated value.
python
Copy code
salary = 30000
print(id(salary)) # Memory address
salary += 5000 # Increment salary
print(id(salary)) # New memory address
In this code, the memory address of salary changes after the increment, showing that Python creates a new object for the updated value.
________________________________________
Practical Applications
Immutable variables and numerical types are widely used in real-world applications.
• Data Security: Immutable variables are ideal for storing sensitive information like Aadhaar numbers or phone numbers, where it is critical that values remain unchanged.
• Financial Calculations: Numerical types are used in applications such as loan calculators, tax computations, and salary calculations. For instance, calculating the monthly EMI for a loan using numerical types ensures that the values are accurate.
Example of an EMI calculator:
python
Copy code
principal = 500000 # Loan amount
rate = 0.07 # Annual interest rate
time = 5 # Years
emi = (principal * rate * time) / 12
print("EMI per month:", emi)
This example calculates the monthly EMI for a loan of ₹5,00,000, showing how numerical types are crucial for handling financial data.
________________________________________
By understanding immutable variables and numerical types, you will be able to write more efficient, secure, and error-free programs. These concepts form the foundation for handling data in a variety of applications, from basic calculations to complex financial systems.