Operators in Python
Agent: meansoperatorIn English, an operator is a symbol that has a specific meaning in Python.
We can classify operators in Python into 7 basic groups:
Operator: means operator in the English language, and operator is a symbol that has a specific meaning in Python. We can classify operators in Python into 7 basic groups:
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Membership Operators
Identity Operators
Factors that are used in arithmetic operations(Arithmetic Operators) in Python
| the name of the employee | his code | Example | Explanation of the code |
|---|---|---|---|
| Assignment | = |
a = b |
I give aa valueb |
| Addition | + |
a + b |
add valueb to valuea |
| Subtraction | - |
a - b |
Subtract a valueb from a valuea |
| Unary plus | + |
+a |
multiply the valuea by the factor+ |
| Unary minus | - |
-a |
multiply the valuea by the factor- |
| Multiplication | * |
a * b |
multiply the valuea byb |
| Exponent | ** |
a ** b |
double the value ofa byb |
| Division | / |
a / b |
swear valuea by a valueb |
| Floor Divide | // |
a // b |
swear valuea by a value band return the closest integer that does not contain a comma. |
| Module | % |
a % b |
To get the last digit that remains when we divide a value aby a valueb |
Here we put an example for each factor in the table
Examples of operators used in arithmetic operations in Python
worker =(Assignment Operator)
The operator =is used to assign a value to a variable. Factor = used to assign a value to a variable.
Example
a = 5 # 5 and we give it the value a here we have defined a variable named
b = a # a and we give it the same value as the variable b here we define a variable named
print('a =', a)
print('b =', b)
• We will get the following result when running.
b = 5
The Worker+(Addition Operator)
The operator +is used to add a value to a value, ie in addition operations. The + operator is used to add a value to a value, that is, in addition.
Example
a = 3
b = 4
c = a + b # c = 3 + 4 = 7
print('c =', c)
• We will get the following result when running.
worker -(Subtraction Operator)
The operator -is used to decrement a value from a value, ie in subtraction operations. Factor - used to reduce a value from a value, that is, in subtraction operations.
Example
a = 3
b = 4
c = a - b # c = 3 - 4 = -1
print('c =', c)
• We will get the following result when running.
worker +(Unary-Plus Operator)
It means multiplying the value by the factor +.
Example
# b has in the variable Unary-Plus a value greater than zero, then we put the value of a here and we put it in the variable
a = 10
b = +a # b = +(10) = 10
print('b =', b)
# b has in the variable Unary-Plus a value less than zero, then we put the value of a here and we put it in the variable
a = -10
b = +a # b = +(-10) = -10
print('b =', b)
• We will get the following result when running.
b = -10
worker -(Unary-Minus Operator)
It means multiplying the value by the factor -.
Example
# b has in the variable Unary-Minus a value greater than zero, then we put the value of a here and we put it in the variable
a = 10
b = -a # b = -(10) = -10
print('b =', b)
# b has in the variable Unary-Minus a value less than zero, then we put the value of a here and we put in the variable
a = -10
b = -a # b = -(-10) = 10
print('b =', b)
• We will get the following result when running.
b = 10
worker *(Multiplication Operator)
The operator *is used to multiply a value by a value, ie in multiplication operations.
Example
a = 6
b = 5
c = a * b # c = 6 * 5 = 30
print('c =', c)
• We will get the following result when running.
worker **(Exponential Operator)
The operator **is used to multiply a value by a value, ie to multiply a value by itself several times.
Example
a = 2
b = 5
c = a ** b # c = 2 ** 5 = 2 * 2 * 2 * 2 * 2 = 32
print('c =', c)
• We will get the following result when running.
worker /(Division Operator)
The operator /is used to divide a value by a value, ie in division operations.
Operator / (Division Operator) Factor / is used to divide a value by a value, i.e. in division operations. Example
Example
a = 8
b = 5
c = a / b # c = 8 ÷ 5 = 1.6
print('c =', c)
• We will get the following result when running.
worker //(Floor Division Operator)
The operator //is used to divide a value by a value and remove any numbers after the comma.
// (Floor Division Operator) The // operator is used to divide a value by a value and remove any numbers after the comma.
Example
a = 8
b = 5
c = a // b # c = 8 ÷ 5 = 1.6 [ after any number there is some comma ]===> c = 1
print('c =', c)
• We will get the following result when running.
The Worker%(Modulo Operator)
The Worker% is calledModuleAnd it's calledRemainderIn mathematics, it is the last number remaining in the division process.
So we use theModuleTo get the last remaining number of division.
And it has many benefits, for example, we can use it to find out whether a number is single or double( WhichEven or Odd) و هذا شرحناه بتفصيل في مادة الخوارزميات . The% factor is called the modulo, it is called Remainder in mathematics, and it is the last digit left over from the division process. So we use the modulo to get the last remaining number of the division. And it has many benefits, for example we can use it to find out if the number is single or double (ie Even or Odd) and this we explained in detail in the algorithms article.
In this example, we will store the number that remains from the division in the variable c.
Example
a = 8
b = 5
c = a % b; # c = 8 % 5 = 3
print('c =', c)
• We will get the following result when running.
Operators that are used to search in matrices(Membership Operators) in Python
| the name of the employee | his code | Example | Explanation of the code |
|---|---|---|---|
| In | in |
a in arr |
Is the value of the variable apresent in the array arr? If the answer is yes, it will return True |
| Not In | not in |
a not in arr |
Is the value of the variable anot in the array arr? If the answer is yes, it will return True |
Examples of operators used for searching arrays in Python
worker in(In Operator)
The operator inis used to check whether an array contains a certain value or not.
If there is an element in the array that has the same value to be searched for, the answer is returned
True.If no element in the array has the same value to be searched for, the answer is returned
False.
Example
# Here we have defined an array of 5 elements which are integers
numbers = [1, 2, 3, 4, 5]
# y and x here we have defined two variables
x = 3
y = 8
# The print command x containing the value of the variable numbers will be executed if the array is
if x in numbers:
print('x value exists in the array')
# The print command y containing the value of the variable numbers will be executed if the array is
if y in numbers:
print('y value exists in the array')
• We will get the following result when running.
• We note that the print command placed in the first condition was executed only because an element was found in the array numberswith the same value as the variable x.
worker not in(Not In Operator)
The operator not inis used to check if an array does not contain a certain value or not.
If no element in the array has the same value to be searched for, the answer is returned
True.If any element in the array has the same value to be searched for, the answer is returned
False.
Example
# Here we have defined an array of 5 elements which are integers
numbers = [1, 2, 3, 4, 5]
# y and x here we have defined two variables
x = 3
y = 8
# The print command x not containing the value of the variable numbers will be executed if the array is
if x not in numbers:
print('x value not exists in the array')
# The print command y not containing the value of the variable numbers will be executed if the array is
if y not in numbers:
print('y value not exists in the array')
• We will get the following result when running.
• We note that the print command placed in the second condition was executed only because no element was found in the array numberswith the same variable value y.
Operators that are used to tell whether two objects refer to the same object in memory(Identity Operators) in Python
| the name of the employee | his code | Example | Explanation of the code |
|---|---|---|---|
| Is | is |
a is b |
Are object aand object brefer to the same object in memory? If the answer is yes, it will return True |
| Is Not | is not |
a is not b |
Is the object aand the objectb not refer to the same object in memory? If the answer is yes, it will return True |
Here we put an example for each factor in the table
Examples of operators are used to check whether two objects refer to one object in memory or not in Python
How to find the address of a variable or object in memory in Python
To find out the memory location or address of any defined object, you can use the id().
Simply put the name of any variable or object inside it and it will return an integer representing its address in memory.
first example
# Here we have defined two variables that have different values
x = 3
y = 7
# Here we have printed their addresses in memory
print('x address is:', id(x))
print('y address is:', id(y))
• We will get a result similar to the following when running.
y address is: 1537266832
• We note that the address of the variable is xdifferent from the address of the variable y, and therefore this means that a special space has been reserved for each of them and that they refer to two different places in the memory.
note
If you declare two or more variables and give them the same value, the Python interpreter will reserve one place for them and put the value in it in order to save memory space.
second example
# Here we have defined two variables that have different values
x = 5
y = 5
# Here we have printed their addresses in memory
print('x address is:', id(x))
print('y address is:', id(y))
• We will get a result similar to the following when running.
y address is: 1833030700
• We note that the address of the variable xis the same as the address of the variable y, and therefore this means that one space has been reserved for both variables and that they point to the same place in memory.
worker is(Is Operator)
The operator isis used to check whether two objects refer to a single object in memory.
If they refer to the same object, it returns the answer
True.If they do not refer to the same object, it returns the answer
False.
Example
# Here we have defined two matrices that have the same size and put the same values in them
A = [1, 2, 3]
B = [1, 2, 3]
# In memory B and A here we have printed the address of the two arrays
print('A address is:', id(A))
print('B address is:', id(B))
# They point to one object in memory (ie to one address) the print command B and A will be executed if
if A is B:
print('A and B have the same ID')
• We will get a result similar to the following when running.
B address is: 36563624
• We note that the address of the array Ais different from the address of the array B, and therefore they do not refer to one place in the memory.
This is why the last print command placed inside the condition, which would have been executed if they were pointing to the same array in memory A, was not executed.B
The Workeris not(Is Not Operator)
The operator is notis used to check whether two objects do not point to a single object in memory.
If they do not refer to the same object, it returns the answer
True.If they refer to the same object, it returns the answer
False.
Example
# Here we have defined two matrices that have the same size and put the same values in them
A = [1, 2, 3]
B = [1, 2, 3]
# In memory B and A here we have printed the address of the two arrays
print('A address is:', id(A))
print('B address is:', id(B))
# They do not point to the same object in memory (ie to one address) the print command B and A will be executed if
if A is not B:
print('A and B have different IDs')
• We will get a result similar to the following when running.
B address is: 5892776
A and B have different IDs
• We note that the address of the array Ais different from the address of the array B, and therefore they do not refer to one place in the memory.
That's why the last print command placed inside the condition has been executed.