02 - Variables and Data Types
A simple picture
A variable is like a name sticker on a box. The sticker helps us find the value inside. The data type tells Python what kind of value it is, just as a label may say “books,” “toys,” or “water.”
Variables
A variable is a readable name attached to a value. Python creates or updates the name when you assign with =.
customer_name = "Meera"
items = 3
print(customer_name, items)
A variable does not permanently lock a type:
value = 10
value = "ten"
This flexibility is useful, but clear programs usually keep one meaning for a variable.
Common built-in types
int - whole numbers
age = 28
temperature_change = -4
Use integers for counts, indexes, and whole-number measurements.
float - decimal numbers
price = 149.50
average = 82.75
Floating-point arithmetic can contain tiny precision differences. For beginner calculations, it is enough to understand that floats represent approximate decimal values.
complex - real and imaginary parts
signal = 3 + 4j
print(signal.real, signal.imag)
Complex numbers are used in scientific and engineering calculations; they are not needed for most business programs.
bool - true or false
is_logged_in = True
has_paid = False
Booleans are often produced by comparisons and used in conditions.
str - text
employee_code = "007"
The quotes make this text. Keeping a code as a string preserves leading zeroes.
None - absence of a value
middle_name = None
None is not zero, an empty string, or False. It means that a value is missing or not available. Check it with is None.
Optional preview: types used in later courses
These additional types are useful for files, networks, and advanced APIs. You do not need them in the Basic projects:
raw_data = b"ABC" # bytes: fixed binary data
editable_data = bytearray(b"ABC") # bytearray: changeable binary data
unique_values = frozenset({1, 2, 3}) # frozenset: an unchangeable set
Use ordinary strings, lists, sets, and dictionaries for the Basic projects. Binary data and immutable sets are useful when working with files, networks, or advanced APIs.
Inspecting types
print(28, type(28))
print(19.5, type(19.5))
print(True, type(True))
print("Python", type("Python"))
print(None, type(None))
type() is useful while learning and debugging. In larger programs, choose clear data models rather than repeatedly checking types.
Real-world example
product_name = "Notebook"
product_code = "N-007"
unit_price = 45.50
quantity = 4
in_stock = True
supplier_note = None
Each type matches the meaning of the field. product_code is text even though it contains digits.
Optional preview: values that can and cannot change
Mutable and immutable values
Numbers, booleans, strings, and tuples cannot be changed in place. Lists and dictionaries can be changed; they are introduced later. This difference matters when multiple names refer to the same collection.
Important fact: quotation marks change meaning.
25is an integer that can be used as a quantity;"25"is text made from the characters2and5.
Bug Hunter
Bug 1 — missing quotation marks
city = Chennai
print(city)
Bug 2 — a code is not a quantity
student_code = 007
print(student_code)
Python 3 does not allow a decimal integer literal with a leading zero. A student code is an identifier, so store it as text.
Bug 3 — unclear meaning
x = "Notebook"
y = 45.50
The code runs, but another learner cannot easily understand the names.
Show Bug Hunter fixes
city = "Chennai"
student_code = "007"
product_name = "Notebook"
unit_price = 45.50
Optional deeper look: how does Python know a type?
Every Python value is an object that remembers its own type. A variable name does not have a permanently fixed type; the name refers to an object, and the object has the type. That is why type(value) can inspect the value at runtime.
Common mistakes
- Treating a phone number as a number and losing leading zeroes.
- Using
0when “not provided” should beNone. - Assuming a decimal is always exact.
- Giving vague names such as
xanddatawhen the meaning is known.
Practice
Try these problems on this page. For every answer, write down why you chose the data type.
Problems
- Choose types for a name, age, salary, employee code, active status, and missing value.
- Print the type of an integer, decimal, boolean, string, and
None. - Store the length and width of a rectangle using numeric variables, then print both types.
- Store a phone number that begins with zero without losing that zero.
- Store whether an item is available using the boolean value
True. - Predict the type of six different values before using
type()to check. - Explain why a postal code and a quantity may look similar but need different types.
- Create variables for a product record and display all fields.
- Store a missing middle name with
None, then print the value and its type. - Design the data types for a student record and justify each choice.
Show hints
- Think about the operations each field needs.
- Use
type(value). - Whole measurements can use
int; measurements with decimal parts can usefloat. - Put the phone number inside quotes.
- Use a clear name such as
is_availableand storeTrue. - Quoted values are strings; comparisons produce booleans.
- A postal code is an identifier, not a quantity to calculate.
- Use clear names such as
product_nameandunit_price. - Use
middle_name = None, then printmiddle_nameandtype(middle_name). - Make a small table with field, type, and reason.
Show solution ideas
- Use
str,int,float,str,bool, andNonerespectively. - Put each value into
print(type(value)). - Store values such as
length = 10andwidth = 4.5, then usetype(). phone_number = "0123456789".free_delivery = order_total >= free_delivery_limit.42isint,4.2isfloat,Trueisbool, text isstr, andNoneisNoneType.- A postal code must preserve formatting; a quantity is used in arithmetic.
- Store the fields in separate variables and use an f-string.
if value is None: print("Missing").- Choose types based on meaning and describe the reason in comments.
Homework
Design a data model for an online order using at least eight variables. Add a comment explaining the type chosen for each value.
Checkpoint
Choose suitable types for a person’s name, age, salary, employee code, account status, and missing middle name. Explain each choice.