🟢 Beginner  ·  Lesson 07

Operators in Python

Python में Operators

What are Operators in Python?

Operators are special symbols or keywords that tell Python to perform an operation on values or variables. For example, + adds two numbers, > compares two values, and and combines two conditions.

Without operators, a program cannot calculate marks, compare age, check pass/fail condition, update totals, or make logical decisions.

💡 Simple Example
total = 80 + 15       # + is an arithmetic operator
result = total >= 33  # >= is a comparison operator

Types of Operators

TypeOperatorsUse
Arithmetic+ - * / // % **Mathematical calculation
Assignment= += -= *= /=Store or update value
Comparison== != > < >= <=Compare two values and return True/False
Logicaland or notCombine conditions
Membershipin, not inCheck value inside list/string/tuple
Identityis, is notCheck whether two variables refer to the same object

Arithmetic Operators

Arithmetic operators are used for normal mathematical work.

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.333...
//Floor Division10 // 33
%Modulus / Remainder10 % 31
**Power2 ** 38

Comparison and Logical Operators

Comparison operators return True or False. Logical operators combine two or more conditions.

Python
marks = 76
attendance = 82

print(marks >= 33)
print(marks >= 33 and attendance >= 75)
print(marks < 33 or attendance < 75)
print(not marks < 33)
True True False True

Assignment Operators

Assignment operators are used to store or update values.

Python
total = 0

total += 50   # same as total = total + 50
total += 25
print(total)
75

Membership Operators

in and not in check whether a value exists inside a sequence such as string, list or tuple.

Python
subjects = ["Hindi", "English", "Computer"]

print("English" in subjects)
print("Maths" not in subjects)
True True

Operator Precedence

Python follows a priority order. Multiplication happens before addition. Use brackets when you want to make the order clear.

Python
print(10 + 5 * 2)
print((10 + 5) * 2)
20 30

Complete Program: Marks and Eligibility Check

Python – operators.py
maths = 78
english = 84
computer = 91
attendance = 80

total = maths + english + computer
percentage = total / 3

print("Total Marks:", total)
print("Percentage:", percentage)

is_pass = percentage >= 33 and attendance >= 75
print("Eligible for promotion:", is_pass)

Expected Output

Total Marks: 253 Percentage: 84.33333333333333 Eligible for promotion: True

Common Mistakes

  • Using = instead of == while comparing values.
  • Thinking / and // give the same result. / gives normal division; // gives floor division.
  • Forgetting brackets in mixed calculations like (a + b) * c.
  • Using logical operators without understanding True/False conditions.

Practice Tasks

  1. Write a program to calculate total and percentage of 5 subjects.
  2. Check whether a student is pass if marks are at least 33 in each subject.
  3. Use in to check whether a subject is available in a subject list.
  4. Write 5 examples each for arithmetic, comparison and logical operators.

Summary

Operators are the working tools of Python. Arithmetic operators calculate, comparison operators compare, logical operators combine conditions, assignment operators update values, and membership operators check whether a value exists in a sequence.

Python Operators क्या होते हैं?

Operators ऐसे symbols या keywords होते हैं जो Python को बताते हैं कि values पर कौन-सा operation करना है। जैसे + जोड़ने के लिए, > compare करने के लिए और and दो conditions जोड़ने के लिए use होता है।

Operators के बिना program marks calculate नहीं कर सकता, pass/fail check नहीं कर सकता और logical decision नहीं ले सकता।

Operators के प्रकार

TypeOperatorsUse
Arithmetic+ - * / // % **Calculation
Assignment= += -= *= /=Value store/update
Comparison== != > < >= <=Values compare करके True/False देना
Logicaland or notMultiple conditions combine करना
Membershipin, not inList/string में value check करना

Arithmetic Operators

ये operators mathematical calculation के लिए use होते हैं।

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.333...
//Floor Division10 // 33
%Remainder10 % 31
**Power2 ** 38

Complete Program: Marks and Eligibility

Python – operators.py
maths = 78
english = 84
computer = 91
attendance = 80

total = maths + english + computer
percentage = total / 3

print("Total Marks:", total)
print("Percentage:", percentage)

is_pass = percentage >= 33 and attendance >= 75
print("Eligible for promotion:", is_pass)

Expected Output

Total Marks: 253 Percentage: 84.33333333333333 Eligible for promotion: True

Program कैसे काम करता है?

  • + तीन subjects के marks जोड़ता है।
  • / total को 3 से divide करके percentage निकालता है।
  • >= percentage और attendance को required limit से compare करता है।
  • and दोनों conditions true होने पर final result True देता है।

Common Mistakes

  • Comparison के लिए == की जगह गलती से = use करना।
  • / और // को same समझना।
  • Mixed calculation में brackets न लगाना।
  • Logical operators में True/False condition clearly न समझना।

Practice Tasks

  1. 5 subjects का total और percentage calculate करें।
  2. हर subject में 33 marks होने पर pass/fail check करें।
  3. Subject list में in operator से subject search करें।
  4. Arithmetic, comparison और logical operators के 5-5 examples बनाएं।

सारांश

Operators Python के working tools हैं। इनसे calculation, comparison, value update, condition checking और membership checking की जाती है।

← Back to Python Tutorial