Python 4 Immutable Variables and Numerical Types #atharhashmisir

Subscribers:
3,800
Published on ● Video Link: https://www.youtube.com/watch?v=dhZ0OAIYZk8



Duration: 0:00
12 views
1


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.




Other Videos By Athar Hashmi Sir


2024-11-24Microsoft Office VS LibreOffice??? #atharhashmisir #wordprocessor #wordprocessing
2024-11-23⚡ Ctrl Key Hacks: Speed Up Your Work in MS Word 🖋️
2024-11-22Master Excel Charts in 1 Minute! #excelcharts #shorts
2024-11-21Mind It!!! 2 Tips of Excel #atharhashmisir #excel #exceltricks
2024-11-20Python 5 Operators in Python
2024-11-20Excel AutoSum in few Seconds!#excel #exceltricks #exceltips #atharhashmisir #shorts
2024-11-20#excel #exceltricks #exceltips #atharhashmisir #shorts
2024-11-20MS Excel Security #excel #exceltricks #exceltips #atharhashmisir #shorts
2024-11-18MS Word Keyboard Shortcuts #shorts #atharhashmisir #windows #msword
2024-11-18#shorts #atharhashmisir #windows
2024-11-17Python 4 Immutable Variables and Numerical Types #atharhashmisir
2024-11-16Python 03 Understanding Data Types and Variables#atharhashmisir
2024-07-24Thesaurus | Basics of Word Processing #atharhashmisir #thesaurus #pgdca #bca #dca #dtp #shortcut
2024-07-23References Tab in Microsoft Word: Part 01 - Creating a Table of Contents #atharhashmisir
2024-07-10How to Insert Roman, English Page Numbers in Same Word Document (Hindi mei) #atharhashmisir
2024-07-07Basics of Word Processing | Copy Paste From Internet | एम एस वर्ड | वर्ड प्रोसेसिंग #atharhashmisir
2024-05-26Research Paper Presentation, @aimsrandasbmmumbai #atharhashmisir
2024-05-20Water Crisis #atharhashmisir #savewater #water #diy
2024-05-13Flowchart using AI Tool #aitools #ai #flowchart #programming #atharhashmisir
2024-05-07DIY Toy #diy #toys #diytoys #atharhashmisir #experiment
2024-05-07How to solder EEPROM IC #atharhashmisir #eeprom #soldering #smd #repair