Site icon Rashed Hossain

Variables and Data Types inPython Programming

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:

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:

  1. Integer (int)
  2. Float (float)
  3. String (str)
  4. 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:

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:

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:


✅ 4. Booleans (bool)

Booleans are a special data type with only two possible values:

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 TypeExample ValuesDescriptionUse Cases
Integer10, -3, 2024Whole numbersAge, year, count, quantity
Float3.14, 99.99Decimal numbersPrice, weight, scientific data
String"Hello", '123'Text or charactersNames, messages, text data
BooleanTrue, FalseYes/No, On/Off, Logic checksConditions, 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


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)!

Exit mobile version