Last Updated on September 22, 2023 by Mayank Dham
In Python programming, a powerful and versatile toolset exists to manipulate and interact with data: Python operators. Operators in Python are the building blocks that allow programmers to perform a wide array of computations, comparisons, and transformations on variables and values. From basic arithmetic calculations to more intricate logical evaluations, Python operators form the backbone of expression evaluation and decision-making within code. These operators encompass a rich collection of functionalities, enabling developers to perform mathematical operations, combine strings, assess conditions, iterate through collections, and much more. Understanding the diverse spectrum of Python operators is essential for crafting efficient, concise, and expressive code that can handle various tasks with precision and elegance. Let’s delve into the world of Python operators, exploring their classifications, use cases, and the intricate role they play in shaping the language’s dynamic capabilities.
Python operators are special symbols that are used for arithmetic and logical operations. This operation performs on operands from the example we will understand what is operands and what is an operator.
Types of Python Operators:
The python arithmetic operators are used mainly with numeric values to perform common mathematical operations.
# your code goes here # Examples of Arithmetic Operator a = 10 b = 20 # Addition of numbers add = a + b # Subtraction of numbers sub = a - b # Multiplication of number mul = a * b # Division(float) of number div1 = a / b # Division(floor) of number div2 = a // b # Modulo of both number mod = a % b # Power p = a ** b # print results print(add) print(sub) print(mul) print(div1) print(div2) print(mod) print(p)
30 -10 200 0.5 0 10 100000000000000000000
In the above python program we have taken variables ‘a’ and ‘b’ and signed the values 10 and 20. After this we simply do all the arithmetic operation like addition (which simply add the numbers), subtraction (this operator subtracts one number from another number, multiplication(this python arithmetic operator simply multiply the number ), power(this operator give the power ab) a is base here and b is the exponential power of a.
These python operators are used to assign value to variables.
# your code goes here # Examples of Assignment Operators a = 50 # Assign value b = a print(b) # Add and assign value b += a print(b) # Subtract and assign value b -= a print(b) # multiply and assign b *= a print(b) # bitwise lishift operator b <<= a print(b)
50 100 50 2500 2814749767106560000
In the above python program we have taken variables ‘a’ and ‘b’ and signed the value a =50 and assign b=a, so if print the value of b we got 50 on the output screen .here value of ‘a’ assigned in b, after that we ‘b+=a’ which simply mean b=b+a, and similarly b-=a means b=b-a, b=a means b=ba, b<<=a means assigning b =b<< a.
These python operators are used to compare two values.
# your code goes here # Examples of Relational Operators a = 23 b = 43 # a > b is False print(a > b) # a < b is True print(a < b) # a == b is False print(a == b) # a != b is True print(a != b) # a >= b is False print(a >= b) # a <= b is True print(a <= b)
False True False True False True
In the above python program we have taken variables ‘a’ and ‘b’ and signed the values 23 and 43. After this, we simply compare these two values like which is greater, which is lesser, either they are equal or not, or greater equal to, lesser equal to by ‘>’,’<’,’==’,’>=’,’<=’.
These python operators are used to combine conditional statements .it performs Logical AND, Logical OR, and Logical NOT.
# your code goes here # Examples of Logical Operator a = True b = False # Print a and b is False print(a and b) # Print a or b is True print(a or b) # Print not a is False print(not a)
False True False
In the above python program we have taken two boolean variables ‘a’ and ‘b’ and simply get the output of the ‘and’, ‘or’,’ not’ logical operation.
These python operators are used to compare the objects if the objects are equal and they allocate the same location then it returns true and if objects are not equal then it returns false.
# your code goes here a = 10 b = 20 c = a print(a is not b) print(a is c)
In the above python program we have taken two variables a and b and assigned the values 10 and 20. after this, we have taken c and set c=a. So in output, we observe true for a ‘is not’ b and a is c give true . ‘is’ operator check whether the object is equal or not.
These python operators are used to test if a sequence is presented in an object or not. if it pre4sent then it returns true and if the sequence is not present then it returns false.
# your code goes here x=["mango","banana"] print("mango" in x) j=["1","2"] print( "3" not in j)
In the above python program we have taken two list names x and j. here we check whether “mango” is in the list or not, so we use the ‘in’ (membership operator) if the element is present in the sequence then it returns true, and ‘not in’ operator returns false if the element is present and return true if the element is not present.
These python operators are used to operate on binary numbers. These operators act on bits and perform bit-by-bit operations.
# your code goes here # Examples of Bitwise operators a = 10 b = 4 # Print bitwise AND operation print(a & b) # Print bitwise OR operation print(a | b) # Print bitwise NOT operation print(~a) # print bitwise XOR operation print(a ^ b) # print bitwise right shift operation print(a >> 2) # print bitwise left shift operation print(a << 2)
0 14 -11 14 2 40
In the above python program we have taken variables ‘a’ and ‘b’ and signed the values 10 and 4 respectively. So here we have two numbers 10 and 4 .then we use the operator ‘&’ for and operation,’|’ for or operation, ‘^’ for xor operation,’>>’ for the right shift, ‘<<’ for the left shift.
Python has a variety of operators that allow you to perform operations on variables and values. These include arithmetic operators (e.g. +, -, *, /), comparison operators (e.g. ==, !=, >, <, >=, <=), assignment operators (e.g. =, +=, -=), and logical operators (e.g. and, or, not). Additionally, Python has special operators such as the identity operator (is), membership operator (in), and bitwise operators (e.g. &, |, ~, ^, <<, >>).
Arithmetic operators perform basic arithmetic operations, such as addition, subtraction, multiplication, and division. Comparison operators compare two values and return a Boolean value (True or False) based on the result. Assignment operators assign values to variables, and logical operators combine multiple conditions to return a single Boolean value.
The identity operator (is) checks if two variables refer to the same object in memory, while the membership operator (in) checks if a value is present in a sequence (such as a list or a string). Bitwise operators perform operations on the bits of a binary number.
Frequently Asked Questions (FAQs)
Here are some frequently asked questions about Python operators.
Q1. What are operators in Python?
Operators in Python are symbols or special keywords that perform various operations on one or more operands (values). They include arithmetic, comparison, logical, assignment, and bitwise operators, among others, and are fundamental for performing computations and making decisions in code.
Q2. What is the difference between unary and binary operators?
Unary operators operate on a single operand, while binary operators work on two operands. For instance, uses a unary minus operator, while x + y employs a binary addition operator.
Q3. How are arithmetic operators used in Python?
Arithmetic operators perform mathematical calculations. Examples include addition +, subtraction , multiplication *, division /, and modulus %. They’re used to manipulate numeric values.
Q4. What are comparison operators used for in Python?
Comparison operators are used to compare values and evaluate conditions. Examples include equality ==, inequality !=, greater than >, less than <, and others. They’re commonly employed in decisionaking structures like if statements.
Q5. How do logical operators work in Python?
Logical operators (and, or, not) are used to perform logical operations on boolean values. They help combine conditions and create complex boolean expressions, making it easier to make decisions based on multiple criteria.