1. What is a Variable?
In Python, a variable is a symbolic name (tag) that references an object in memory.
name = "Sandeep"
age = 25
salary = 50000
Unlike some languages where variables are fixed "boxes" storing a specific type, Python creates an object in memory and binds the name tag to it using the = assignment operator:
2. 🔥 Python is Dynamically Typed
Python is dynamically typed. You do not declare a data type beforehand (e.g., no int x = 10;). Python automatically inspects the assigned value at runtime.
x = 10
type(x) # Returns: <class 'int'>
x simply pointed to a different object in memory. In statically typed languages (like C++ or Java), reassigning a string to an integer variable would cause a compile error.3. 🔥 Core Built-in Data Types
Every value you analyze in Python belongs to a specific built-in type:
| Type Name | Category | Example | Data Analytics Meaning |
|---|---|---|---|
int | Integer (Whole Number) | age = 25 | Customer count, quantity sold, user IDs |
float | Floating-Point (Decimal) | price = 99.99 | Currency, percentages, conversion rates |
complex | Complex Number | z = 2 + 3j | Signal processing & advanced engineering |
bool | Boolean (True / False) | is_active = True | Customer churned flag, filter conditions |
str | String (Text Sequence) | name = "Sandeep" | Customer names, product SKUs, categories |
NoneType | Null / Absence of Value | result = None | Missing data, unpopulated database fields |
4. 🔥 Live Lab: Identify the Data Type
Task: Match each variable to its correct Python data type. Pay special attention to the difference between 25 and "25":
5. 🔥 The type() Function
Python provides the built-in type() function to inspect the exact data type of any value or variable:
>>> type(age)
<class 'int'>
6. 🔥 Live Lab: Data Type Investigation
In real-world data analytics (e.g. reading CSV files with Pandas), columns often load with unexpected types. Inspect this sample record:
| Field | Sample Value | Visual Appearance | Python Type | Calculations Allowed? |
|---|---|---|---|---|
customer_id | "1094" | Looks like number | str | ❌ No (Text only) |
order_total | 249.99 | Decimal | float | ✅ Yes (Math enabled) |
items_purchased | 3 | Whole number | int | ✅ Yes (Math enabled) |
7. Reassigning Variables
Variables can be reassigned new values at any time during execution:
score = 95
print(score) # Output: 95
The name score is simply rebound to the new integer object 95.
8. Multiple Assignment
Python allows clean, one-line assignments for multiple variables:
x, y, z = 10, 20, 30
# 2. Same value to multiple variables
x = y = z = 0
9. Type Conversion (Casting)
Convert values between types using Python's built-in constructor functions:
int("25") ➔ 25Converts text digits to integer
float("99.50") ➔ 99.5Converts text decimals to float
str(100) ➔ "100"Converts any value to text string
bool(1) ➔ True, bool(0) ➔ FalseConverts truthy/falsy values
10. 🔥 Live Lab: Converting Raw Dataset Values
Suppose an API returns all numbers as text strings: sales = "50000", quantity = "10", price = "99.50":
sales = "50000"
quantity = "10"
price = "99.50"
# Attempting calculation directly on strings:
total_val = quantity * price # ❌ TypeError: can't multiply sequence by non-int of type 'str'
11. 🔥 Common Type Confusion (Strong Typing)
Python is strongly typed at runtime. It will never silently guess whether you intended numeric addition or text concatenation:
30 # Integer Addition
12. Variable Naming Rules (PEP 8)
Follow official Python identifier rules and the PEP 8 style guide:
age = 25employee_name = "Amit"(snake_case)total_sales_2026 = 50000_is_calculated = True
2name(Cannot start with a digit)employee-name(Hyphens parsed as subtraction)classorfor(Reserved Python keywords)total sales(Spaces not allowed)
13. Good Naming Practices for Analytics
Self-documenting code prevents analytical mistakes. Prefer meaningful names over cryptic single letters:
| Poor / Ambiguous Name | Professional Data Analytics Name | Why It Matters |
|---|---|---|
x = 50000 | total_sales = 50000 | Immediately reveals business metric |
n = 1420 | customer_count = 1420 | Clarifies count population |
avg = 65000.0 | average_salary = 65000.0 | Prevents mixing up averages |
14. Mini Data-Analytics Practical Example
Connecting variables, types, and basic arithmetic in a realistic order invoicing script:
price = 60000 # int
quantity = 3 # int
discount = 0.10 # float (10% discount)
subtotal = price * quantity # 180,000 (int)
discount_val = subtotal * discount # 18,000 (float)
final_total = subtotal - discount_val # 162,000 (float)
15. 🔥 Final Interactive Challenge
Task: You are given an ingested raw customer record where all fields were imported as strings. Rectify the data types:
employee_name = "Rahul" # Target: str (Keep as text)
age = "28" # Target: int
salary = "55000.50" # Target: float
is_active = "True" # Target: bool
# Click "Execute Data Cleaning Code" to rectify the types!
16. Common Mistakes & Traps
Quoted numbers are strings. Running "25" + "25" results in "2525" instead of 50.
1st_quarter throws a SyntaxError. Use quarter_1 instead.
total_sales and Total_Sales are two completely separate variables in Python.
17. Assessment Certification Quiz
Verify your understanding of variables, dynamic typing, built-in types, and type conversions:
Python Variables & Data Types Certification Quiz
Answer all 5 questions to test your practical mastery of Python 3 variables and data types.
1. In Python, what is a variable fundamentally?