COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-003 / Understanding data◷ 16 min read + practice

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.

By the end, you can: Distinguish str, int, float and bool and understand why the same-looking characters can behave differently.
Before this lesson: PY-002

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.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Inspect different types

type() identifies what each value represents.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

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.

  1. Which type is "100": str or int?

    View answer

    "100" has type str, because the quotes make it text. 100 without quotes is an int. The characters can be similar but the operations they support differ.

  2. 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, so int("twenty") raises ValueError; error handling is taught later.

Where this leads.

Now that we recognise values, we can give them names and make programs easier to change.