Python Data Types
Explore core data types (str, int, float, bool), inspect values with type(), and convert between types safely.
After completing this chapter, you will be able to:
- Understand why programming languages categorize data into types.
- Work with Python's four foundational scalar types: str, int, float, and bool.
- Inspect any value or variable's type using type().
- Convert values between data types safely using int(), float(), str(), and bool().
What Are Data Types?
In programming, a data type tells Python what kind of information a value represents and what operations are allowed on it. For example, you can multiply two numbers, while adding two strings joins text together.
For Python fundamentals, these four built-in types are essential:
| Type | Name | Example | Description |
|---|---|---|---|
str | String | "Hello" | Text inside quotes |
int | Integer | 42, -7 | Whole numbers without decimals |
float | Float | 3.14, -0.5 | Numbers with a decimal point |
bool | Boolean | True, False | Logical truth values |
Checking Types with type()
Whenever you are unsure what type a variable holds, pass it to the built-in type() function.
Each built-in value reports its exact class name.
name = "Python"
lessons = 9
rating = 4.9
is_beginner_friendly = True
print(type(name))
print(type(lessons))
print(type(rating))
print(type(is_beginner_friendly))Expected Output
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Mixing Types Carefully
Notice how quotes change the data type completely! "25" (with quotes) is a string, whereas 25 (without quotes) is an integer.
"25" + "5"joins two strings into"255".25 + 5adds two integers into30."25" + 5causes aTypeErrorbecause Python will not guess whether you want text joining or math addition.
Using + between a string and an int raises a TypeError. Convert the number using str() or pass comma-separated values to print().
age = 15
print("Age: " + age)age = 15
print("Age: " + str(age))Type Conversion (Casting)
When you need to change a value from one type to another, Python provides built-in conversion functions:
int(x)— Converts a whole-number string or truncates a float into an integer.float(x)— Converts an integer or numeric string into a decimal number.str(x)— Converts numbers or booleans into text strings.bool(x)— Converts a value intoTrueorFalse.
Type conversion lets you do math on text digits or combine numbers with text.
text_num = "18"
whole = int(text_num)
decimal = float(whole)
print(whole + 2)
print(decimal)
print("Value is " + str(whole))Expected Output
20 18.0 Value is 18
Float to int truncation
Calling int(4.9) does not round up—it removes everything after the decimal point and returns 4. Converting non-numeric text like int("abc") raises a ValueError.
Quick Check
What will type(7.0) output in Python?
Chapter Summary
- Python's core beginner data types are
str(text),int(whole numbers),float(decimal numbers), andbool(True/False). - Use
type(value)to check any value's data type. - Use
int(),float(),str(), andbool()to convert between types explicitly.