Challenge 1 +10 XP
Print the type of the whole number 100.
- Test 1 — expects "<class 'int'>"
Need a hint? (−25% XP)
Wrap 100 in type(), then wrap that in print().
Show solution (0 XP)
print(type(100)) Every piece of data in Python is a value, and every value has a type — a kind. Knowing the type tells you what you can do with the value. Three types cover almost everything you’ll use at first. Run this and change the values:
print(42)
print(3.14)
print("hello")42 3.14 hello
Those are the three you’ll meet constantly:
int — whole numbers, like 42, 0, -7.float — numbers with a decimal point, like 3.14, 5.0, -0.5.str — text (“string”), written in quotes, like "hello".You don’t have to guess a value’s type — the built-in type() function tells you. Edit
the value inside type(...) and run it to inspect anything you like:
print(type(42))<class 'int'>
print(type(3.14))<class 'float'>
print(type("hello"))<class 'str'>
Read <class 'int'> as simply “this is an int.” type() only reports the type — it
never changes the value.
Here’s the one that trips up almost every beginner. What is 10 / 2? You’d say 5 — and
you’re right about the number, but not the type:
print(10 / 2)5.0
It’s 5.0, not 5. The division operator / always gives back a float, even when
the numbers divide evenly. Prove it:
print(type(10 / 2))<class 'float'>
Keep this in mind — a value that looks like a whole number can still be a float.
Putting digits in quotes makes them text, not a number:
print(type("42"))<class 'str'>
"42" is a str; 42 is an int. They look similar but they are different types — and
Python treats them very differently. Because of that, you can’t just add text to a number:
print("5" + 5)TypeError
Python raises a TypeError: it won’t guess whether you meant to join text ("55") or
add numbers (10). You have to be explicit — which is exactly what the next lessons build
toward.
10 / 2 is 5.0 (a float), never 5 (an int). Division always produces a float."3.14" (in quotes) is a str, while 3.14 (no quotes) is a float. The quotes, not
the characters, decide the type.What does 10 / 2 evaluate to in Python?
5.0 — a float — The / operator always returns a float, so 10 / 2 is 5.0 even though 10 divides evenly by 2. Its type is float.
Are "42" and 42 the same thing in Python?
No — "42" is a str (text) and 42 is an int (a number) — Quotes make a value a string. "42" is text; 42 is a number. They are different types and behave differently.
What does type() do?
Reports what type a value already is — type() only inspects — it tells you the type of a value and never changes the value itself.
What happens when you run print("5" + 5)?
Python raises a TypeError — "5" is a string and 5 is an int. Python won't guess whether you meant to join text or add numbers, so it raises TypeError.
Print the type of the whole number 100.
Wrap 100 in type(), then wrap that in print().
print(type(100)) On the first line print the result of 5 divided by 2. On the second line print the type of that result.
5 / 2 is 2.5 — and remember what type division always produces.
print(5 / 2)
print(type(5 / 2))