Variables and Data Types in Python
Programs become useful when they can remember information.
Imagine an online shopping application. It might need to remember:
- Customer name
- Product price
- Quantity
- Whether payment was completed
Python uses variables to give names to values.
Variables as Labeled Boxes
Imagine a room filled with boxes. Each box has a label.
For example:
┌─────────────────┐
│ age │
│ │
│ 21 │
└─────────────────┘
The label is: age
The value inside is: 21
In Python, you can create this using:
age = 21Think of it as: "Create a name called age and associate it with the value 21."
Creating Variables
Here are some examples:
name = "Aarav"age = 21price = 99.50is_logged_in = TrueWe have created four names: name, age, price, is_logged_in.
Each refers to a value. A useful mental picture is:
name→"Aarav"age→21price→99.50is_logged_in→True
What Is a Data Type?
A data type tells Python what kind of value something represents.
Think about objects in your home. You might have: Book, Bottle, Chair, Phone. They are all objects, but they have different purposes.
Similarly, Python values have different types. For beginners, four important types are:
- Integer
- Float
- String
- Boolean
1. Integer
An integer is a whole number without a decimal part.
age = 21count = 10temperature = -5Examples of integers: 0, 1, 10, 100, -20. There is no decimal point.
2. Float
A float represents a number containing a decimal component.
price = 99.50height = 5.8temperature = 36.5The decimal point tells us that these are floating-point numbers.
Think about money: 99 and 99.50. Both are numbers, but the second has a decimal component.
3. String
A string represents text.
name = "Aarav"city = "Pune"message = "Welcome to Python"The quotes tell Python that the value is text.
Remember:
age = 21 is a number.
But age = "21" is text.
They may look similar to a human, but Python treats them differently.
4. Boolean
A boolean represents one of two logical states: True or False.
is_logged_in = TrueThis could mean: The user is currently logged in.
has_paid = FalseThis could mean: The payment has not been completed.
Booleans are extremely useful when programs need to make decisions.
Seeing the Types
Python provides a built-in function called type().
For example:
age = 21print(type(age))Python reports that age is an integer.
Similarly:
name = "Aarav"print(type(name))Python reports that name is a string.
You can experiment with:
print(type(10))print(type(10.5))print(type("Hello"))print(type(True))Python Is Dynamically Typed
One of Python's beginner-friendly features is dynamic typing.
What does that mean? In Python, you do not normally need to declare the type of a variable before assigning a value.
You can simply write: age = 21 (Python determines that 21 is an integer).
You can write: name = "Aarav" (Python determines that "Aarav" is a string).
You can write: price = 99.99 (Python determines that the value is a float).
The basic idea is:
You provide the value → Python determines its type
Dynamic Typing vs Static Typing
To understand dynamic typing, compare it with a statically typed language such as Java.
In Java, you commonly write:
int age = 21;String name = "Aarav";You explicitly specify the type.
Python allows:
age = 21name = "Aarav"Python determines the types from the values.
| Python | Java |
|---|---|
age = 21 | int age = 21 |
| Type is inferred | Type is explicitly declared |
| Dynamically typed | Statically typed |
This does not mean Python has no types. Python absolutely has types. It means that the type information does not usually need to be written in the variable declaration.
Can a Variable Change Type?
Yes.
value = 10Here, value refers to an integer.
Later:
value = "Hello"Now value refers to a string.
Conceptually:
First: value → 10
Later: value → "Hello"
This flexibility is one feature of dynamic typing. However, changing the meaning of a variable repeatedly can make programs confusing. As a beginner, try to give variables clear and consistent purposes.
Variables Are Names, Not Boxes in Memory
The "labeled box" analogy is useful, but there is a more accurate mental model.
Think of a variable as a name attached to an object or value.
For example: age = 21
You can imagine:
age ───────→ 21
The name age lets your program refer to that value. This becomes particularly important when we study Python objects and memory in more depth.
a = 10Why This Matters: In Python, names are just references to heap objects. If you assign `b = a` and modify a mutable object (like a list or dict), both variables change because they point to the identical heap address!
A Type Map
Here is a simple overview:
graph TD
A[Python Values] --> B[Integer]
A --> C[Float]
A --> D[String]
A --> E[Boolean]
B --> F[21]
C --> G[99.50]
D --> H[Hello]
E --> I[True / False]
Using Variables
Once you store information in a variable, you can use that variable later.
name = "Aarav"print(name)Output:
Aarav
You can also perform calculations:
price = 100quantity = 3total = price * quantityprint(total)Output:
300
The variable total now represents the result.
Choosing Good Variable Names
Good names make programs easier to understand.
Prefer:
student_name = "Aarav"total_price = 500number_of_items = 3Instead of unclear names:
x = "Aarav"a = 500n = 3Short names are sometimes useful, but descriptive names are generally easier to understand.
Think of variable names as labels on boxes. Which is easier to understand?
Box A or Customer Details
The same principle applies to programming.
[!TIP] Key Takeaways
- A variable gives a name to a value.
- Python variables can refer to different kinds of values.
- An integer is a whole number.
- A float represents a decimal number.
- A string represents text.
- A boolean represents True or False.
- Python is dynamically typed.
- Python automatically determines the type of a value when you assign it.
- The
type()function can tell you the type of a value.- Good variable names make programs easier to understand.
Beginner rule: When you see
name = value, read it as "the name now refers to this value."