Introduction
Imagine you’re at a grocery store, and you have different types of items in your cart:
- Apples (whole numbers, like 5 apples)
- Milk (a decimal quantity, like 1.5 liters)
- A note that says "Buy eggs" (text)
- A reminder that you already bought bread (True or False)
Just like that, Python has different data types to store different kinds of values!
Integer (Whole Numbers)
An integer (or int
) is a whole number—no decimals, no fractions, just pure, solid numbers.
Example:
age = 25
apples = 10
print(type(age)) # Output: <class 'int'>
Integer values can be positive, negative, or even zero!
score = -50 # Negative integer
count = 0 # Zero is also an integer!
Float (Decimal Numbers)
A float (or float
) is a number that has decimals. Think of it as the fancy cousin of int
who likes precision.
Example:
price = 19.99
pi = 3.14159
print(type(price)) # Output: <class 'float'>
Floats are useful when you need precision, like measuring weight or calculating scientific formulas.
temperature = -2.5 # Yes, it can be negative!
height = 180.5 # A person’s height
String (Text & Characters)
A string (or str
) is just text inside quotes. You can use either single ('
) or double ("
) quotes.
Example:
name = "Alice"
greeting = 'Hello, world!'
print(type(name)) # Output: <class 'str'>
Strings can hold anything from names, messages, to even entire books!
sentence = "Python is fun!"
quote = 'Life is short, use Python!'
You can even combine strings using concatenation:
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name) # Output: John Doe
Boolean (True or False)
A boolean (or bool
) is like a light switch—only two options: True or False.
Example:
is_python_fun = True
is_sky_green = False
print(type(is_python_fun)) # Output: <class 'bool'>
Booleans are useful in decision-making (like checking if a user is logged in).
logged_in = True
if logged_in:
print("Welcome back!")
else:
print("Please log in.")
Conclusion
Python’s basic data types are simple but powerful:
- Integers (
int
) – Whole numbers (e.g.,5
,-10
) - Floats (
float
) – Decimal numbers (e.g.,3.14
,-2.5
) - Strings (
str
) – Text data (e.g.,"Hello"
,'Python'
) - Booleans (
bool
) – True/False values (e.g.,True
,False
)
With these, you can start writing awesome Python programs!
0 Comments