Variables, types and operations

First lesson of the Programming Basics and Algorithms course, free to read: about 15 minutes, 3 exercises and 3 quiz questions. This is the catalogue's base lesson; in the student space, it is rewritten for your profile.

Objectives

  • Understand what a program and an instruction are
  • Create variables and choose the right type (integer, decimal, text, boolean)
  • Perform calculations and display a result with print and f-strings

VariablesBasic typesArithmetic operators

A program is a sequence of instructions that the computer executes in order, from top to bottom, like a cooking recipe. In this course we use Python, a readable and widely used language, but the ideas apply to every language.

Variables: labels on values

A variable is a name that refers to a value kept in memory. You create it with the = sign, which means “store this value under this name” (it is an assignment, not a mathematical equality).

prix = 12.5          # a decimal number
quantite = 3         # an integer
produit = "Cahier"   # text, between quotes
en_stock = True      # a boolean: True or False

quantite = quantite + 1   # we replace the old value: quantite is now 4

Choose meaningful names, in lowercase, with _ between words (prix_total rather than x). A name cannot start with a digit or contain a space.

Basic types

Every value has a type, which determines what you can do with it:

Type Example Usage
int 42 whole numbers
float 3.14 decimal numbers (a dot, not a comma)
str "Bonjour" text (string of characters)
bool True true / false

The type() function lets you check: type(prix) returns <class 'float'>.

Operations

a = 17
b = 5
print(a + b)    # 22  addition
print(a - b)    # 12  subtraction
print(a * b)    # 85  multiplication
print(a / b)    # 3.4 decimal division (always a float)
print(a // b)   # 3   integer division (quotient)
print(a % b)    # 2   modulo (remainder of the division)
print(a ** 2)   # 289 power

The modulo % is very useful: n % 2 == 0 tells you a number is even. Precedence follows the rules of math: 2 + 3 * 4 equals 14, and parentheses change the order.

Displaying a result

print() displays a value. To mix text and variables, the f-string is the clearest method:

total = prix * quantite
print(f"{quantite} x {produit} = {total} euros")   # 4 x Cahier = 50.0 euros
print(f"Rounded total: {total:.2f}")               # 2 decimal places

Watch out for types

With text, + glues strings together instead of adding: "3" + "4" gives "34". And "3" + 4 raises a TypeError. User input (input()) is always text: you must convert it with int() or float() before calculating.

age_texte = "30"
age = int(age_texte)    # conversion: 30 (int)
print(age + 1)          # 31

Key takeaway: name your variables well, check their type, and convert when necessary.

Exercises

Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.

  1. CodeDifficulty 1Python in the browser1 hint

    Calculate a price including tax

    Create a variable prix_ht equal to 80, a variable tva equal to 0.2, then a variable prix_ttc equal to the price excluding tax increased by the VAT.

    prix_ht = 80
    tva = 0.2
    # Calculez prix_ttc
    prix_ttc = ...
  2. CodeDifficulty 1Python in the browser2 hints

    Integer division and remainder

    You have minutes = 137. Compute heures (number of whole hours) and reste (remaining minutes) with // and %, then build the text affichage in the format "2h17" with an f-string.

    minutes = 137
    heures = ...
    reste = ...
    affichage = ...
  3. CodeDifficulty 2Python in the browser2 hints

    Convert an input

    The variables a_texte = "12" and b_texte = "30" come from a form (they are text). Create somme containing the numeric sum of the two (42, an integer).

    a_texte = "12"
    b_texte = "30"
    somme = ...

End-of-lesson quiz

  1. What is 17 % 5?

    • 3
    • 2
    • 3.4
  2. What is the type of the value 3.0?

    • int
    • str
    • float
  3. What does “3” + “4” give in Python?

    • 7
    • "34"
    • An error

The rest of the course

  1. Conditions and boolean logic15 min
  2. Lists and loops20 min
  3. Functions: break down and reuse15 min
  4. Thinking algorithmically: from problem to program20 min

Programming Basics and Algorithms · Beginner · 2 h 25 min