๐Ÿ“ฆ What is a Variable?

A variable is a named container for data. Python figures out the type automatically โ€” just assign a value with =.

name = value

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

int
34, -7, 1000
Whole numbers. No decimal point.
float
3.14, 45000.80
Numbers with a decimal point.
str
"Priya", 'Nairobi'
Text โ€” any sequence of characters.
bool
True, False
Yes/no, on/off, active/inactive.
NoneType
None
"No value yet" โ€” missing data.

๐Ÿฆ The Business Scenario

๐Ÿฆ
Banking ยท SouthBank India
Storing a New Customer's Profile

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.

5
Core Python types
1
Customer record
<1ms
To assign in memory

๐Ÿ’ป Step 1: Assign the Customer Profile

customer_profile.py โ–ถ Run to see output below
# Output Name : Priya Sharma Age : 34 Balance : 145,250.80 Active : True Loan ends : None

๐Ÿ” Step 2: Check Each Variable's Type

check_types.py โ–ถ Pre-computed output
# Output <class 'str'> <class 'int'> <class 'float'> <class 'bool'> <class 'NoneType'>

๐Ÿ”„ 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.

type_casting.py โ–ถ Common in data pipelines
# Output Age type : <class 'int'> Balance type : <class 'float'> Senior discount? False Balance: 145250.8

๐ŸŒ Also Used In

The Same Pattern Across Every Domain

๐Ÿ“ฑ
subscriber = {"name": str, "plan": str, "balance": float, "active": bool}
Telecom โ€” subscriber records in every billing system
๐Ÿฅ
policy = {"number": int, "premium": float, "claim_date": None}
Insurance โ€” policy details and unclaimed fields
๐Ÿšš
shipment = {"id": str, "weight_kg": float, "delivered": bool}
Logistics โ€” every parcel tracked the same way
๐Ÿค–
df.astype({"age": int, "score": float})
AI & ML โ€” Pandas does type casting on whole columns for training

๐Ÿค– 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.

pandas scikit-learn TensorFlow PyTorch

๐ŸŽฏ Mini Challenge

Build an Insurance Claim Record

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).

Start with 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:.
Python is dynamically typed โ€” you don't declare types when creating variables. Python figures them out at runtime. You can even reassign a variable to a different type: 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.
Use 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

Question 1 of 5
What type does Python assign to 3.0?
Question 2 of 5
Which is a valid Python variable name?
Question 3 of 5
What does type(True) return?
Question 4 of 5
What happens when you run int("hello")?
Question 5 of 5
Which correctly stores "no loan yet" in a customer record?
0
/5

๐Ÿ“‹ Quick Cheatsheet

TypeCreateCast toUse Case
intx = 42int("42")Age, count, year
floatx = 3.14float("3.14")Balance, rate, score
strx = "hello"str(42)Name, city, ID
boolx = Truebool(1)Active flag, eligibility
NoneTypex = Nonen/aMissing or unset value
type()type(x)โ€”Check a variable's type

โš ๏ธ Common Mistakes

Confusing None with "None"
None is NoneType. "None" is a string. They are not equal. Always use bare None without quotes for missing values.
Using == instead of is for None
Write if x is None:, not if x == None:. is checks identity โ€” which is what you want. Some custom classes override == in unexpected ways.
Adding a string and a number without casting
"Age: " + 34 raises TypeError. Cast first: "Age: " + str(34), or use f-strings: f"Age: {34}" โ€” they handle conversion automatically.
Shadowing built-in type names
Don't name a variable 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
  • None means "no value" โ€” always check with is None, never == None
  • f-strings (f"...") are the cleanest way to embed variables in text
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’