Skip to main content

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
  • solve MCQs involving int() on positive and negative floats

30-minute recording plan

  • 0-5 min: what data type means in Python
  • 5-12 min: scalar types (int, float, bool, str)
  • 12-18 min: type checking with type()
  • 18-26 min: type conversion (int, float, str, bool)
  • 26-30 min: MCQ traps (int(-x), floor vs truncation, conversion errors)

What is a data type?

A data type defines:
  • kind of value stored
  • operations allowed on that value
  • how Python interprets memory and behavior for 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.

MCQ critical: int() truncates toward zero

int(x) removes the fractional part and moves the value toward 0. It does not always move to the smaller integer. Examples:
print(int(7.99))    # 7
print(int(-7.99))   # -7
print(int(2.1))     # 2
print(int(-2.1))    # -2
Exam rule:
  • positive float -> decimal part dropped (3.8 -> 3)
  • negative float -> decimal part dropped toward zero (-3.8 -> -3)

Exactly your quiz pattern

If x is of form a.bcd where a, b, c, d are positive digits:
print(int(x))
print(int(-x))
Output pattern:
  • first line: a
  • second line: -a
Worked sample:
x = 4.278
print(int(x))    # 4
print(int(-x))   # -4

int() vs floor division idea

Do not mix these:
  • int(-2.9) is -2 (toward zero)
  • -2.9 // 1 is -3.0 (floor, toward negative infinity)
  • math.floor(-2.9) is -3

Type conversion and boolean

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

Truthiness trap: bool(input())

input() returns a string.
bool(string) is False only when the string is empty.
x = bool(input())
If user types False and presses Enter:
  • input() gives "False" (non-empty string)
  • bool("False") is True
  • so x becomes True
Quick truthiness check:
print(bool(1))      # True
print(bool(0.0))    # False
print(bool("0"))    # True
print(bool(" "))    # True (space is still non-empty)
print(bool(None))   # False
Exam trap summary:
  • bool("False") -> True
  • bool("0") -> True
  • bool(0) and bool(0.0) -> False
  • bool([]) and bool("") -> False

Beginner alert: bool and int relation

In Python, bool is related to integers internally:
print(True == 1)   # True
print(False == 0)  # True
print(True + True) # 2
This is language behavior, not a style to copy in beginner programs. Prefer clear logic with True/False, not numeric tricks.

Conversion error patterns

  • int("25") -> valid
  • int("-25") -> valid
  • int("+25") -> valid
  • int("25.0") -> ValueError
  • float("25.0") -> valid
  • int("abc") -> ValueError

Mini showcase: BMI classifier

weight = float(input("Weight in kg: "))
height = float(input("Height in meters: "))

bmi = weight / (height ** 2)
is_healthy = 18.5 <= bmi <= 24.9

print(f"BMI: {bmi:.2f}")
print(f"Healthy range: {is_healthy}")
Concepts used:
  • float input conversion
  • arithmetic with **
  • boolean expression with range check

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)
  • int() truncates toward zero for both positive and negative floats
  • int(-x) and math.floor(-x) can produce different outputs
  • for bool(input()), non-empty user input always becomes True

Practice questions

  1. Predict type and output:
x = 5
y = 2.0
print(x + y)
print(type(x + y))
  1. Predict output:
x = 9.876
print(int(x))
print(int(-x))
  1. For x = -4.2, compare:
  • int(x)
  • x // 1
  • math.floor(x)
  1. Write 3 examples where conversion fails and explain why.
  2. Predict output:
x = bool(input())
for each input:
  • False
  • 0
  • (just press Enter)