Operators - Python Basics

Operators - Python Basics

Operators in Python are special symbols or keywords that perform operations on variables and values. They are the building blocks of expressions and logic in programming. In this guide, we will explore different types of Python operators with clear examples so you can use them effectively in your projects.


Types of Operators

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

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more.

Operator Description Example
+Addition5 + 3 → 8
-Subtraction5 - 3 → 2
*Multiplication5 * 3 → 15
/Division5 / 2 → 2.5
//Floor Division5 // 2 → 2
%Modulus5 % 2 → 1
**Exponentiation2 ** 3 → 8
# Arithmetic Operators Example
x = 10
y = 3
print(x + y)  # 13
print(x - y)  # 7
print(x * y)  # 30
print(x / y)  # 3.333...
print(x // y) # 3
print(x % y)  # 1
print(x ** y) # 1000
Tip: Use // when you want an integer result without decimals, especially in loops and indexes.

2. Assignment Operators

Assignment operators are used to assign values to variables, with options to perform operations simultaneously.

Operator Description Example
=Assign valuex = 5
+=Add and assignx += 3 → x = x + 3
-=Subtract and assignx -= 3 → x = x - 3
*=Multiply and assignx *= 3 → x = x * 3
/=Divide and assignx /= 3 → x = x / 3
//=Floor divide and assignx //= 3
%=Modulus and assignx %= 3
**=Exponent and assignx **= 3
# Assignment Operators Example
a = 5
a += 2   # a = 7
a -= 1   # a = 6
a *= 3   # a = 18
a /= 2   # a = 9.0
print(a)
Tip: Assignment operators make code shorter and more readable, especially inside loops and iterative calculations.

3. Comparison Operators

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

Operator Description Example
==Equal to5 == 3 → False
!=Not equal to5 != 3 → True
>Greater than5 > 3 → True
<Less than5 < 3 → False
>=Greater or equal to5 >= 3 → True
<=Less or equal to5 <= 3 → False
# Comparison Operators Example
x = 10
y = 20
print(x == y)  # False
print(x != y)  # True
print(x > y)   # False
print(x < y)   # True
print(x >= 10) # True

4. Logical Operators

Logical operators are used to combine conditional statements.

  • and – Returns True if both statements are true.
  • or – Returns True if at least one statement is true.
  • not – Reverses the result, returns False if result is true.
# Logical Operators Example
x = 5
print(x > 3 and x < 10)  # True
print(x > 8 or x == 5)   # True
print(not(x > 3))        # False

5. Identity Operators

Identity operators compare memory locations, not just values.

  • is – Returns True if both variables point to the same object.
  • is not – Returns True if they are not the same object.
# Identity Operators Example
a = ["apple", "banana"]
b = a
c = ["apple", "banana"]

print(a is b)       # True (same object)
print(a is c)       # False (different objects)
print(a is not c)   # True

6. Membership Operators

Membership operators check if a value exists in a sequence.

  • in – Returns True if the value exists.
  • not in – Returns True if the value does not exist.
# Membership Operators Example
fruits = ["apple", "banana"]
print("apple" in fruits)      # True
print("grape" not in fruits)  # True

7. Bitwise Operators

Bitwise operators work on binary representations of integers.

Operator Description Example
&AND5 & 3 → 1
|OR5 | 3 → 7
^XOR5 ^ 3 → 6
~NOT~5 → -6
<<Left Shift5 << 1 → 10
>>Right Shift5 >> 1 → 2
# Bitwise Operators Example
x = 5   # 0101
y = 3   # 0011
print(x & y)  # 1  (0001)
print(x | y)  # 7  (0111)
print(x ^ y)  # 6  (0110)
print(~x)     # -6 (two's complement)
print(x << 1) # 10 (1010)
print(x >> 1) # 2  (0010)

Final Thoughts

Understanding Python operators is essential for writing efficient and powerful code. From basic arithmetic to advanced bitwise manipulation, these tools allow you to process data, control logic, and create smarter programs. Practice them with small projects to solidify your skills.

👉 Continue Learning: Explore more beginner-friendly topics and practice materials in our Programming Resource Hub.

s

Post a Comment

0 Comments