Basic Operators
Introduction
Python, one of the most popular programming languages, offers a wide range of operators that allow you to perform various operations on different data types. Whether you’re a beginner or an experienced programmer, having a solid understanding of operators in Python is essential for writing efficient and effective code. In this blog post, we will explore the different types of operators in Python like arithmetic operators, comparison operators, logical operators, assignment operators, and bitwise operators, and how to use them effectively in your programs.
Sections
- What is mean by operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Identity operators
- Membership Operators
- Using Operators with Strings
- Using Operators with Lists
- Type of Variable
Section 1- What is operators?
operators are used to perform operation on variables and values In the example below, we used the + operator to add together two values.

1- Arithmetic Operators
This section explains how to use basic arithmetic operators in Python. Arithmetic operators in Python are used to perform mathematical operations on two operands. Just as in any other programming language, addition, subtraction, multiplication, and division operators can be used with numbers. Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more. Here are the commonly used arithmetic operators in Python:

- Addition (+): Used to add two values together.
- Subtraction (-): Used to subtract one value from another.
- Multiplication (*) Used to multiply two values together.
- Division (/): Used to divide one value by another.
- Modulo (%): Returns the remainder of a division operation.
- Exponentiation (**): Raises a value to the power of another value.
- Floor Division (//): Returns the quotient of a division operation, rounded down to the nearest whole number.
print(2 + 3) # addition(+)
print(3 - 1) # subtraction(-)
print(2 * 3) # multiplication(*)
print(3 / 2) # division(/)
print(3 ** 2) # exponential(**)
print(3 % 2) # modulus(%)
print(3 // 2) # Floor division operator(//)
# Example usage of arithmetic operators
number = 1 + 2 * 3 / 4.0
print(number) # Output: 2.5
a = 10
b = 3
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
modulo = a % b
exponentiation = a ** b
floor_division = a // b
print(addition) # Output: 13
print(subtraction) # Output: 7
print(multiplication) # Output: 30
print(division) # Output: 3.3333333333333335
print(modulo) # Output: 1
print(exponentiation) # Output: 1000
print(floor_division) # Output: 3
# Another operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder remainder = 11 % 3 print(remainder) # Output: 2
# Using two multiplication symbols makes a power relationship. squared = 7 ** 2 cubed = 2 ** 3 print(squared) # Output: 49 print(cubed) # Output: 8
2. Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True or False). These operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

For example:
Let suppose x = 5 and y = 10
print(x == y) # False : Equal to (==) :Checks if two values are equal.
print(x != y) # True : Not equal to (!=) :Checks if two values are not equal.
print(x > y) # False : Greater than (>): Checks if one value is greater than another.
print(x < y) # True : Less than (<): Checks if one value is less than another.
print(x >= y) # False : Greater than or equal to (>=): Checks if one value is greater than or equal to another.
print(x <= y) # True : Less than or equal to (<=): Checks if one value is less than or equal to another.
3. Logical Operators
Logical operators are used to combine multiple conditions and return a Boolean value. The three logical operators in Python are “and”, “or”, and “not”. Here’s how they work:

-
“and” Operator: Returns True if both conditions on either side of the operator are true.
-
“or” Operator: Returns True if at least one condition on either side of the operator is true.
-
“not” Operator: Returns the opposite of the Boolean value of the condition.
For example:
Let suppose x = 5 and y = 10 then:
print(x > 0 and y < 20) # True
print(x > 0 or y < 20) # True
print(not(x > 0)) # False
4. Assignment Operators
Assignment operators are used to assign values to variables. In addition to the basic assignment operator (=), Python also provides compound assignment operators such as “+=”, “-=”, “*=”, “/=”, “%=”, “**=”, etc.
For example:

Let suppose x = 5 then:
x += 3 # Equivalent to x = x + 3
print(x) # Output: 8
if y = 10
y -= 5 # Equivalent to y = y - 5
print(y) # Output: 5
5. Bitwise Operators
Bitwise operators are used to perform operations on individual bits of binary numbers. These operators include bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise complement (~), left shift (<<), and right shift (>>).

For Example
x = 5 # Binary representation: 0101
y = 3 # Binary representation: 0011
print(x & y) # Bitwise AND: 0001 (Decimal: 1)
print(x | y) # Bitwise OR: 0111 (Decimal: 7)
print(x ^ y) # Bitwise XOR: 0110 (Decimal: 6)
print(~x) # Bitwise NOT: -6 (Decimal: -6)
print(x << 1) # Left Shift: 1010 (Decimal: 10)
print(x >> 1) # Right Shift: 0010 (Decimal: 2)
Identity operators
Identity operators are used to compare the objects, not if they are equal but if they are actually same object with same memory location.
# Integers (small integers are often stored as singletons)
a = 5
b = 5
print(a is b) # True (they might share the same memory location)
# Strings (immutable, often reused if the same value exists)
c = "hello"
d = "hello"
print(c is d) # True (they might share the same memory location)
# Lists (mutable, each list has its own memory location)
e = [1, 2, 3]
f = [1, 2, 3]
print(e is f) # False (even though they have the same values, they are different objects)
# Checking with `id()`
print(id(a)) # Same for `a` and `b` if they share memory
print(id(c)) # Same for `c` and `d` if they share memory
print(id(e)) # Different for each `list` object
Membership Operators
Membership operators are used to test if a sequence is presented in an object

my_list = [1, 2, 3, 4]
print(2 in my_list) # True
my_string = "Hello World!"
print("lo" in my_string) # True
my_tuple = (5, 7, 9)
print(7 in my_tuple) # True
my_set = {10, 12, 14}
print(12 in my_set) # True
my_dict = {"name": "Alice", "age": 30}
print("Alice" in my_dict) # True (checks keys, not values)
6 -Using Operators with Strings
Python supports concatenating strings using the addition operator:
helloworld = "hello" + " " + "world"
print(helloworld)
lotsofhellos = "hello" * 10 print(lotsofhellos)
7- Using Operators with Lists
even_numbers = [2,4,6,8] odd_numbers = [1,3,5,7] all_numbers = odd_numbers + even_numbers print(all_numbers)
# Just as in strings, Python supports forming new lists with a repeating sequence using the multiplication operator:
print([1,2,3] * 3)
Section 11-Type of Variable
There are two main ways to find the type of a variable in Python:
1. Using the built-in type() function:
The type() function returns the type of the object passed as an argument. You can simply call it with the variable name as an argument to get the type
x = 10
print(type(x)) # Output: <class 'int'>
y = "Hello"
print(type(y)) # Output: <class 'str'>
z = True
print(type(z)) # Output: <class 'bool'>
# Checking data types
print(type(10)) # Int
print(type(3.14)) # Float
print(type(1 + 3j)) # Complex
print(type('Asabeneh')) # String
print(type([1, 2, 3])) # List
print(type({'name':'Asabeneh'})) # Dictionary
print(type({9.8, 3.14, 2.7})) # Set
print(type((9.8, 3.14, 2.7))) # Tuple
2. Using the isinstance() function:
The isinstance() function checks if an object is an instance of a specific type. This is useful when you want to check if a variable belongs to a subclass of a particular type.
a = 3.14
print(isinstance(a, float)) # Output: True
b = "Python"
print(isinstance(b, str)) # Output: True
c = [1, 2, 3]
print(isinstance(c, list)) # Output: True
# Checking if an object is an instance of a subclass
d = ["Apple", "Banana", "Orange"]
print(isinstance(d, list)) # Output: True
print(isinstance(d, tuple)) # Output: False
Both methods are simple to use and have their advantages. The type() function is more concise and efficient for quickly checking the type of a variable. The isinstance() function is more versatile and allows you to check for specific subclasses as well.
Conclusion
In this blog post, we have explored the various types of operators available in Python, including arithmetic, comparison, logical, assignment, and bitwise operators. Understanding how these operators work and how to use them effectively is crucial for developing efficient and reliable Python programs. By mastering these operators, you can manipulate data, make decisions, and perform complex calculations with ease. So go ahead, experiment with these operators, and unleash the full potential of Python programming!
