Python lesson
Maths
* means times and / means divide. total holds 140, so total / 2 shows 70.0: dividing always gives a decimal.
Try this lesson free →Example
Use + - * and / on numbers, or on variables that hold numbers:
price = 35
qty = 4
total = price * qty
print(total)
print(total / 2)140
70.0Key points
+ - * /Add, subtract, multiply, divide.
total = price * qtyWork out the answer, then store it in a new variable.
(a + b) * 2Brackets are worked out first, just like in maths.
Put together: profit = revenue - costs stores the answer, and print(profit) shows it.
Practice
1. Build a line that stores price times qty in a variable called total.
total = price * qty2. Build a line that shows revenue minus costs.
print(revenue - costs)3. Revenue was 120 and costs were 45. Work out the profit in a variable called profit, then print it.
revenue = 120
costs = 45
profit = revenue - costs
print(profit)4. Two orders were worth 70 and 35. Print their total.
print(70 + 35)5. price is 35 and qty is 3. Print the total with VAT added: (price * qty) * 1.075.
price = 35
qty = 3
print((price * qty) * 1.075)Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →