Values and types
Programming becomes easier when you ask a simple question: “What kind of value is this?” Python keeps track of types so operations can behave predictably.
str, int, float and bool and understand why the same-looking characters can behave differently.Four useful built-in types
str represents text such as "42". int represents a whole number such as 42. float represents a floating-point value such as 42.5. bool represents True or False. The exact capitalisation of True and False matters.
The type() function lets you inspect a value. Notice that quotation marks make "42" a string even though its characters look like a number.
Why types matter
Adding two integers calculates a numerical total. Adding two strings joins their text. Python does not automatically interpret every numeric-looking string as a number.
Converting with int(), float() or str() makes your intention explicit. A conversion can fail if the input does not make sense; later we will teach exception handling.
Booleans express a yes/no fact
True and False are boolean values. They are often returned by comparisons and will be important when we reach conditional logic.
For now, we only need to recognise them and distinguish them from strings such as "True".
Try it yourself.
Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.
type() identifies what each value represents.
Press Run Python to execute the editable code.
Add a new line to inspect the type of False.
Need a hint?
Use the same type(...).__name__ pattern.
On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.
Check your understanding.
Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.
Which type is
"100":strorint?View answer
"100"has typestr, because the quotes make it text.100without quotes is anint. The characters can be similar but the operations they support differ.Why can
int("twenty")raise an exception?View answer
int()parses appropriate numeric text, such as"100", into an integer. The text"twenty"is not written in the accepted integer syntax, soint("twenty")raisesValueError; error handling is taught later.
Where this leads.
Now that we recognise values, we can give them names and make programs easier to change.