operator of python
Operators: An operator is a symbol that represents an operation that may be performed on one or more operands. For Example - The + symbol represents the operation of addition. An operand is a value that a given operator is applied to, such as operands 3 and 4 is the expression 3 + 4. A unary operator operates on only one operand, such as the negation operator in -9. A binary operator operates on two operands, as with the addition operator. Most operators in programing languages are binary operators.
An operator is a symbol that represents an operation that may be performed on one or more operands. Operators that take one operand are called unary operators. Operators that take two operands are called binary operator.
These are the following operators are used in python-
Arithmetic Operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc. Python provides the arithmetic operators like +, - , * and /. It operators perform the usual operations. Note that the – symbol is used both as a unary operator (for negation) and a binary operator (for subtraction).
|
Arithmetic operator |
Meaning |
|
- Operand |
Negation |
|
+ |
Addition |
|
- |
Subtraction |
|
* |
Multiplication |
|
/ |
Division |
|
// |
Truncating div |
|
% |
Modulus |
|
** |
Exponentiation |
Python also includes an exponentiation (**) operator. Integer and floating point values can be used in both the base and the exponent.
2**5 =32
2.5**5.5=154.40808887540913
Python provides two forms of division “true” division is denoted by a single slash, /. Thus, 25/10 evaluates to 2.5. Truncating division is denoted by a double slash,//, it providing a truncated result based on the type of operands applied to. When both operands are integer values, the result is a truncated integer referred to as integer division. When as least one of the operands is a float type, the result is a truncated floating point. Thus, 25//10 evaluates to 2, while 25.0 // 10 becomes 2.0
|
Operators |
Operands |
Result type |
|
/ Division operator |
int,int |
Float |
|
Int,float |
Float |
|
|
Float,float |
float |
|
|
// truncating division operator |
Int,int |
Truncate int (“integer division) |
|
Int,float |
Truncated float |
|
|
Float,float |
Truncated float |
For Example: Write a program to demonstrate the use of arithmetic operator.
x = 16
y = 5
# Use of addition
print('Addition of x + y is =',x+y)
# use of subtraction
print('Subtraction of x – y is =',x-y)
# use of multiplication
print('Multiplication of x * y is=',x*y)
# Output: x / y
print('Division of x / y is =',x/y)
# use of truncating division
print('Truncating division x // y is =',x//y)
# use of exponentiation
print('Exponentiation x ** y is=',x**y)
Output:
Relational operators: These operators are also known as Comparison operators. These operators are used to compare values. It either returns True or False according to the condition. These operators are used to compare expressions. We often compare two quantities and depending on their relation, take certain decision. A relational operator is termed as a relational expression. The value of relational expression is either one or zero. It is one if the specified relation is true and zero if the relation is false.
For example: we compare the age of two person or the price of two items and so on.
|
Operator |
Meaning |
|
< |
Is less than |
|
< = |
Is less than or equal to |
|
> |
Is greater then |
|
>= |
Is greater than or equal to |
|
= = |
Is equal to |
|
! = |
Is not equal to |
For Example: Write a program to demonstrate the use of Relational operators.
x = 20
y = 32
# Use of is less than
print('x < y is:',x<y)
# Use of is less than or equal to
print('x <= y is:',x<=y)
# Use of is greater than
print('x > y is:',x>y)
# Use of is greater than or equal to
print('x >= y is:',x>=y)
# Use of is equal to
print('x == y is:',x==y)
# Use of is not equal
print('x != y is:',x!=y)
Output:
Logical Operators: These operators are used to combine two or more expressions. The entire expression is called as logical expression. Which evaluate to true or false. We need to test two or more condition at a time and make a decision depending upon the result. These are the following logical operators-
|
Operator |
Meaning |
|
and |
True if both the operands are true |
|
or |
True if either of the operands is true |
|
not |
True if operand is false |
For Example: Write a program to demonstrate the use of comparison operators.
x = True
y = False
# use of and
print('X and Y is:',x and y)
# use of or
print('X or Y is:',x or y)
# use of not
print('X not Y is:',not x)
Output:
Bit-wise Operators: These operators are used to manipulation of data at bit level. Bit-wise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name. For example, 5 and 10 in binary is 101 and 1010 respectively. These are the following bit-wise operators used in python programming-
|
Operator |
Meaning |
|
& |
Bitwise AND |
|
| |
Bitwise OR |
|
~ |
Bitwise NOT |
|
^ |
Bitwise XOR |
|
>> |
Bitwise right shift |
|
<< |
Bit-wise left shift |
Assignment operators: Assignment operator is used for assigning the value of right operand to the left operand. These operators are used in Python to assign values to variables. a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left. There are various compound operators used in Python programming, like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5. These are the following assignment operators used in python programming-
|
Operator |
Example |
Equivalent to |
|
= |
x = 5 |
x = 5 |
|
+= |
x += 5 |
x = x + 5 |
|
-= |
x -= 5 |
x = x - 5 |
|
*= |
x *= 5 |
x = x * 5 |
|
/= |
x /= 5 |
x = x / 5 |
|
%= |
x %= 5 |
x = x % 5 |
|
//= |
x //= 5 |
x = x // 5 |
|
**= |
x **= 5 |
x = x ** 5 |
|
&= |
x &= 5 |
x = x & 5 |
|
|= |
x |= 5 |
x = x | 5 |
|
^= |
x ^= 5 |
x = x ^ 5 |
|
>>= |
x >>= 5 |
x = x >> 5 |
|
<<= |
x <<= 5 |
x = x << 5 |



Comments
Post a Comment