Python Operators

Welcome to The Coding College, your trusted resource for learning Python programming. In this tutorial, we’ll explore Python operators, which are essential for performing operations on variables and values. Whether you’re a beginner or revisiting the basics, this guide will give you a thorough understanding of Python operators and their use cases.

Let’s dive in!

What Are Python Operators?

Operators in Python are special symbols or keywords that perform operations on one or more operands. They allow you to perform calculations, comparisons, and logical operations, manipulate data, and much more.

Types of Operators in Python

Python provides several types of operators:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Membership Operators
  7. Identity Operators

1. Arithmetic Operators

Arithmetic operators are used for basic mathematical operations.

OperatorDescriptionExampleOutput
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division (float)5 / 31.6667
//Floor Division5 // 31
%Modulus (remainder)5 % 32
**Exponentiation5 ** 3125

Example

a = 10  
b = 3  

print(a + b)  # Output: 13  
print(a % b)  # Output: 1  

2. Comparison Operators

Comparison operators compare two values and return a Boolean (True or False).

OperatorDescriptionExampleOutput
==Equal to5 == 3False
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal to5 >= 3True
<=Less than or equal to5 <= 3False

Example

x = 7  
y = 10  

print(x > y)  # Output: False  
print(x != y) # Output: True  

3. Logical Operators

Logical operators are used to combine conditional statements.

OperatorDescriptionExampleOutput
andReturns True if both are TrueTrue and FalseFalse
orReturns True if at least one is TrueTrue or FalseTrue
notReverses the Boolean valuenot TrueFalse

Example

a = True  
b = False  

print(a and b)  # Output: False  
print(a or b)   # Output: True  
print(not a)    # Output: False  

4. Bitwise Operators

Bitwise operators perform operations on binary representations of integers.

OperatorDescriptionExampleOutput
&Bitwise AND5 & 31
``Bitwise OR`5
^Bitwise XOR5 ^ 36
~Bitwise NOT~5-6
<<Left Shift5 << 110
>>Right Shift5 >> 12

Example

x = 5  # Binary: 101  
y = 3  # Binary: 011  

print(x & y)  # Output: 1 (Binary: 001)  

5. Assignment Operators

Assignment operators assign values to variables.

OperatorDescriptionExampleEquivalent
=Assignx = 5x = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 3x = x - 3
*=Multiply and assignx *= 3x = x * 3
/=Divide and assignx /= 3x = x / 3
//=Floor divide and assignx //= 3x = x // 3
%=Modulus and assignx %= 3x = x % 3

Example

x = 10  
x += 5  
print(x)  # Output: 15  

6. Membership Operators

Membership operators test if a value is in a sequence.

OperatorDescriptionExampleOutput
inTrue if value is in sequence"a" in "apple"True
not inTrue if value is not in sequence"z" in "apple"False

7. Identity Operators

Identity operators check if two objects are the same in memory.

OperatorDescriptionExampleOutput
isTrue if same objectx is yTrue
is notTrue if not same objectx is not yFalse

Example

x = [1, 2, 3]  
y = x  
z = [1, 2, 3]  

print(x is y)     # Output: True  
print(x is z)     # Output: False  

Exercises

1. Arithmetic Practice

Write a program to compute the following:

  • Sum of two numbers
  • Remainder when one number is divided by another

2. Comparison Challenge

Check if two numbers are equal or not using comparison operators.

3. Logical Fun

Use logical operators to check if a number is both greater than 5 and less than 20.

Learn Python at The Coding College

At The Coding College, we aim to make coding accessible to everyone. Python operators are the foundation of many programming tasks, and mastering them will set you up for success.

Conclusion

Python operators are versatile tools that allow you to manipulate data, compare values, and execute logical conditions. Practice these concepts to enhance your programming skills, and don’t forget to explore other Python tutorials on The Coding College.

Leave a Comment