Operators are special symbols in Python that carry out specific operations.
Below are different types of operators that we are going to discuss:
Below is the list of Arithmetic operators:
What is operand?
The value on which the operator operates.
*Operand and operator together makes an "Expression".
" + " operator is used to perform addition of numbers.
" - " operator is used to perform substraction of numbers.
" / " operator is used to perform division of numbers.
" * " operator or Aestrisk is used to perform multiplication of numbers.
" // " operator or Floor Division provides the lower integer value .
" % " operator or Modulus is used to find out the remainder of division of 2 numbers.
" ** " operator or Exponentiation is used to find the power of number.
Using Arithmetic operators with strings:
" + " is used to add or combine 2 strings.
To add blank space between two strings:
" - " ," / ", " // ", " ** " - All these operators doesn't work on strings.
" * " operator or aestrisk cannot multiple one string with the other.
To perform multiplication on string, the string must be multiplied with a number as shown below.
To perfrom 2 operators - " + " and " * " together.
User cannot add a integer or number into the list using " + " operator.
User can add a list into a list using " + " operator.
Below is the list of Assignment operators:
To perform arithmetic a variable and to store the updated value in the same variable, assignment operators come handy.
The other operators(" -=", " *= "," /= ", " //= ", " %= ", " **= ") works in similar way.
" = " or equal to operator is used to assign a value to the variable.
In below cell, the user wants to add 10 in num1 (2000) and then store the new value i.e. 2010 in the same variable num1.
Shortcut to achieve above:
Below is the list of Comparison operators:
Comparison operators are used to compare to or more values. It will always return the boolean value i.e. True or False.
Now, the user has created a variable num1. To check num1 is less than 2 or not, " < " is used.
To check num1 is greater than 2 or not, " > " is used.
To check num1 is equal to 2 or not, " == " is used. It return true only if the two operands are equal. " = " is used only for assigning the values.
To check num1 is less than or equal to 2 or not, " <= " is used.
To check num1 is greater than or equal to 2 or not, " >= " is used.
To check num1 is not equal to 2 or not, " != " is used.
Below is the list of Logical operators:
AND operator is used when user wants 2 or more conditions to met.
Num1 is equals to 10 and user wants the number to be less than. But the output is false because num1 is not less than 10..
OR operator is used when user wants either of the conditions to met.
OR and NOT operator can be used together as shown below:
NOT operator is used to make the statement negative.
Below is the list of Identity operators:
Identity operators never compares the value instead it checks the memory address of the variable.
The output is true as both the values are same.
The output is true because the memory location is same.
Look at the IDS, both are same.
Num1 is equals to num2, that is why the output is true.
Since, the value is greater than 256, that is why the ouput is false a their memory address will be different..
What does it mean?
It means that '==' compares if both the object values are identical or not. 'is' compares if both the object belongs to the same memory location
Look the IDs, the IDs are different of both the variables.
The output is false because .
The output is false because .
The output is false because the memory location of both the variables are different.
Below is the list of Membership operators:
A list(l) and a variable(num1) is created, and to check if num1 is present inside the list or not, "IN" operator can be used.
Similarly, to check if 56 is present in list:
To check if 56 is not present in list, then "NOT IN" operator is used. Here, 56 is not present in list that is why the output is true.
Similarly, to check if 89 is not present in list:
Bitwise operators are used to compare (binary) numbers:
Below is the list of Bitwise operators: