Introduction to Python Operators
Python operators are special symbols or reserved keywords that execute operations on one or more operands (values or variables). Operators are essential to programming, allowing developers to manipulate data and control program flow. Python has many operators that perform various operations such as arithmetic, comparison, logic, assignment, identity, membership, and bitwise operations.
Operators can operate on different types of data, such as numbers, strings, lists, tuples, and dictionaries, depending on the operator type and the operands’ data type. First, read this article if you think you won`t be able to cope with programming tasks without Python assignment help. For example, the addition operator (+) can add two numbers, concatenate two strings, or merge two lists.
Table of Content
Key Highlights
- Python operators are the symbols that perform specific operations in between the operands. These operators are usually involved in performing the operations such as logical, arithmetic, comparison, etc.
- The Python interpreter follows a particular order while executing these operators. The operator precedence in Python determines how operators are evaluated in an expression.
- Understanding Python operators is critical for developing Python programs. Using operators, developers can write code that performs complex operations quickly and efficiently, allowing them to create more powerful and effective programs.
Types of Python Operators
Python Operators are a fundamental programming part that allows developers to manipulate data and control program flow. Python has several types of operators that can be used to perform different operations.
Python’s most common types of operators include
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators.
Understanding the different types of Python operators and how to use them effectively is critical for developing Python programs that can perform complex operations on data.
Python offers a diverse set of operators that serve various purposes.
1. Arithmetic Operator
Arithmetic operators are used to perform mathematical operations.
Operator | Description | Syntax | Output |
+ | Addition | a+b | Returns the sum of the operands |
– | Subtraction | a-b | Returns Difference of the operands |
/ | Division | a/b | Returns Quotient of the operands |
* | Multiplication | a*b | Returns product of the operands |
** | Exponentiation | a**b | returns exponent of a raised to the power b |
% | Modulus | a%b | returns the remainder of the division |
// | Floor division | a//b | returns a real value and ignores the decimal part |
Consider an example program for carrying out the arithmetic operations explained above.
1. Addition (+)
Adds two values to obtain the sum.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
add = Xa + Xb
print('Sum of the numbers is',Xa ,'and' ,Xb ,'is :',add)
Output:
2. Subtraction (–)
This operator subtracts or provides the difference between two operands.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
diff = Xa - Xb
print('Difference of the numbers is ',Xa ,'and' ,Xb ,'is :',diff)
Output:
3. Multiplication (*)
This operator multiplies two operands and provides the results.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
mul = Xa * Xb
print('Product of the numbers is ' ,Xa ,'and' ,Xb ,'is :',mul)
Output:
4. Division (/)
This operator involves dividing two operands.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
div = Xa / Xb
print('Division of the numbers is ',Xa ,'and' ,Xb ,'is :',div)
Output:
5. Exponentiation (**)
The exponentiation operator is nothing but the power, where the representation of the operands is in the form of a^{b} or Xa ** Xb. It performs the multiplication operation.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
power = Xa ** Xb
print('Exponent of the numbers is ',Xa ,'and' ,Xb ,'is :',power)
Output:
6. Floor division (//)
This operator rounds off the decimals obtained in the division operation.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
floor_div = Xa // Xb
print('Floor Division of the numbers is ',Xa ,'and' ,Xb ,'is :',floor_div)
Output:
7. Modulus (%)
The modulus operator obtains the remainder after dividing the operands.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
modulus = Xa % Xb
print('Modulus of the numbers is ',Xa ,'and' ,Xb ,'is :',modulus)
Output:
Overall Example:
Code:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
add = Xa + Xb
diff = Xa - Xb
mul = Xa * Xb
div = Xa / Xb
floor_div = Xa // Xb
power = Xa ** Xb
modulus = Xa % Xb
print('Sum of the numbers is',Xa ,'and' ,Xb ,'is :',add)
print('Difference of the numbers is ',Xa ,'and' ,Xb ,'is :',diff)
print('Product of the numbers is ' ,Xa ,'and' ,Xb ,'is :',mul)
print('Division of the numbers is ',Xa ,'and' ,Xb ,'is :',div)
print('Floor Division of the numbers is ',Xa ,'and' ,Xb ,'is :',floor_div)
print('Exponent of the numbers is ',Xa ,'and' ,Xb ,'is :',power)
print('Modulus of the numbers is ',Xa ,'and' ,Xb ,'is :',modulus)
Output:
2. Bitwise Operators
In Python bitwise operator is usually used to perform operations on the binary representation for the integer values. The bitwise operator works on bits and conducts the operations bit by bit. Refers to the operators working on a bit, i.e., they treat the operand as a string of bits; for example, in bitwise operations, 5 will be considered 0101.
The box below provides the bitwise operators in Python
Operator |
Description | Syntax |
Output |
& | Binary AND | a&b | The bit is copied to the result only if present in both operands. |
| | Binary OR | a|b | This function copies a bit if present in either of the operands. |
^ | Binary XOR | a^b | Copies the bit if it is set in one operand but not both. |
~ | Binary One’s Complement | a~b | Unary operation of flipping bits. |
<< | Binary Left Shift | a<<b | The left operand value is moved left by the number of bits specified by the right operand. |
>> | Binary Right Shift | a>>b | The value of the left operand is shifted to the right by the number of bits indicated by the right operand. |
The below example shows the bitwise operator as follows. In the below example, we have defined all the bitwise operators as follows.
1. Bitwise AND (&)
The bitwise AND operator performs the logical AND operation on the values given and returns the value.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
res = Xa & Xb
print ("Xa & Xb : ", res)
Output:
2. Bitwise OR (|)
The bitwise OR operator performs logical OR operation on the given input values.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
res = Xa | Xb
print ("Xa | Xb : ", res)
Output:
3. Bitwise xor (^)
The bitwise xor operator performs the logical XOR operation on the corresponding bits on the input values.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
res = Xa ^ Xb
print ("Xa ^ Xb : ", res)
Output:
4. Bitwise 1’s complement (~)
The bitwise 1’s complement operator returns the result of the bitwise negation of a value where each bit is inverted.
Example:
X = int(input('Enter number: '))
res = ~X
print ("X : ", res)
Output:
5. Bitwise left-shift (<<)
The bitwise left-shift operator shifts the bits for a value by a given number of places to the left-hand side by adding 0s to new positions.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
res = Xa << Xb
print ("Xa << Xb : ", res)
Output:
6. Bitwise right-shift (>>)
The bitwise right-shift operator involves shifting the bits for an input value by a given number of places right; during this, some bits usually get lost.
Example:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
res = Xa >> Xb
print ("Xa >> Xb : ", res)
Output:
Overall Example
Code:
first_operand = 10
second_operand = 15
res = first_operand & second_operand
print ("first_operand & second_operand : ", res)
res = first_operand | second_operand
print ("first_operand | second_operand : ", res)
res = first_operand ^ second_operand
print ("first_operand ^ second_operand : ", res)
res = ~first_operand;
print ("~first_operand : ", res)
res = first_operand << 5;
print ("first_operand << 5 : ", res)
res = first_operand >> 5;
print ("first_operand >> 5 : ", res)
Output:
3. Membership Operators
Refers to the operators used in the validation of membership of operand test in a sequence, such as strings, lists, or tuples. There are two types of membership operators in Python.
Operator |
Syntax |
Output |
in | if (a in x): | This statement is considered true if it locates a variable in the designated sequence and false if it does not. |
not in | If ( b not in x ): | This statement will be true if it fails to locate a variable in the designated sequence and false if it does locate it. |
The below example shows the membership operator as follows.
Code:
first_operand = 29
second_operand = 23
list = [11, 15, 19, 23, 27]
if (first_operand not in list):
print("first_operand is NOT present in given list")
else:
print("first_operand is present in given list")
if (second_operand in list):
print("second_operand is present in given list")
else:
print("second_operand is NOT present in given list")
Output:
4. Identity Operators
In Python, the identity operator is used to compare the memory locations of two objects and to return the Boolean value that depends on whether they refer to the same object. There are two types of identity operators in Python.
Operator | Syntax | Output |
is | x is y | returns True if the type of the value in y points to the same type in the x. |
is not | x is not y | returns True if the type of the value in y points to a different type than the value in the x |
The below example shows the identity operator as follows.
Code:
first_operand = 10
second_operand = 20
third_operand = first_operand
print(first_operand is not second_operand)
print(first_operand is third_operand)
Output:
5. Comparison Operators
A comparison operator is used to compare the values of two operands, and after comparing the value, it will return a true or false Boolean value. It is also known as Relational operators.
Operator | Syntax | Output |
== | (a == b) | If the values of a and b are equal, then the condition becomes true. |
!= | (a != b) | If the values of a and b are not equal, then the condition becomes true. |
<> | (a <> b) | If the values of a and b are not equal, then the condition becomes true. |
> | (a > b) | If the value of a is greater than that of b, then the condition becomes true. |
< | (a < b) | If the value of a is less than that of b, then the condition becomes true. |
>= | (a >= b) | If the value of a is greater than or equal to that of b, then the condition becomes true. |
<= | (a <= b) | If the value of b is less than or equal to that of b, then the condition becomes true. |
The below example shows the comparison operator as follows.
1. Greater than (>)
Code:
x = 30
y = 35
print('x > y is', x>y)
Output:
2. Less than (<)
Code:
x = 30
y = 35
print('x < y is', x<y)
Output:
3. Equal to (==)
Code:
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
print("Xa == Xb : ", Xa == Xb)
Output:
4. Not equal to (!=)
Code:
x = 30
y = 35
print('x != y is', x!=y)
Output:
5. Greater than or equal to (>=):
Code:
x = 30
y = 35
print('x >= y is', x>=y)
Output:
6. Less than or equal to (<=):
Code:
x = 30
y = 35
print('x <= y is', x<=y)
Output:
Overall Code
Code:
first_operand = 15
second_operand = 25
print("first_operand == second_operand : ", first_operand == second_operand)
print("first_operand != second_operand : ", first_operand != second_operand)
print("first_operand > second_operand : ", first_operand > second_operand)
print("first_operand < second_operand : ", first_operand < second_operand)
print("first_operand >= second_operand : ", first_operand >= second_operand)
print("first_operand <= second_operand : ", first_operand <= second_operand)
Output:
6. Assignment Operators
When working with Python, you can use assignment operators to assign variable values. Following are the types of assignment operators in Python.
Operator | Description | Syntax | Output |
= | Equal to | c = a + b | assigns a value of a + b into c |
+= | Add AND | c += a | is equivalent to c = c + a |
-= | Subtract AND | c -= a | is equivalent to c = c – a |
*= | Multiply AND | c *= a | is equivalent to c = c * a |
/= | Divide AND | c /= a | is equivalent to c = c / ac /= a is equivalent to c = c / a |
%= | Modulus AND | c %= a | is equivalent to c = c % a |
**= | Exponent AND | c **= a | is equivalent to c = c ** a |
//= | Floor Division | c //= a | is equivalent to c = c // a |
The below example shows the assignment operator as follows.
Code:
first_operand = 10
first_operand += 5
print ("first_operand += 5 : ", first_operand)
first_operand -= 5
print ("first_operand -= 5 : ", first_operand)
first_operand *= 5
print ("first_operand *= 5 : ", first_operand)
first_operand /= 5
print ("first_operand /= 5 : ",first_operand)
first_operand %= 3
print ("first_operand %= 3 : ", first_operand)
first_operand **= 2
print ("first_operand **= 2 : ", first_operand)
first_operand //= 3
print ("first_operand //= 3 : ", first_operand)
Output:
7. Logical Operators
These operators are used to perform similar operations as logical gates; there are 3 types of logical operators in Python.
Operator | Description | Syntax | Output |
and | Logical AND | a and b | a condition is true if both a and b are true |
or | Logical OR | a or b | a condition is true if either a and b are true |
not | Logical NOT | not a | Complement the operand |
The below example shows the logical operator as follows.
Code:
first_operand = True
second_operand = False
print(first_operand and second_operand)
print(first_operand or second_operand)
print(not first_operand)
Output:
Python Operators Precedence
In Python, operators are evaluated in a specific order of precedence when used in an expression. This order determines their sequence of evaluation. The order of precedence ensures that expressions are evaluated correctly, following the standard mathematical rules. In cases where multiple operators have the same precedence, the order of evaluation follows the associativity of the operator. For example, some operators, such as addition and multiplication, are left-associative, meaning they are evaluated from left to right. Others, such as exponentiation, are right-associative, meaning they are evaluated from right to left. The below table shows operator precedence as follows.
Operator | Description |
** | Exponentiation |
‘~ + -’ | Bitwise NOT, Unary Plus and Minus |
<‘* / % //’ | Multiplication, division, modulus, and floor division |
‘+ -’ | Addition and subtraction |
‘>> <<’ | Bitwise shift right and left |
& | Bitwise AND |
^ | Bitwise XOR |
and | Logical and |
not | Logical not |
or | Logical or |
‘== != > >= < <=’ | Comparison Operators |
= %= /= //= -= += *= **= | Assignment Operators |
Is, is not | Identity Operators |
in not in | Membership Operators |
not or and | Logical Operators |
Conclusion
In conclusion, Understanding Python operators and their precedence in writing efficient and error-free code is important. With its intuitive syntax and a vast library of operators, Python remains a popular choice for developers across various domains, from web development to data science and machine learning. With its ease of use and flexibility, Python is an excellent language for beginners and advanced programmers.
Frequently Asked Questions(FAQs)
Q1. What does <> mean in Python?
Ans: The symbol <> represents the not equal operator. You can also represent not equal as !=. It returns a true statement in the given condition where both the operands are false, else false.
Q2. What is operator precedence? Which operator has the highest precedence?
Ans: Operator precedence is an order which is predefined and followed by the interpreter while executing multiple operations at the same time. Among all the operators (), parentheses have the highest precedence.
Q3. Is the boolean value true if null?
Ans: The null value represents that the specified variable doesn’t have anything in it. Hence it is neither true nor false.
Recommended Articles
We hope that this EDUCBA information on “Python Operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.