Beginner·5 min·basics · types
Variables & Types
You’ll be able to
- Assign values to variables using Python's dynamic typing
- Recognize the four core types: int, float, str, bool
- Convert between types safely with
int(),str(),float() - Use
type()to inspect what type a variable holds
Why this matters
Type confusion is the #1 bug in beginner code and a favorite interview trap. Real jobs using pandas or FastAPI expect type coercion instincts, because a CSV column read as str will silently break every sum() and comparison downstream.
Common pitfalls
- Using
input()and comparing to a number:input()returnsstr, sox == 5is alwaysFalse. Wrap withint(). - Reassigning built-in names like
list = [1,2,3]shadows the type and breaks laterlist(...)calls. - Assuming
int('3.5')works. It raisesValueError; usefloat('3.5')first, thenint().
Python figures out the type for you, but you should still know what you're holding.
The basics
- int — whole numbers (
27) - float — decimals (
1.78) - str — text (
"Ada") - bool —
TrueorFalse
type(x).__name__ tells you what type a value is.
Try it
- Add a
cityvariable and print it. - What happens if you add
age + height? Does the type change?
Practice
3 graded exercises
Write the code, click Check. We’ll tell you exactly what to fix.
Progress0 / 3
- Exercise 1
Create an integer variable
ageequal to30, then print it. - Exercise 2
Create
name = "Ada"andage = 30, then use one f-string to print exactlyAda is 30 years old. - Exercise 3
The variable
raw_priceholds the string"499". Convert it to an integer, add100(a shipping fee), and print the total.