01 / 21
int str bool float

Python Types
int, float, bool, str

Master the four fundamental data types in Python: understand how the interpreter classifies every value and why it matters for your code.

Python

What Is a Data Type?

Every value in Python belongs to a type. A type tells the interpreter what kind of data you are working with and what operations are allowed on it. Think of types as labels that determine the rules.

42 int 3.14 float True bool "hi" str

Why Types Matter

Python is dynamically typed: you don't declare types explicitly, but the interpreter tracks them behind the scenes. Knowing types prevents bugs and unlocks the right operations.

1
Operations depend on type. The + operator adds numbers but concatenates strings.
2
Mixing types causes errors. You cannot add an int to a str without conversion.
3
Debugging gets easier. When output looks wrong, checking types is the first step.

Dynamic Typing in Action

A variable in Python is just a name that refers to a value. The same name can point to different types at different times.

x = 10 int x = 3.14 float x = "hello" str Line 1 Line 2 Line 3 The variable x changes type each time it is reassigned. Python does not lock a variable to one type.

The int Type

An int (integer) represents whole numbers with no decimal point. Python integers have unlimited precision: they can be as large as your memory allows.

>>> age = 17 >>> type(age) <class 'int'> >>> 2 ** 100 1267650600228229401496703205376 >>> -42 # negative integers too >>> 0 # zero is an int

The float Type

A float stores decimal (floating-point) numbers. Internally, Python uses 64-bit IEEE 754 representation, which means floats have limited precision.

>>> pi = 3.14159 >>> type(pi) <class 'float'> >>> 10 / 3 # division always returns float 3.3333333333333335 >>> 0.1 + 0.2 0.30000000000000004 # precision quirk!

The bool Type

A bool (boolean) has exactly two possible values: True and False. Booleans are the backbone of conditions and control flow in Python.

>>> is_student = True >>> type(is_student) <class 'bool'> >>> 5 > 3 True >>> 10 == 7 False

The str Type

A str (string) is a sequence of characters, enclosed in single quotes '...' or double quotes "...". Strings are immutable: once created, their contents cannot change.

>>> name = "Alice" >>> type(name) <class 'str'> >>> len(name) 5 >>> name[0] # indexing starts at 0 'A' >>> name + " B." # concatenation 'Alice B.'

int vs float: Key Difference

int
Exact whole numbers. Unlimited size. No decimal point. Example: 42
float
Approximate decimals. Fixed 64-bit precision. Has a decimal point. Example: 42.0
>>> type(42) # <class 'int'> >>> type(42.0) # <class 'float'> >>> 42 == 42.0 # True (equal value) >>> 42 is 42.0 # False (different objects)

Type Conversion

Python provides built-in functions to convert between types. This is sometimes called casting.

type() check int int() float float() str str() bool bool() int(3.9) → 3 float(3) → 3.0

How to Check and Convert

1
Check the type. Use type(value) or isinstance(value, int) to inspect what you have.
2
Decide if conversion is needed. Are you mixing types in an operation? Does input() return a string you need as a number?
3
Cast with the target type. Use int(), float(), str(), or bool() as a function call around the value.
4
Handle invalid conversions. int("hello") raises a ValueError. Always validate user input first.

Example 1: Basic Expressions

Predict the type and value of each expression, then verify with type().

# Expression 1: integer arithmetic >>> 7 + 3 10 # int + int → int # Expression 2: division always gives float >>> 7 / 2 3.5 # int / int → float # Expression 3: floor division stays int >>> 7 // 2 3 # int // int → int # Expression 4: mixing int and float >>> 5 + 2.0 7.0 # int + float → float

Example 2: String Operations

The + operator behaves differently for strings than for numbers. Let's explore.

# Concatenation: joining strings >>> "Hello" + " " + "World" 'Hello World' # Repetition: multiply string by int >>> "ha" * 3 'hahaha' # TypeError: cannot add str and int >>> "age: " + 17 TypeError: can only concatenate str to str # Fix: convert the int to str first >>> "age: " + str(17) 'age: 17'

Example 3: Boolean Logic

Every Python value has a truthiness: it can be interpreted as True or False in a boolean context.

# Truthy values (evaluate to True) >>> bool(42) # True >>> bool(-1) # True >>> bool("hello") # True >>> bool(3.14) # True # Falsy values (evaluate to False) >>> bool(0) # False >>> bool(0.0) # False >>> bool("") # False (empty string) >>> bool(None) # False

Common Mistakes

✕ Forgetting input() returns str
Writing age = input() then age + 1 causes a TypeError. Cast first: int(input()).
✕ Using / instead of //
The / operator always returns a float, even for 10 / 2 (gives 5.0). Use // for integer division.
✕ Comparing types with ==
Writing type(x) == int works but isinstance(x, int) is preferred: it handles subclasses like bool.

Summary Table

Type Example Mutable? Key Operation
int 42, -7, 0 Immutable +, -, *, //, **, %
float 3.14, -0.5 Immutable +, -, *, /, round()
bool True, False Immutable and, or, not
str "hello", '42' Immutable +, *, [ ], .upper()

Question 1 of 5

What is the type of the expression 10 / 5 in Python?

Question 2 of 5

What does type(True) return?

Question 3 of 5

Which of the following will cause a TypeError?

Question 4 of 5

What is the value of bool("")?

Question 5 of 5

What does int(7.9) return?