Operators
Operators are symbols or characters that perform specific operations on operands, such as variables or values.They allow us to manipulate data and perform various calculations or comparisons.
There are different types of operators in Python. In this week, we will learn about arithmetic operators and assignment operators. Next week, we will learn about comparison operators and logical operators. We will learn about more operators in future lessons.
Arithmetic Operators
+
performs addition-
performs subtraction*
performs multiplication/
performs division**
performs exponentiation%
performs modulo operation (the remainder after integer division)
What would happen?
Try to guess what each snippet would print out. Then, click the arrow to see an explanation.
print(5 + 10)
print(5 + 10)
It would print 15
.
First, python adds 5
and 10
, then it does the print
.
print(4 + 3 * 2)
print(4 + 3 * 2)
It would print 10
.
First, python multiplies 3
and 2
and gets 6
. Then it adds 6
to 4
, then it prints the final result.
There are other operators, like comparison, assignment, and logical operators. We will cover assignment operators in this lesson, and the rest in future lessons.
Using the Repl to explore
The best way to learn how the Python operators behave is to try out different operators and see what they do.
- Open a Python repl on your computer by entering
python
in a terminal - Set a timer for 5 minutes and try to find out as much as you can about the operators.
- Ask yourself questions, then try entering the code in the REPL to answer your question.