Python Operators
- Technical Education
- Oct 8, 2021
- 3 min read
Updated: Dec 1, 2021
In this module tutorial, we will learn Python Operators. It’s a basic idea of Python that's needed for understanding the workflow of a Python program.
Python Operators
Operators commonly are used to performing operations on both values and variables in Python. These are standard symbols used for the aim of logical and arithmetic operations. In this post, we will look into various types of Python operators.
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Membership Operators
Arithmetic operators
These operators are used to performing mathematical functions such as addition, multiplication, division, subtraction and much more.
OperatorDescriptionSyntax+Addition: It adds two operandsx + y-Subtraction: It subtracts two operandsx - yMultiplication: It multiplies two operandsx y/Division (float): It divides the first operand by the secondx / y//Division (floor): It divides the first operand by the secondx // y%Modulus: It returns the remainder when the first operand is divided by the secondx % yPower: It Returns first raised to power secondx y
# Examples of Arithmetic Operator a = 90b = 40# 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# Powerp = a ** b# print results print(add)print(sub)print(mul)print(div1)print(div2)print(mod)print(p)Output:1305036002.25211.4780E + 78
Relational Operators:
A relational operator compares the values. It either returns True or False based on the conditions.
OperatorDescriptionSyntax> Greater than: It gives True if the left operand is greater than the rightx > y< Less than: It gives True if the left operand is less than the rightx < y==Equal to: It gives True if both operands are equalx == y!=Not equal to: It gives True if operands are not equalx != y>=Greater than or equal to : It gives True if the left operand is greater than or equal to the rightx >= y<=Less than or equal to : It gives True if the left operand is less than or equal to the rightx <= y
# Examples of Relational Operatorsa = 1b = 3# a > b is Falseprint(a > b)# a < b is Trueprint(a < b)# a == b is Falseprint(a == b)# a != b is Trueprint(a != b)# a >= b is Falseprint(a >= b)# a <= b is Trueprint(a <= b)Output:FalseTrueFalseTrueFalseTrue
Logical operators:
Logical operators perform Logical AND, Logical OR and Logical NOT operations.
OperatorDescriptionSyntaxAndLogical AND: It gives True if both the operands are truex and yOrLogical OR: It gives True if either of the operands is truex or yNotLogical NOT: It gives True if the operand is falsenot x
# Examples of Logical Operatora = Trueb = False# Print a and b is Falseprint(a and b)# Print a or b is Trueprint(a or b)# Print, not a is Falseprint(not a)Output:FalseTrueFalse
Bitwise operators:
Bitwise operators act on bits and perform bit by bit operations.
OperatorDescriptionSyntax&Bitwise ANDx & y|Bitwise ORx | y~Bitwise NOT~x^Bitwise XORx ^ y>> Bitwise right shiftx>><< Bitwise left shiftx<<
# Examples of Bitwise operatorsa = 10b = 4# Print bitwise AND operation print(a & b)# Print bitwise OR operationprint(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)Output:014-1114240
Assignment operators:
Assignment operators are used to assigning values to the variables.
OperatorDescriptionSyntax=Assign the value of the right side of expression to the left side operandx = y + z+=Add AND: Add right-side operand with left side operand and then assign to left operanda+=b a=a+b-=Subtract AND: Subtract right operand from left operand and then assign to left operanda-=b a=a-b=Multiply AND: Multiply right operand with left operand and then assign to left operanda=b a=ab/=Divide AND: Divide left operand with right operand and then assign to left operanda/=b a=a/b%=Modulus AND: Takes modulus using left and right operands and assign the result to left operanda%=b a=a%b//=Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operanda//=b a=a//b=Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operanda=b a=a*b&=Performs Bitwise AND on operands and assign value to left operanda&=b a=a&b|=Performs Bitwise OR on operands and assign value to left operanda|=b a=a|b^=Performs Bitwise XOR on operands and assign value to left operanda^=b a=a^b>>=Performs Bitwise right shift on operands and assign value to left operanda>>=b a=a>>b<<=Performs Bitwise left shift on operands and assign value to left operanda <<= b a= a << b
Learn full tutorial-
I hope this module can be useful for you to understand Operators in Python. You should try to use all the Operators and functionality to get a better understanding. Stay in touch with us to find out more modules that are taught like this. Keep rehearsing it!
Original posted on - Python operators
Comments