When you write a program, you’re basically teaching the computer how to handle information. To do that, you need a way to store, identify, and manipulate data โ and thatโs where variables and data types come into play.
Letโs explore what they are, why they matter, and how they work.
๐ง What is a Variable?
A variable is like a labeled box that stores data for your program. You can give the box any name you want (like name
, price
, age
), and later use that box to access or change the data inside.
๐น Real-Life Analogy:
Imagine a school:
- The principal keeps a file for each student.
- That file contains student data like name, age, grade, and pass/fail status.
In programming:
student_name = "Ayesha"
student_age = 20
student_grade = 4.5
has_passed = True
Each of these variables holds a different type of data. But how does the computer know what type it is?
๐ฆ What is a Data Type?
A data type tells the computer what kind of data you’re storing in the variable. It helps the computer decide what it can or canโt do with that data.
There are many data types, but here we will focus on 4 common primitive data types:
- Integer (
int
) - Float (
float
) - String (
str
) - Boolean (
bool
)
๐ข 1. Integers (int)
Integers are whole numbers, either positive or negative โ no decimal points.
๐น Examples:
age = 25
year = 2025
temperature = -10
๐น What you can do with integers:
- Add, subtract, multiply, divide.
- Use them for counting, IDs, loops, math calculations, etc.
total_items = 3 + 7
print(total_items) # Output: 10
๐ฃ 2. Floats (float)
Floats (or floating-point numbers) are numbers that contain a decimal point.
๐น Examples:
height = 5.8
price = 99.99
pi = 3.1416
๐น Where floats are used:
- When precision is important (e.g., prices, distances, weights).
- Useful in scientific or financial calculations.
price = 50.75
discount = 10.50
final_price = price - discount
print(final_price) # Output: 40.25
๐ 3. Strings (str)
A string is a sequence of characters or text, written inside quotes (" "
or ' '
).
๐น Examples:
name = "Rashed"
message = 'Welcome to Python!'
address = "House 23, Dhaka"
Strings can include letters, numbers, symbols, and spaces โ but everything inside the quotes is treated as text.
๐น String operations:
- Combine strings:
first = "Hello" last = "World" full = first + " " + last print(full) # Output: Hello World
- Get the length of a string:
print(len("Rashed")) # Output: 6
- Access specific characters:
name = "Ayesha" print(name[0]) # Output: A print(name[-1]) # Output: a
โ 4. Booleans (bool)
Booleans are a special data type with only two possible values:
True
False
Booleans are used to represent yes/no, on/off, pass/fail, or any kind of binary situation.
๐น Examples:
is_raining = True
has_passed = False
Booleans are often used in conditions and logic:
age = 20
is_adult = age >= 18
print(is_adult) # Output: True
๐ Summary Table
Data Type | Example Values | Description | Use Cases |
---|---|---|---|
Integer | 10 , -3 , 2024 | Whole numbers | Age, year, count, quantity |
Float | 3.14 , 99.99 | Decimal numbers | Price, weight, scientific data |
String | "Hello" , '123' | Text or characters | Names, messages, text data |
Boolean | True , False | Yes/No, On/Off, Logic checks | Conditions, decisions |
๐ป Practice Example in Python
Try running this in Python:
name = "Tanvir"
age = 30
height = 5.9
is_student = False
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is a student:", is_student)
Output:
Name: Tanvir
Age: 30
Height: 5.9
Is a student: False
๐ Final Notes
- You donโt need to “declare” the data type in Python โ it automatically detects it based on the value.
- Choosing the correct data type is important. For example, donโt store a number as a string if you need to perform math.
Let me know if you want a PDF version, a quiz to test your understanding, or the next lesson (like conditional statements, lists, or loops)!