Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

Python operators are symbols that perform operations on operands, such as variables and values. Python supports various types of operators, including arithmetic, comparison, assignment, logical, identity, membership, and bitwise operators.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

– Addition `+`
– Subtraction `-`
– Multiplication `*`
– Division `/`
– Modulus `%`
– Exponentiation `**`
– Floor Division `//`

“`python
# Addition
result_addition = 10 + 5 # Result: 15

# Subtraction
result_subtraction = 10 – 5 # Result: 5

# Multiplication
result_multiplication = 10 * 5 # Result: 50

# Division
result_division = 10 / 5 # Result: 2.0

# Modulus
result_modulus = 10 % 3 # Result: 1

# Exponentiation
result_exponentiation = 2 ** 3 # Result: 8

# Floor Division
result_floor_division = 10 // 3 # Result: 3
“`

Assignment Operators

Assignment operators are used to assign values to variables.

– `=`
– `+=`
– `-=`
– `*=`
– `/=`
– `%=`
– `//=`
– `**=`

“`python
x = 10 # Assigns the value 10 to x

x += 5 # Equivalent to x = x + 5

x -= 3 # Equivalent to x = x – 3

x *= 2 # Equivalent to x = x * 2

x /= 4 # Equivalent to x = x / 4

x %= 3 # Equivalent to x = x % 3

x //= 2 # Equivalent to x = x // 2

x **= 3 # Equivalent to x = x ** 3
“`

Comparison Operators

Comparison operators are used to compare values.

– Equal to `==`
– Not equal to `!=`
– Greater than `>`
– Less than `<`
– Greater than or equal to `>=`
– Less than or equal to `<=`

“`python
x = 5
y = 10

print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True
“`

Logical Operators

Logical operators are used to combine conditional statements.

– `and`
– `or`
– `not`

“`python
x = True
y = False

print(x and y) # False
print(x or y) # True
print(not x) # False
“`

Identity Operators

Identity operators are used to compare the memory locations of two objects.

– `is`
– `is not`

“`python
x = [“apple”, “banana”]
y = [“apple”, “banana”]
z = x

print(x is z) # True
print(x is y) # False
print(x is not y) # True
“`

Membership Operators

Membership operators in Python are used to test whether a value or variable is found within a sequence, such as strings, lists, tuples, or sets. Python provides two membership operators: `in` and `not in`. These operators return a Boolean value (`True` or `False`) based on whether the specified value exists in the given sequence.

Here’s a brief explanation of both membership operators:

1. **`in` Operator:**
– The `in` operator checks if a value exists in a sequence.
– It returns `True` if the value is found in the sequence.
– Otherwise, it returns `False`.
– Example:
“`python
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
print(6 in my_list) # Output: False
“`

2. **`not in` Operator:**
– The `not in` operator checks if a value does not exist in a sequence.
– It returns `True` if the value is not found in the sequence.
– Otherwise, it returns `False`.
– Example:
“`python
my_string = “hello”
print(‘e’ not in my_string) # Output: False
print(‘x’ not in my_string) # Output: True
“`

“`python
x = [“apple”, “banana”]

print(“banana” in x) # True
print(“orange” not in x) # True
“`

 

 

Membership operators are commonly used when you need to search for specific elements in collections like lists, tuples, strings, and sets. They provide a convenient way to check for the presence or absence of elements within these sequences, allowing for concise and readable code.

 

Join the conversation