Python FunctionsPython Functions1

Python Exception HandlingPython Exception Handling1

🔍

Python Operators



Operators in Python are symbols or keywords that help you perform different kinds of actions, like calculations, comparisons, or checking conditions. Let’s explore each type of operator with easy-to-understand examples.

1. Arithmetic Operators

These are used to do basic math like addition, subtraction, multiplication, etc.

OperatorNameExampleResult
+Addition3 + 25
-Subtraction5 - 23
*Multiplication4 * 28
/Division6 / 23.0
%Modulus (Remainder)5 % 21
//Floor Division5 // 22
**Exponent (Power)2 ** 38

Example

a = 10
b = 3
print(a + b)    # 13
print(a ** b)   # 1000 (10³)

2. Comparison Operators

These are used to compare two values. The result is always either True or False.

OperatorMeaningExampleResult
==Equal to3 == 3True
!=Not equal to5 != 2True
>Greater than5 > 3True
<Less than2 < 4True
>=Greater or equal4 >= 4True
<=Less or equal3 <= 5True

Example

x = 7
y = 10
print(x < y)    # True
print(x == y)   # False

3. Assignment Operators

These are used to assign or update values of variables.

OperatorMeaningExample
=Assignx = 5
+=Add and assignx += 3 (same as x = x + 3)
-=Subtract and assignx -= 2
*=Multiply and assignx *= 4
/=Divide and assignx /= 2
%=Modulus and assignx %= 3
//=Floor divide and assignx //= 2
**=Power and assignx **= 2

Example

x = 4
x += 1
print(x)   # 5

4. Logical Operators

Used to combine multiple conditions.

OperatorMeaningExampleResult
andTrue if both are True5 > 3 and 4 > 2True
orTrue if at least one is True5 > 3 or 2 > 4True
notReverse the resultnot(5 == 5)False

Example

a = 7
print(a > 5 and a < 10)   # True
print(not a == 7)         # False

5. Identity Operators

Used to check if two variables point to the same object.

OperatorMeaningExampleResult
isTrue if both are same objecta is bTrue or False
is notTrue if not same objecta is not bTrue or False

Example

a = [1, 2]
b = a
print(a is b)       # True
print(a is not b)   # False

6. Membership Operators

Used to check if a value is inside a group (like a list or string).

OperatorMeaningExampleResult
inTrue if value is present"a" in "apple"True
not inTrue if value not present"x" not in "apple"True

Example

colors = ["red", "blue"]
print("red" in colors)      # True
print("green" not in colors) # True

7. Bitwise Operators (Just a Peek)

These work on binary (0s and 1s). You won’t need them yet, but here’s what they do:

OperatorName
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

Conclusion

Operators are the tools that help you build logic in your Python programs. Now that you’ve learned the basics, try using them in your own mini programs!



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M