What are operators in python?
Operators are the symbols that perform various operations on Python objects. Python operators are the most essential to work with the Python data types. In addition, Python also provides identify membership and bitwise operators. The value that the operator operates on is called the operand.
For example:
2 + 3 = 5
#here python is + is used to add numbers
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.
Let's Look At The Operators In General
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
- "+" --> is used to add numbers or concatenate values
- "-" --> is used for subtraction
- "*" --> is used for multiplication
- "/ "--> is used for division
- "//" --> is used for floor division
- "%" --> is used for remainder
- "**" --> is used for exponent
Let's see the code in action
a = 2+ 3
print(a)
#output: 5 add both values
#there is something tricky in this operand
#this operand is used for joining 2 strings together. if you use numbers as string
#you will get 2 strings join together
a = "2" + "3"
print(a)
#output: 23, look we have 23 instead of 5. We will look deep into this later
b = 3 - 1
print(b)
#output: 1 find the difference
c = 2 * 3
print(c)
#output: 6 multiplication
d = 6 / 3
print(d)
#output: 2 division
e = 10 // 3
print(a)
#output: 3 #Floor division - division that results into whole number adjusted to the left in the number line
f = 10 // 3
print(f)
#output: 1 gives the remainder
g = 10 ** 2
print(g)
#output: 100, because 10 * 10
Let's look at the code in the code editor
Let's Look At Comparison Operator
Comparison Operator
Comparison operators are used to compare values. It returns either True or False according to the condition.
- ">" --> is used to check if the value at the left is greater than the value at the right
- "<" --> is used to check if the value at the left is less than the value at the right
- ">=" --> is used to check if the value at the left is greater than or equal to the value at the right
- "<="--> is used to check if the value at the left is less than or equal to the value at the right
- "==" --> is used to check if both sides are equal
- " != " --> is used to check if both sides are not equal
Let's code some comparison operator
print(10 > 5)
#output: True
print(10 < 5)
#output: False
print(10 >= 5)
#output: True
print(10 <= 5)
#output: False
print(10 == 5)
#output: False
print(10 != 5)
#output: True
Let's check our code editor
Logical operators
Logical operators are the and, or, not operators. They are used to check if conditions or values are True or False
- "and" --> check if both values are True
- "or" --> check if one value is True
- "not" --> True if operand is false (complements the operand)
a = True
b = False
c = True
print(a and b)
#output: False, because b is not True
print(a and c)
#output: True, because both are True
print(a or b)
#output: True, because one value is True
print(not a)
#output: False, because not give you the opposite of the value
Let's see our code editor
Bitwise operators
Computers store all kinds of information as a stream of binary digits called bits. Whether you’re working with text, images, or videos, they all boil down to ones and zeros. Python’s bitwise operators let you manipulate those individual bits of data at the most granular level. There is no bitwise operator without a proper understanding of the binary systems. Here is a link that explains bitwise operators in-dept Bitwise Indept
Assignment operators
Assignment operators are used in Python to assign values to variables.
name = "Dozie" is a simple assignment operator that assigns the value "Dozie" on the right to the variable name on the left.
This is the most common method of defining a variable in python. But there is also a typed pattern which we look into later.
There are various compound operators in Python like a = 10, a += 2 that adds to the variable and later assigns the same. It is equivalent to a = a + 2.
You can also use an assignment operator with other arithmetic operators. Let's take a look.
a = 10
a += 5
#this is equivalent to a = a + 5. the value will be changed to 15
#others are:
a -= 5
a *= 2
a /= 2
a %= 2
a //= 4
Special Operator
Python has 2 special operators, which are identity and membership operators. let's take a look.
Identity operators
is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.
- "is" --> used to check if a value is located in the same memory space
- "is not" --> used to check if a value is not located in the same memory space
Let's look at the code
##Membership operators in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
a = [3, 4, 5, 7, 8, 9]
print(6 in a)
#output: False
b = ("a", "b", 9)
print("a" in b)
#output: True
print("b" not in b)
#output: False