Skip to main content
CodeFlow
control flow~12 minRoute 01 2 of 12

Data Types

What kind of value is in the box — numbers, words, true/false, lists.

Prerequisites:Variables

Story — the recycling sorter

7"Asha"Trueintstrboollist

A conveyor belt rolls past a row of bins labeled int, string, bool, list. A small robot picks up each thing as it goes by and drops it into the bin that matches what it is. A number into the int bin. A word in quotes into the string bin. A checkmark into the bool bin.

Try to put a string into the int bin and a buzzer goes off. That is a type — a tag that says what kind of value this is, so the language knows what operations make sense.

Sorter says

The label on the bin is not just decoration. It is the rule for what fits.

See it — sort the belt

Press Step to advance one item. The last item is a deliberate mismatch — watch what happens.

Belt
7"Asha"True[9, 7, 8]"3" + 5
int
    string
      bool
        list
          Belt presents: 7.
          Text-only trace

            Try it — declare three values

            The four lines below set up three differently-typed values and print them. Put them in a working order.

            Drag the pieces — or tab to one and use the / buttons — to put the program in order.

            1. is_open = True
            2. name = "Asha"
            3. age = 7
            4. print(age, name, is_open)
            Hint: Declare each value before you print, in any sensible order.

            Code it — five languages, one idea

            The same four declarations in five popular languages. Notice how static languages (Java, C++) write the type next to the name; dynamic ones (Python, JavaScript, Go) infer it from the value.

            age = 7              # int
            name = "Asha"        # str
            is_open = True       # bool
            scores = [9, 7, 8]   # list
            print(type(age), type(name))

            Walk through it line by line

            Each click of Next declares one variable. Watch the type tags grow.

            Step 1 / 5python
            1age = 7
            2name = "Asha"
            3is_open = True
            4scores = [9, 7, 8]
            5print(type(age), type(name))

            Quiz it — make it stick

            1. Question 1

              Which of these values is a string?

            2. Predict — Question 2

              In Python, what does this line do?

              print("3" + 5)
            3. True or false — Question 3

              A boolean variable can hold any small whole number.

            No Dumb Questions

            Real questions other learners asked on this page.

            • Why does Python not need me to write the type, but Java does?
              Python is dynamically typed — it figures out the type from the value you assigned (7 is clearly an int). Java is statically typed — it wants you to commit up front so the compiler can catch mistakes before the program runs. Both work; both have trade-offs.
            • What is the difference between an int and a float?
              An int is a whole number (1, 42, -7). A float has a decimal point (3.14, -0.5). Floats can represent fractions but with tiny rounding errors, which is why money is usually stored as integers (cents) rather than floats.
            • Why do strings need quotes?
              Quotes are how the computer tells "the word name" apart from "the variable named name". Without quotes, `name` is a label; with quotes, `"name"` is a piece of text. Single or double quotes both work in most languages.