MyMisPal
← Python lessons
Lesson 3 of 13

Operators and f-strings

add, subtract, multiply+ - *
division (always a float)/
floor division / remainder// %
power**
PYTHON
print(17 / 5)
print(17 // 5)
print(17 % 5)
print(2 ** 10)

Comparisons return True or False, and you can combine them with and, or and not.

PYTHON
print(5 > 3, 5 == 3, 5 != 3)
print(True and False, True or False, not True)

An f-string is the easiest way to mix text and values: put f before the quotes and variables inside { }. You can format too: :.2f for two decimals, :, for thousands separators.

PYTHON
name = "Omar"
salary = 9500
print(f"{name} earns {salary} SAR")
print(f"Yearly: {salary * 12:,}")
print(f"VAT 15%: {salary * 0.15:.2f}")

Exercises

Exercise 1

A product costs 200 and VAT is 15%. Print the final price with two decimals: 230.00.

Exercise 2

You have minutes = 125. Print hours and minutes like: 2 h 5 min.

Spotted a mistake on this page, or have an idea to improve the site?