Learning outcomes
By the end of this chapter, you should be able to:- explain
int,float,bool, andstr - 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 Python5-12 min: scalar types (int,float,bool,str)12-18 min: type checking withtype()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.
float
Decimal numbers.
bool
Logical values True and False.
str
Text data.
Checking type
Type conversion
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:
- positive float -> decimal part dropped (
3.8->3) - negative float -> decimal part dropped toward zero (
-3.8->-3)
Exactly your quiz pattern
Ifx is of form a.bcd where a, b, c, d are positive digits:
- first line:
a - second line:
-a
int() vs floor division idea
Do not mix these:
int(-2.9)is-2(toward zero)-2.9 // 1is-3.0(floor, toward negative infinity)math.floor(-2.9)is-3
Type conversion and boolean
Truthiness trap: bool(input())
input() returns a string.bool(string) is False only when the string is empty.
False and presses Enter:
input()gives"False"(non-empty string)bool("False")isTrue- so
xbecomesTrue
bool("False")->Truebool("0")->Truebool(0)andbool(0.0)->Falsebool([])andbool("")->False
Beginner alert: bool and int relation
In Python, bool is related to integers internally:
True/False, not numeric tricks.
Conversion error patterns
int("25")-> validint("-25")-> validint("+25")-> validint("25.0")->ValueErrorfloat("25.0")-> validint("abc")->ValueError
Mini showcase: BMI classifier
floatinput conversion- arithmetic with
** - boolean expression with range check
Exam-focused points
type()returns class/type of object- invalid conversion raises
ValueError boolis a subclass of integer in Python behavior (True == 1,False == 0)int()truncates toward zero for both positive and negative floatsint(-x)andmath.floor(-x)can produce different outputs- for
bool(input()), non-empty user input always becomesTrue
Practice questions
- Predict type and output:
- Predict output:
- For
x = -4.2, compare:
int(x)x // 1math.floor(x)
- Write 3 examples where conversion fails and explain why.
- Predict output:
False0- (just press Enter)
