Python Operators

Python Operators

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.

Operator Description Syntax + Addition: It adds two operands x + y - Subtraction: It subtracts two operands x - y

Multiplication: It multiplies two operands x y / Division (float): It divides the first operand by the second x / y // Division (floor): It divides the first operand by the second x // y % Modulus: It returns the remainder when the first operand is divided by the second x % y

 Power: It Returns first raised to power second x y   # Examples of Arithmetic Operator 
a = 90
b = 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 
# Power
p = a ** b
# print results 
print(add) 
print(sub) 
print(mul) 
print(div1) 
print(div2) 
print(mod)
print(p)

Output:
130
50
3600
2.25
2
1
1.4780E + 78        

Relational Operators:

A relational operator compares the values. It either returns True or False based on the conditions.

   Operator Description Syntax   >  Greater than: It gives True if the left operand is greater than the right x > y   <  Less than: It gives True if the left operand is less than the right x < y   == Equal to: It gives True if both operands are equal x == y   != Not equal to: It gives True if operands are not equal x != y   >= Greater than or equal to : It gives True if the left operand is greater than or equal to the right x >= y   <= Less than or equal to : It gives True if the left operand is less than or equal to the right x <= y   # Examples of Relational Operators
a = 1
b = 3
# 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)

Output:
False
True
False
True
False
True        

Logical operators:

Logical operators perform Logical AND, Logical OR and Logical NOT operations.

   Operator Description Syntax   And Logical AND: It gives True if both the operands are true x and y   Or Logical OR: It gives True if either of the operands is true x or y   Not Logical NOT: It gives True if the operand is false not x   # 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)

Output:
False
True
False        

Bitwise operators:

Bitwise operators act on bits and perform bit by bit operations.

   Operator Description Syntax   & Bitwise AND x & y   | Bitwise OR x | y   ~ Bitwise NOT ~x   ^ Bitwise XOR x ^ y   >>  Bitwise right shift x>>   <<  Bitwise left shift x<<   # 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)

Output:
0
14
-11
14
2
40        

Assignment operators:

Assignment operators are used to assigning values to the variables.

Operator Description Syntax = Assign the value of the right side of expression to the left side operand x = y + z += Add AND: Add right-side operand with left side operand and then assign to left operand a+=b a=a+b -= Subtract AND: Subtract right operand from left operand and then assign to left operand a-=b a=a-b = Multiply AND: Multiply right operand with left operand and then assign to left operand a=b a=ab /= Divide AND: Divide left operand with right operand and then assign to left operand a/=b a=a/b %= Modulus AND: Takes modulus using left and right operands and assign the result to left operand a%=b a=a%b //= Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand a//=b a=a//b = Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand a=b a=a*b &= Performs Bitwise AND on operands and assign value to left operand a&=b a=a&b |= Performs Bitwise OR on operands and assign value to left operand a|=b a=a|b ^= Performs Bitwise XOR on operands and assign value to left operand a^=b a=a^b >>= Performs Bitwise right shift on operands and assign value to left operand a>>=b a=a>>b <<= Performs Bitwise left shift on operands and assign value to left operand a <<= b a= a << b Learn full tutorial-

Python Operators

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

要查看或添加评论,请登录

Alimam Miya的更多文章

  • Elevate Your CAT Preparation with iQuanta's CAT Online Course

    Elevate Your CAT Preparation with iQuanta's CAT Online Course

    Are you preparing for CAT 2025 and looking for the best online course to boost your chances of success? iQuanta’s CAT…

  • Best CAT Score Calculator 2024

    Best CAT Score Calculator 2024

    The Common Admission Test (CAT) is a crucial milestone for MBA aspirants aiming for prestigious institutes like IIMs…

  • Top 10 Pharma Companies in India 2024

    Top 10 Pharma Companies in India 2024

    In this will talk about the Top 10 Pharma Companies in India. India’s pharmaceutical assiduity has surfaced as a global…

  • Top 10 Credit Card Companies in India 2024

    Top 10 Credit Card Companies in India 2024

    This article will talk about the Top 10 Credit Card Companies in India. In the dynamic geography of India’s fiscal…

  • Top Real Estate Companies in India 2024

    Top Real Estate Companies in India 2024

    This article will talk about the Top Real Estate Companies in India 2024. The real estate region in India has witnessed…

    1 条评论
  • Top Real Estate Companies in Kolkata 2024

    Top Real Estate Companies in Kolkata 2024

    This article will talk about the Top Real Estate Companies in Kolkata. Kolkata, the artistic locus of India, is going…

  • Top Real Estate Companies in Delhi 2024

    Top Real Estate Companies in Delhi 2024

    This article will talk about the Top Real Estate Companies in Delhi. Delhi, the heart of India, not only serves as the…

    1 条评论
  • Hostinger Coupon Codes 2024 [60% Plan Discount]

    Hostinger Coupon Codes 2024 [60% Plan Discount]

    Looking for an affordable yet powerful hosting solution? Hostinger is one of the leading web hosting providers known…

  • 5 Best CAT Online Coaching in Gurugram

    5 Best CAT Online Coaching in Gurugram

    Preparing for the Common Admission Test (CAT) can be a daunting task, especially with the myriad of coaching options…

  • CAT Mock Tests Series - Boost Your CAT Preparation

    CAT Mock Tests Series - Boost Your CAT Preparation

    Preparing for the Common Admission Test (CAT) can be a daunting task, but with the right resources and strategy, it can…

社区洞察

其他会员也浏览了