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.
Calculate a price including tax
Create a variable
prix_htequal to80, a variabletvaequal to0.2, then a variableprix_ttcequal to the price excluding tax increased by the VAT.prix_ht = 80 tva = 0.2 # Calculez prix_ttc prix_ttc = ...Integer division and remainder
You have
minutes = 137. Computeheures(number of whole hours) andreste(remaining minutes) with//and%, then build the textaffichagein the format"2h17"with an f-string.minutes = 137 heures = ... reste = ... affichage = ...Convert an input
The variables
a_texte = "12"andb_texte = "30"come from a form (they are text). Createsommecontaining the numeric sum of the two (42, an integer).a_texte = "12" b_texte = "30" somme = ...
End-of-lesson quiz
What is 17 % 5?
- 3
- 2
- 3.4
What is the type of the value 3.0?
- int
- str
- float
What does “3” + “4” give in Python?
- 7
- "34"
- An error
The rest of the course
- Conditions and boolean logic15 min
- Lists and loops20 min
- Functions: break down and reuse15 min
- Thinking algorithmically: from problem to program20 min