Summary
In this lesson, you learned that:
- A program is a series of commands that run one after another, from top to bottom.
print()prints to the output. Several values separated by commas are printed on one line, with spaces between them:print("Age:", 25)printsAge: 25.print()on its own prints an empty line.- Text (a string) is written in quotes, numbers without them.
- The arithmetic operators are
+,-,*and/. Multiplication and division are done before addition and subtraction; parentheses change the order./always gives a decimal number:6 / 2is3.0. //is whole-number division and%the remainder:7 // 3is2,7 % 3is1. A number is even whennumber % 2is0.- Decimal numbers are written with a dot:
1.5. - Comments start with
#; the computer ignores the rest of the line. You can also use them to switch code off (comment it out). - Variables store values under a name:
score = 0. You change the value by storing a new one:score = score + 1, or shorter,score += 1(andscore -= 1). - Names are written in snake_case (
first_name), can't start with a digit or contain spaces, and uppercase and lowercase letters matter. - Every value has a type:
int(a whole number),float(a decimal number) orstr(a string).+adds numbers and joins strings, but a string and a number can't be joined:"5" + 3ends with aTypeError.str(),int()andfloat()convert values. - F-strings insert values into text:
f"I am {age} years old." - An error message is read from the bottom: the last line gives the type of the error and its description; above it, you'll find the number of the line where the error happened.
SyntaxErrormeans that the code is written wrongly, and nothing runs;NameErrormeans an unknown name;TypeErrormeans something the type of a value doesn't allow.
In the next lesson, Conditions, your programs will learn to make decisions: to do one thing or another, depending on the values of variables.
Instructions
- 1
Before you go on, try it all out on a small task: a receipt from a coffee shop.
Create a variable
totalwith the price of two coffees and one cake. Calculate it fromcoffee_priceandcake_price. - 2
Using an f-string, print the last line of the receipt:
Total: 11.5 EUR