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:

  • 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:

  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:

  • 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 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

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

Rashed Hossain
Rashed Hossain

As an experienced WordPress professional, I specialize in theme and plugin development with a robust background in product and project management. My commitment lies in delivering top-notch solutions that align with client needs and surpass expectations.

Articles: 14