Python Operations
Learning outcomes:
- What are operations, operators and operands
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- The concatenation operator
Introduction
Back in the Python Basics chapter, we saw how to add together two numbers using the +
symbol, how to subtract them using the -
symbol, and so on.
These symbols, known as operators, along with the function they perform on given values, known as operands, is what we will study in this chapter. We'll explore a wide variety of operators in Python, see what are they used for, and finally consider examples for each set of operators.
What are operations?
Understanding what exactly is an operation is very easy if we take the example below:
Here as you can see, two numbers are being added. What's happening is an operation known as addition.
Every operation has a corresponding operator that operates on given operands. To understand this we first need to understand what exactly is meant by the terms 'operator' and 'operands'.
Taking the example above, 3 + 5
represents the addition operation. Here +
is the operator of the addition operation, whereas 3
and 5
are the operands of this operation.
Let's consider another example.
Below shown is the logical OR operation. It returns True
if either of the operands is True
, and False
otherwise.
True or False
Can you name the operator and operands over here?
True
and False
are the operands on which the logical OR operation takes place, while the keyword or
is the operator that represents the logical OR operation.
Operators in Python
In Python, there are a handful of operators as we shall see in the sections that follow, each serving a specific job. We've divided the operators based on the type of operation they perform.
This gives us the following categories of operators:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- The concatenation operator
Arithmetic operators
Arithmetic operators perform mathematical operations on numbers such as addition, subtraction, exponentiation and so on.
Operation | Operator | Syntax | Purpose |
---|---|---|---|
Addition | + | x + y | Adds the numbers x and y |
Subtraction | - | x - y | Subtracts y from x |
Multiplication | * | x * y | Multiplies x with y |
Division | / | x / y | Divides x by y ; returns a float |
Floor Division | // | x // y | Divides x by y ; returns the floor of the result |
Exponentiation | ** | x ** y | Raises x to the power of y |
Modulo | % | x % y | Returns the remainder when x is divided by y |
Assignment operators
Assignment operators assign a value to an identifier in Python. The best example is of the =
equals sign.
Compound assignment operators perform an operation on the value and then assign it to the given identifer. They are frequently used to shorten reassignment statements.
Operation | Operator | Syntax | Purpose |
---|---|---|---|
Assignment | = | identifier = value | Assigns value to identifier |
Addition-assignment | += | x += y | Evaluate x + y , then assign to x |
Subtraction-assignment | -= | x -= y | Evaluate x - y , then assign to x |
Multiplication-assignment | *= | x *= y | Evaluate x * y , then assign to x |
Division-assignment | /= | x /= y | Evaluate x / y , then assign to x |
Exponentiation-assignment | **= | x **= y | Evaluate x ** y , then assign to x |
Modulo-assignment | %= | x %= y | Evaluate x % y , then assign to x |
Comparison operators
Comparison operator are used throughout Python to compare two given values together.
The most basic one of comparison operators is the equality operator, denoted by ==
(double equals sign). It compares two values and returns True
if they both are equal to one another; and False
otherwise.
Operation | Operator | Syntax | Purpose |
---|---|---|---|
Equality | == | x == y | Returns True if x is equal to y , and False otherwise. |
Negated equality | != | x != y | Returns True if x is not equal to y , and False otherwise. |
Greater than or equal to | >= | x >= y | Return True if x is greater than or equal to y , and False otherwise. |
Lesser than or equal to | <= | x <= y | Return True if x is lesser than or equal to y , and False otherwise. |
Greater than | > | x >y | Return True if x is greater than y , and False otherwise. |
Lesser than | < | x < y | Return True if x is lesser than y , and False otherwise. |
Logical operators
Logical operators evaluate given Boolean values and return True
or False
based on their truthy/falsy values.
Logical operators are frequently used in conditional statements to combine the results of two or more Boolean values.
Operation | Operator | Syntax | Purpose |
---|---|---|---|
Logical NOT | not | not x | Returns True if x is False , and otherwise False |
Logical OR | or | x or y | Returns True if x , or y or both are True , and otherwise False |
Logical AND | and | x and y | Returns True if x and y are both True , and otherwise False |
Bitwise operators
Bitwise operators evaluate given values and process their corresponding bits to return a given value.
Operation | Operator | Syntax | Purpose |
---|---|---|---|
Bitwise NOT | ~ | ~x | Inverts each bit in x and returns the result. |
Bitwise OR | | | x | y | Processes corresponding bits in x and y using the OR operation, and returns the result. |
Bitwise AND | & | x & y | Processes corresponding bits in x and y using the AND operation, and returns the result. |
Bitwise XOR | ^ | x ^ y | Processes corresponding bits in x and y using the XOR operation, and returns the result. |
Bitwise left-shift | << | x << y | Shifts each bit in x to the left by y places. |
Bitwise right-shift | >> | x >> y | Shifts each bit in x to the right by y places, adding zeroes to the right. |
Concatenation operator
There is one operator that's used to join together given sequences, that's also used in arithmetic operations. It's the +
symbol.
When used on strings, for instance, +
serves to join together the strings into one single string. Similarly, when used on lists, +
adds together the given lists and returns back the result.
In programming terminology, this is referred to as concatenation. Concatenation is to add together two sequences to produce a single sequence.
Operation | Operator | Syntax | Purpose |
---|---|---|---|
Concatenation | + | a + b | Concatenates the sequences a and b together. |
Spread the word
Think that the content was awesome? Share it with your friends!
Join the community
Can't understand something related to the content? Get help from the community.