Skip to main content
Anime study buddy

Learning outcomes

By the end of this chapter, you should be able to:
  • explain int, float, bool, and str
  • check data types using type()
  • convert values safely between basic types

What is a data type?

A data type defines:
  • kind of value stored
  • operations allowed on that value

Core scalar types

int

Whole numbers, positive or negative.
x = 42

float

Decimal numbers.
y = 6.75

bool

Logical values True and False.
is_pass = True

str

Text data.
name = "Python"

Checking type

x = 12
print(type(x))

Type conversion

a = int("25")
b = float("2.5")
c = str(100)
Careful:
# int("2.5")  # ValueError
Use int(float("2.5")) if needed.

Type conversion and boolean

bool(0)      # False
bool(1)      # True
bool("")     # False
bool("abc")  # True

Exam-focused points

  • type() returns class/type of object
  • invalid conversion raises ValueError
  • bool is a subclass of integer in Python behavior (True == 1, False == 0)

Practice questions

  1. Predict type and output:
x = 5
y = 2.0
print(x + y)
print(type(x + y))
  1. Take a decimal input and print integer part.
  2. Write examples where conversion fails and explain why.