Skip to main content
  1. Notes/
  2. Python ๐Ÿ/

โž•โž– Operators in Python | Lesson 03

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 3: This Article

โž•โž– Operators in Python
#

Get ready to supercharge your code with operators! Operators let you perform calculations, compare values, and work with logic in Python. Theyโ€™re like the tools ๐Ÿ› ๏ธ youโ€™ll use for almost everything.


What Are Operators? ๐Ÿค”
#

Operators are special symbols or keywords that help you perform operations on variables and values.


1. Arithmetic Operators ๐Ÿ”ข
#

Used for math calculations.

OperatorMeaningExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
%Modulus7 % 21
//Floor Division7 // 23
**Exponent2 ** 38

Practice:

print(5 + 4)   # 9
print(10 % 3)  # 1

2. Comparison Operators ๐Ÿง
#

Compare values and return True or False.

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater or equal5 >= 5True
<=Less or equal3 <= 5True

Practice:

print(7 > 2)    # True
print(5 == 4)   # False

3. Logical Operators ๐Ÿง 
#

Combine multiple conditions.

OperatorMeaningExampleResult
andBoth True(5 > 2) and (3 < 4)True
orEither(5 < 2) or (3 < 4)True
notOppositenot(5 == 5)False

Practice:

print(True and False)   # False
print(not (2 > 1))      # False

4. Assignment Operators ๐Ÿ–Š๏ธ
#

Assign and update values.

OperatorExampleEquivalent To
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 4x = x * 4
/=x /= 2x = x / 2

Practice:

score = 10
score += 5   # score is now 15

5. Membership Operators ๐Ÿ”
#

Check if a value is in a sequence.

OperatorExampleResult
in‘a’ in ‘apple’True
not in3 not inTrue

Practice Questions with Solutions ๐Ÿ†
#

  1. Practice: Write code to check if 6 is divisible by 3.
    • Solution:
print(6 % 3 == 0)   # True
  1. Practice: If x = 3, what is the result of x ** 2 + 1?
    • Solution:
x = 3
print(x ** 2 + 1)   # 10
  1. Practice: Check if the word “cat” is in “education”.
    • Solution:
print("cat" in "education")   # True

Step-by-Step Challenge: Score Calculator ๐ŸŽฎ
#

Task: Start with score = 0. Add 10, subtract 2, multiply by 3. Print the final score.

Solution:

score = 0
score += 10    # 10
score -= 2     # 8
score *= 3     # 24
print("Final Score:", score)

Output:

Final Score: 24

Well done! Youโ€™re now a pro at using operators in Python! ๐Ÿฅณ Keep practicing and try creating mini calculators or conditional games!

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 3: This Article

Related

โฐ Working with Date and Time in Python | Lesson 16
Python Progmramming Course
๐ŸŽฌ Strings in Python | Lesson 04
Python Progmramming Course
๐Ÿ—๏ธ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15
Python Progmramming Course
๐Ÿ‘ถ Python Basics | Lesson 02
Python Progmramming Course
๐Ÿ“„ File Handling in Python | Lesson 14
Python Progmramming Course
๐Ÿ“‹ Lists in Python | Lesson 05
Python Progmramming Course