A data type tells Python what kind of value a variable holds. Different data types allow different operations.
x = 10
y = "Hello"
z = True
int)Integers are whole numbers (positive, negative, or zero).
age = 25
temperature = -5
count = 0
Common operations:
print(10 + 5)
print(10 - 3)
print(4 * 2)
print(10 // 3)
float)Floats are numbers with decimal points.
price = 19.99
pi = 3.14
average = 7.5
print(5 / 2)
print(2.5 + 1.5)
str)Strings store text and must be inside quotes.
name = "Alice"
greeting = 'Hello'
sentence = "Python is fun!"
String operations:
print("Hello" + " World")
print("Hi" * 3)
bool)Booleans represent True or False. They are often used in conditions.
is_logged_in = True
has_permission = False
print(5 > 3)
print(10 == 5)
Use the type() function to check a value’s data type.
print(type(10))
print(type(3.14))
print(type("Hello"))
print(type(True))
You can convert between data types using built-in functions.
x = "10"
y = int(x)
z = float(y)
s = str(z)
int("abc") will cause an error.
Mixing incompatible data types can cause errors.
print(5 + 3) # Works
print("5" + "3") # Works
print(5 + "3") # Error
int → whole numbersfloat → decimal numbersstr → textbool → True or Falsetype() checks data type