Skip to content
CCC Python Course

Values, types, variables and print

Module
M1.1
Lesson
1 of 1
Reading time
4 min

In this lesson

  • Tell integers, floats, strings and booleans apart.
  • Store values in variables with the assignment operator.
  • Follow how reassignment changes what a variable refers to.
  • Print values in exactly the format you intend.

A program works with pieces of data: a number of plates, a price, a name, an answer. Python calls each piece of data a value. This lesson shows you the four kinds of value you will use most, how to give a value a name, and how to show it on the screen.

Four core types

Every value in Python has a typeThe classification of a value, such as an integer, string, or boolean, that determines what operations it supports.In the glossary. The type decides what you can do with the value. You can multiply two numbers, but you cannot multiply two pieces of text.

An integer, or int, is a whole number such as 0, 42 or -7. Python integers can be as large as you need. A number with hundreds of digits works the same way as a small one.

A float, or float, is a number with a decimal point, such as 3.5 or -0.25. Floats are stored with limited precision, so they can be off by a tiny amount. Contest problems usually stick to integers for that reason.

A string, or str, is text inside quotes, such as "sushi" or 'red'. Single and double quotes mean the same thing. Pick one style and keep using it.

A boolean, or bool, is either True or False. Those are the only two boolean values. Both start with a capital letter.

If you are not sure what type a value has, ask Python. The type() function tells you. The program below uses variables, which the next section explains. For now, read count = 42 as "call this value count".

examples/value_types.py
count = 42price = 3.5label = "42"ready = Trueprint(type(count))print(type(price))print(type(label))print(type(ready))

Output

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Asking Python for the type of four values

Look at label. Its value "42" is a string, because it is inside quotes. It looks like a number, but Python treats it as text. The next program shows why that matters.

examples/quotes.py
print(7 + 5)print("7" + "5")

Output

12
75
The same symbols as numbers and as text

Adding two integers gives 12. Adding two strings joins them, so you get 75. The quotes changed the type, and the type changed the result.

Variables and assignment

A variableA name that refers to a value stored while a program runs.In the glossary is a name that refers to a value. You create one with assignmentStoring a value under a variable name using the equals sign.In the glossary, which uses the = sign.

Python
plates = 3player = "Jordan"is_open = True

Read plates = 3 as "the name plates now refers to 3". In maths, = says two things are equal. In Python it does something else. It takes the value on the right and attaches the name on the left to it.

Think of the name as a tag. Python creates the value first, then ties the tag to it. Whenever you write plates later, Python follows the tag to the value 3.

A name can use letters, digits and the underscore character. It cannot start with a digit, and it cannot contain spaces. Python also reserves some words for itself, such as if, for and def, so you cannot use those as names.

Names are case-sensitive. count, Count and COUNT are three different names. This causes a common mistake: you create one spelling and then use another.

examples/name_error.py
count = 5print(Count)

Error: NameError

Traceback (most recent call last):
  File "name_error.py", line 2, in <module>
    print(Count)
NameError: name 'Count' is not defined. Did you mean: count?
Using a name that was never created

The program created count but asked for Count. Python has no value tagged Count, so it stops with a NameError. When you see this error, check the spelling and the capital letters first.

Choose names that say what the value means. total_cost tells you more than t or x2. Write names in lowercase, with an underscore between words.

Reassignment

You can assign a new value to a name that already exists. This is called reassignmentBinding an existing variable name to a new value.In the glossary. Python moves the tag from the old value to the new one.

examples/reassignment.py
total = 10print(total)
total = total + 5print(total)
tag = "box"print(total, tag)
Assigning and reassigning variables

Line 4 is worth a slow read: total = total + 5. Python always works out the right side first. It follows total to 10, adds 5 and gets 15. Only then does it move the tag total to 15.

Step through the program below. Watch the name total move from 10 to 15 while the printed lines build up underneath the code.

1total = 10
2print(total)
3
4total = total + 5
5print(total)
6
7tag = "box"
8print(total, tag)
Output so far
(nothing printed yet)
Frames and objects, step 1 of 7FramesGlobal frame
Speed
Python starts at the top of the file. Line 1 runs first.
  • Just changed

Figure 1Tracking variable assignment and reassignment

Read the steps as text

The program assigns 10 to total, prints it, increases total by 5, prints 15, and finally prints total alongside the string tag box.

  1. Python starts at the top of the file. Line 1 runs first.
  2. Line 1 runs: total is created with the value 10. total is created and points to integer 10.
  3. Line 2 runs: it prints 10.
  4. Line 4 runs: total changes from 10 to 15. total is updated to refer to 15.
  5. Line 5 runs: it prints 15.
  6. Line 7 runs: tag is created with the value 'box'. tag refers to the string "box".
  7. Line 8 runs: it prints 15 box. The program has finished: no lines are left to run.

Printing output

The print() function writes a line of text to the screen. In a contest, that text is your answer.

You can give print() several values separated by commas. It prints them in order with one space between each pair. When it finishes, it moves to a new line, so the next print() starts on a line of its own. A print() with nothing inside prints an empty line. A string prints without its quotes: print("Done") shows Done. The quotes only tell Python where the text starts and ends.

examples/print_parts.py
plates = 3price = 5print("Plates:", plates)print("Total:", plates * price)print()print("Done")

Output

Plates: 3
Total: 15

Done
Several values on one line, then an empty line

The judge compares your output with the expected output exactly. An extra word, a missing space or a label like Total: counts as a wrong answer, even when the number is right. Print only what the problem asks for.

Reading input into variables

In a contest, your program gets its data from standard input. The input() function reads one line and gives it to you as a string. That is true even when the line holds a number.

To use the line as a number, convert it with int(). The step-through below reads two lines. The first becomes an integer and the second stays a string.

Input and program, step 1 of 342CupcakeInput (stdin)Program\n\ncount = int(input())item = input()Variables
Speed
The input waits on standard input, one line per row. Nothing has been read yet; line 1 of the program runs next.
  • Current
  • Queued
  • Done
  • Not reached
  • Just changed

Figure 2How input values enter variables during execution

Read the steps as text

Standard input holds 2 line(s). Each call to input() takes the next line, without its newline, and the program stores it in a variable.

  1. The input waits on standard input, one line per row. Nothing has been read yet; line 1 of the program runs next.
  2. Line 1 ran: input() reads the first line, '42', and drops its newline. int() turns the text into the number 42, and count refers to it. Line 2 runs next.
  3. Line 2 ran: input() reads the second line, 'Cupcake', and drops its newline. item refers to that text: it is a string, even if it looks like a number. The program ends here.

The module on reading input covers this in more detail. For now, you have enough to read a few numbers, store them in variables and print a result. The problems below need exactly that.

Practice

Try these on the judge. Each link opens the problem on WMOJ.

  1. 2024 J1
    Conveyor Belt Sushi (opens on WMOJ in a new tab) WMOJ

    Calculate the total cost from plate counts and fixed prices.

  2. 2022 J1
    Cupcake Party (opens on WMOJ in a new tab) WMOJ

    Compute leftover cupcakes from regular and small boxes.