< previous | Table of Contents | next> |
2.5 OPERATORS AND OPERANDS
We can use operators to create simple expressions or combine them to build complex ones. Operators are unique symbols that represent actions like addition +
, subtraction -
, multiplication *
, and division /
. The values or variables that operators work on are known as operands. Parentheses ()
can be used to group operators, the same as how you would do in your maths class.
NUMERIC OPERATORS
- Addition
+
adds two numbers. - Subtraction
-
subtracts one number from another - Multiplication
*
multiplies one number with another. - Division
/
divides one number by another. Note that unlike in some languages, division always produces a floating-point result in python, even for integer operands. - Floor division
//
divides and cuts the result to the nearest whole number towards minus infinity, disregarding any fractions. It also works on floating point numbers, but the final result would still be a whole number with the floating point type. - Exponentiation
**
raises one number to the power of another.
# Examples Numeric operations print(5 + 11) # Addition. Outputs 16 print(10 - 8) # Subtraction. Outputs 2 print(6 * 2) # Multiplication. Outputs 12 print(18 / 2) # Division. Outputs 9.0 print(17 // 2) # Floor division. Outputs 8 print(-10 // 3) '''Floor division. Outputs -4 as the number is cut to nearest whole towards negative infinity''' print(10.0 // 3.0) # Floor division. Outputs 3.0 print(2 ** 4) # Exponentiation. Outputs 16
STRING OPERATORS
- Concatenation
+
allows to combine strings together. Example: "Hello" + " " + "World" results in "Hello World". - Repetition
*
repeats a string a given number of times. Example: "Repeat" * 3 results in "RepeatRepeatRepeat."
print("Introduction “ + “to “ + “Python!”) ) # Concatenation. Outputs Introduction to Python! print("Welcome" * 3) # Repetition. Outputs WelcomeWelcome Welcome
MODULO OPERATOR
The modulo operator (%) provides the remainder of the division between two numbers. It's beneficial for the following:
Checking divisibility
If x % y == 0, then x is divisible by y.Extracting digits
x % 10 gives the right-most digit of x in base 10.- Determining if the number is
even or odd
If x % 2 == 0, if the remainder of the division by two is zero, then x is an even number.
# Modulo operation print(10 % 3) # outputs 1 as 10 divided by 3 equals 3 with a remainder of 1 print(5 % 9) # outputs 5 as 5 cannot be divided evenly by 9 print(18 % 1) # for any number modulo 1 will always return 0 as any number divided by 1 is itself
ORDER OF PRECEDENCE
The precedence of operators determines the order in which operations are evaluated. The concept of operator precedence is similar to the order of operations in mathematics (PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).
- Parentheses
()
have the highest precedence and can be used to override the default precedence. - Exponentiation
**
is the second highest, and they also have right-to-left associativity, meaning if an expression contains several exponentiation operators, they are evaluated from right to left. - Multiplication
*
, Division/
, Floor Division//
, and Modulo%
all have the same precedence level and are evaluated from left to right. - Addition and Subtraction
+, -
are last and are also evaluated from left to right.
result = (2 + 1) ** 3 ** 2 + 10 / 3 * 4 ''' 1. Parentheses are present, so we evaluate them first 2. Exponents are next with @@3**2@@ being evaluated second, resulting in 9 3. Next, due to right-to-left associativity, 3 ** 9 will be evaluated, resulting in 19683 3. Subsequently multiplication/division. First 10 / 3 is evaluated resulting in 3.333.. and then multiplied by 4, yielding 13.333... 4. Addition/Subtraction. Lastly, we add the result of the parenthesis and exponentiation to the division and multiplication. The final result is 19696.333... '''
KEY THINGS TO REMEMBER
- The main numeric operators are addition, subtraction, multiplication, division, floor division, modulo, and exponentiation.
- The main string operators are concatenation and repetition.
- The modulo operator provides the remainder of the division.
- Order of precedence determines how operations are evaluated in complex expressions. Python follows the PEMDAS rule:
- Parentheses. Highest precedence
- Exponentiation. Evaluated right to left.
- Multiplication, Division, Floor Division, Modulo. Evaluated left to right.
- Addition and subtraction. Evaluated last, from left to right.
SELF-CONTROL EXERCISES
< previous | Table of Contents | next> |