Data Types
Data Types
Every value in Python has a type. Data types help Python understand what kind of data it is working with and what operations can be performed on that data.
Why Data Types Matter
Imagine trying to add a number to a sentence or divide a name by 2. Python uses data types to prevent invalid operations and to process data correctly.
Common Python Data Types
String (str)
Used for text.
name = "Alex"
message = "Welcome to Python"
Integer (int)
Used for whole numbers.
age = 25
year = 2026
Float (float)
Used for decimal numbers.
price = 19.99
height = 5.8
Boolean (bool)
Used for True or False values.
is_logged_in = True
has_paid = False
Checking a Data Type
Use the type() function.
name = "Alex"
print(type(name))
Output:
<class 'str'>
Type Conversion
Sometimes you need to convert one type into another.
age = "25"
age = int(age)
print(age + 5)
Output:
30
Common Conversion Functions
str()→ convert to stringint()→ convert to integerfloat()→ convert to floatbool()→ convert to boolean
Code Along
name = "Maya"
age = 20
height = 1.65
student = True
print(type(name))
print(type(age))
print(type(height))
print(type(student))
Mini Challenge
Create four variables:
- Your name
- Your age
- Your height
- Whether you like Python
Print each value and its type.
Real World Use Case
An online store uses strings for product names, integers for stock quantity, floats for prices, and booleans for availability.
Quiz
- Which type is used for text?
- What type is
3.14? - What are the two boolean values?
- What function checks a value’s type?
Assignment
Create a script with at least six variables using different data types. Print each variable and its type.
Summary
You learned what data types are, why they matter, common Python types, how to check types, and how to convert between them.