| layout | default |
|---|---|
| title | Variables and Data Types |
| parent | Lessons |
| nav_order | 2 |
| permalink | /lessons/variables-and-data-types/ |
| course_lesson | true |
| course_index | 02 |
| previous_page | /lessons/fundamentals/ |
| previous_title | Python Fundamentals |
| next_page | /lessons/type-conversion/ |
| next_title | Type Conversion |
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.”
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.
age = 28
temperature_change = -4Use integers for counts, indexes, and whole-number measurements.
price = 149.50
average = 82.75Floating-point arithmetic can contain tiny precision differences. For beginner calculations, it is enough to understand that floats represent approximate decimal values.
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.
is_logged_in = True
has_paid = FalseBooleans are often produced by comparisons and used in conditions.
employee_code = "007"The quotes make this text. Keeping a code as a string preserves leading zeroes.
middle_name = NoneNone 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 setUse 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.
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.
product_name = "Notebook"
product_code = "N-007"
unit_price = 45.50
quantity = 4
in_stock = True
supplier_note = NoneEach type matches the meaning of the field. product_code is text even though it contains digits.
Optional preview: values that can and cannot change
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.
city = Chennai
print(city)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.
x = "Notebook"
y = 45.50The 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.50Optional 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.
- 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.
Try these problems on this page. For every answer, write down why you chose the data type.
- 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.
Design a data model for an online order using at least eight variables. Add a comment explaining the type chosen for each value.
Choose suitable types for a person's name, age, salary, employee code, account status, and missing middle name. Explain each choice.