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.
total = 80 + 15 # + is an arithmetic operator result = total >= 33 # >= is a comparison operator
Types of Operators
| Type | Operators | Use |
|---|---|---|
| Arithmetic | + - * / // % ** | Mathematical calculation |
| Assignment | = += -= *= /= | Store or update value |
| Comparison | == != > < >= <= | Compare two values and return True/False |
| Logical | and or not | Combine conditions |
| Membership | in, not in | Check value inside list/string/tuple |
| Identity | is, is not | Check whether two variables refer to the same object |
Arithmetic Operators
Arithmetic operators are used for normal mathematical work.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3.333... |
// | Floor Division | 10 // 3 | 3 |
% | Modulus / Remainder | 10 % 3 | 1 |
** | Power | 2 ** 3 | 8 |
Comparison and Logical Operators
Comparison operators return True or False. Logical operators combine two or more conditions.
marks = 76 attendance = 82 print(marks >= 33) print(marks >= 33 and attendance >= 75) print(marks < 33 or attendance < 75) print(not marks < 33)
Assignment Operators
Assignment operators are used to store or update values.
total = 0 total += 50 # same as total = total + 50 total += 25 print(total)
Membership Operators
in and not in check whether a value exists inside a sequence such as string, list or tuple.
subjects = ["Hindi", "English", "Computer"]
print("English" in subjects)
print("Maths" not in subjects)Operator Precedence
Python follows a priority order. Multiplication happens before addition. Use brackets when you want to make the order clear.
print(10 + 5 * 2) print((10 + 5) * 2)
Complete Program: Marks and Eligibility Check
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
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
- Write a program to calculate total and percentage of 5 subjects.
- Check whether a student is pass if marks are at least 33 in each subject.
- Use
into check whether a subject is available in a subject list. - 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 के प्रकार
| Type | Operators | Use |
|---|---|---|
| Arithmetic | + - * / // % ** | Calculation |
| Assignment | = += -= *= /= | Value store/update |
| Comparison | == != > < >= <= | Values compare करके True/False देना |
| Logical | and or not | Multiple conditions combine करना |
| Membership | in, not in | List/string में value check करना |
Arithmetic Operators
ये operators mathematical calculation के लिए use होते हैं।
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3.333... |
// | Floor Division | 10 // 3 | 3 |
% | Remainder | 10 % 3 | 1 |
** | Power | 2 ** 3 | 8 |
Complete Program: Marks and Eligibility
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
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
- 5 subjects का total और percentage calculate करें।
- हर subject में 33 marks होने पर pass/fail check करें।
- Subject list में
inoperator से subject search करें। - Arithmetic, comparison और logical operators के 5-5 examples बनाएं।
सारांश
Operators Python के working tools हैं। इनसे calculation, comparison, value update, condition checking और membership checking की जाती है।