๐ฆ What is a Variable?
A variable is a named container for data. Python figures out the type automatically โ just assign a value with =.
In plain English: give a piece of data a label so you can refer to it later.
Python has 5 core types you'll use in almost every program: int, float, str, bool, and NoneType.
๐ฏ The 5 Core Data Types
๐ฆ The Business Scenario
A Hyderabad-based bank's onboarding system captures a new customer's details before writing them to the database. Every field needs the right data type โ names as strings, age as integers, balance as floats, active flag as boolean, and any not-yet-set field as None.
๐ป Step 1: Assign the Customer Profile
๐ Step 2: Check Each Variable's Type
๐ Step 3: Type Casting (Real CSV Data)
Real data from CSVs and APIs often arrives as strings, even when it represents numbers. You have to convert โ this is type casting.
๐ Also Used In
The Same Pattern Across Every Domain
๐ค AI & Machine Learning Connection
Every machine learning dataset is a table of typed data. When you load a CSV with Pandas and call df.astype(), it's doing the same type casting you just did โ but on millions of rows at once. Variables and types are the foundation every AI model is built on.
๐ฏ Mini Challenge
Create 5 variables for an insurance claim: claimant_name (str), claim_amount (float), policy_number (int), is_approved (bool), and settlement_date = None. Print each with its type. Then create a display label: "POL-" + str(policy_number).
claimant_name = "Chidi Okafor", then claim_amount = 45000.50. Policy number stays as int. For is_approved = True or False. Settlement date is None until paid. Remember: str(int_value) converts for concatenation.
๐ค Interview Questions
int stores whole numbers (34, -7, 1000). float stores numbers with decimals (3.14, 145250.80). Use int for counts and ages, float for money and measurements. Note: dividing two ints with / always returns a float; use // for integer division.None represents "no value yet". Its type is NoneType. Use it for fields that haven't been filled in (loan_end_date = None), or for functions that don't explicitly return a value. Always check with if x is None:, never if x == None:.x = 5 then x = "hello" is valid. This is flexible but can cause bugs. Compare with Java where int x = 5; means x can only ever hold integers.float("3.14"). Possible exceptions: ValueError if the string can't be parsed (float("hello")), TypeError if you pass None. In production pipelines, wrap in try/except or use pd.to_numeric(series, errors='coerce') which converts invalid values to NaN instead of crashing.== checks if two values are equal. is checks if two variables point to the same object in memory. For None, always use is: if x is None:. For custom objects, == can be overridden via __eq__; is always checks identity. CPython caches small integers and short strings so is sometimes works by accident โ don't rely on it.๐ง Quick Quiz
3.0?type(True) return?int("hello")?๐ Quick Cheatsheet
| Type | Create | Cast to | Use Case |
|---|---|---|---|
int | x = 42 | int("42") | Age, count, year |
float | x = 3.14 | float("3.14") | Balance, rate, score |
str | x = "hello" | str(42) | Name, city, ID |
bool | x = True | bool(1) | Active flag, eligibility |
NoneType | x = None | n/a | Missing or unset value |
type() | type(x) | โ | Check a variable's type |
โ ๏ธ Common Mistakes
None with "None"None is NoneType. "None" is a string. They are not equal. Always use bare None without quotes for missing values.== instead of is for Noneif x is None:, not if x == None:. is checks identity โ which is what you want. Some custom classes override == in unexpected ways."Age: " + 34 raises TypeError. Cast first: "Age: " + str(34), or use f-strings: f"Age: {34}" โ they handle conversion automatically.str, int, float, list, or type. Doing so overwrites the built-in function โ breaking every str() call after that line.โฆ Key Takeaways
- Variables are named containers โ assign with
=, no declaration keyword needed - Python has 5 core types:
int,float,str,bool,NoneType - Use
type()to check what type a variable holds - Type casting (
int(),float(),str()) is essential when loading real data from CSVs Nonemeans "no value" โ always check withis None, never== None- f-strings (
f"...") are the cleanest way to embed variables in text
You've learnt variables.
Now go from syntax to salary.
Join the Best AI Program: The AI-Era Analyst โ 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Analyst โ