Python 3 — Variables & Core Data Types

🐍 Python 3 🟢 Lesson 2 📅 July 2026

Think of a variable as a storage box with a label stuck to it. You write a label on the box (the variable name), put something inside it (the value), and then look it up whenever you need it. Python handles variables dynamically, meaning you don't have to state what type of data goes in the box beforehand.

1 Creating Variables

To store a value in a variable, we use the assignment operator (=). The variable name goes on the left, and the value goes on the right:

Python 3 — Variable Assignment ▶ Run Code
# Storing data in variables
username = "Balaji"
age = 25
height = 5.9
is_developer = True

# Printing the variables
print(username)
print(age)
print(height)
print(is_developer)
2 Core Data Types

Every piece of data stored in Python belongs to a specific category. Here are the 4 fundamental types you must know:

TypeClass NameDescriptionExample
Textstr (String)Unicode characters wrapped in quotes"Python 3"
Whole Numberint (Integer)Positive or negative whole values-42
Decimalfloat (Float)Numbers with decimal points3.1415
Booleanbool (Boolean)Logical state representing true/falseTrue or False

You can check the type of any variable using the built-in type() function:

Python 3 — Checking Types ▶ Run Code
score = 99
print(type(score))  # Outputs: <class 'int'>

is_online = False
print(type(is_online))  # Outputs: <class 'bool'>
3 Dynamic Typing in Python

In languages like Java or C++, once you declare a variable as an integer, you can never store a string inside it. Python is **dynamically typed**, meaning variables can change their type easily as you assign new values:

Python 3 — Dynamic Types ▶ Run Code
data = 100
print(type(data))  # <class 'int'>

data = "Now I am text!"
print(type(data))  # <class 'str'>
⚠️ Variable Naming Rules:
  • Variable names can only contain letters, numbers, and underscores (e.g., user_1).
  • They must **never** start with a number (e.g., 1user is invalid).
  • Use standard **snake_case** (all lowercase words connected by underscores) to follow clean Python guidelines (PEP 8).
  • Do not use reserved keywords like if, else, class, or print as variable names.
4 Coding Challenge

Write a program that declares a variable representing a product price, discount rate, product name, and whether it is in stock. Output their values and their types to the terminal.