Operators in Python
Python में Operators
What are Operators?
Operators are symbols that perform operations on values (called operands). 5 + 3 uses the + operator on operands 5 and 3.
Arithmetic Operators
| Op | Meaning | Example | Result |
|---|---|---|---|
| + | Add | 5 + 2 | 7 |
| - | Subtract | 5 - 2 | 3 |
| * | Multiply | 5 * 2 | 10 |
| / | Divide (float) | 5 / 2 | 2.5 |
| // | Floor divide | 5 // 2 | 2 |
| % | Modulus (remainder) | 5 % 2 | 1 |
| ** | Power | 5 ** 2 | 25 |
Comparison Operators
Return True or False: == (equal), != (not equal), >, <, >=, <=.
Logical Operators
and (both true), or (at least one true), not (reverse).
Assignment Operators
= assigns. Shortcuts: +=, -=, *=, /=. So x += 5 means x = x + 5.
Program 1: Arithmetic
a = 17
b = 5
print("Sum :", a + b)
print("Quotient :", a // b)
print("Remainder:", a % b)
print("Power :", a ** 2)//gives the whole-number part of division (3, not 3.4).%gives the remainder (17 = 5*3 + 2).**is power: 17 squared = 289.
Program 2: Comparison & Logical
age = 20 has_id = True print(age >= 18) # comparison print(age >= 18 and has_id) # both must be true print(age < 13 or has_id) # one true is enough print(not has_id) # reverse
andneeds both sides true.oris true if any side is true.notflips True to False.
Program 3: Even/Odd & Leap Year
num = int(input("Enter a number: "))
# Even/Odd using modulus
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
# Leap year check using logical operators
year = int(input("Enter a year: "))
is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(year, "leap year?", is_leap)num % 2 == 0is the classic even-number test.- The leap-year rule combines
and,orand!=in one expression.
Operator Precedence
Python follows BODMAS-like rules: ** first, then * / // %, then + -, then comparisons, then not, and, or. Use brackets to be safe: (a + b) * c.
Common Mistakes
- Using
=(assign) instead of==(compare) in conditions. - Expecting
/to give a whole number — use//for that. - Writing
&&/||(C style) instead ofand/or.
Practice Tasks
- Take two numbers and print all 7 arithmetic results.
- Check if a number is divisible by both 3 and 5 using
and. - Ask age and check eligibility to vote (>= 18).
- Swap two numbers using
+=style operators.
Summary
- Arithmetic:
+ - * / // % **. - Comparison returns bool; logical:
and or not. %for even/odd,//for whole-number division.- Use brackets to control precedence.
Operators क्या हैं?
Operators वे चिह्न हैं जो values (operands) पर operation करते हैं। 5 + 3 में + operator, operands 5 और 3 पर काम करता है।
Arithmetic Operators
| Op | मतलब | Example | Result |
|---|---|---|---|
| + | जोड़ | 5 + 2 | 7 |
| - | घटाव | 5 - 2 | 3 |
| * | गुणा | 5 * 2 | 10 |
| / | भाग (float) | 5 / 2 | 2.5 |
| // | Floor भाग | 5 // 2 | 2 |
| % | शेषफल | 5 % 2 | 1 |
| ** | घात (power) | 5 ** 2 | 25 |
Comparison Operators
True या False लौटाते हैं: == (बराबर), != (बराबर नहीं), >, <, >=, <=।
Logical Operators
and (दोनों true), or (कम से कम एक true), not (उल्टा)।
Assignment Operators
= assign करता है। Shortcuts: +=, -=, *=, /=। यानी x += 5 का मतलब x = x + 5।
Program 1: Arithmetic
a = 17
b = 5
print("Sum :", a + b)
print("Quotient :", a // b)
print("Remainder:", a % b)
print("Power :", a ** 2)//भाग का पूर्ण भाग देता है (3, न कि 3.4)।%शेषफल देता है (17 = 5*3 + 2)।**power है: 17 का वर्ग = 289।
Program 2: Comparison & Logical
age = 20 has_id = True print(age >= 18) # comparison print(age >= 18 and has_id) # दोनों true हों print(age < 13 or has_id) # एक true काफी print(not has_id) # उल्टा
andको दोनों तरफ true चाहिए।ortrue है अगर कोई एक तरफ true हो।notTrue को False में बदल देता है।
Program 3: Even/Odd और Leap Year
num = int(input("Enter a number: "))
# Modulus से Even/Odd
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
# Logical operators से Leap year
year = int(input("Enter a year: "))
is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(year, "leap year?", is_leap)num % 2 == 0classic even-number test है।- Leap-year नियम एक ही expression में
and,orऔर!=जोड़ता है।
Operator Precedence
Python BODMAS जैसे नियम follow करता है: पहले **, फिर * / // %, फिर + -, फिर comparisons, फिर not, and, or। सुरक्षित रहने के लिए brackets use करें: (a + b) * c।
सामान्य गलतियाँ
- conditions में
==की जगह=लगाना। /से पूर्ण संख्या की उम्मीद करना — उसके लिए//use करें।and/orकी जगह&&/||(C style) लिखना।
Practice Tasks
- दो numbers लेकर सातों arithmetic results print करें।
andसे check करें कि number 3 और 5 दोनों से divisible है या नहीं।- Age पूछकर vote की eligibility check करें (>= 18)।
+=style operators से दो numbers swap करें।
सारांश
- Arithmetic:
+ - * / // % **। - Comparison bool लौटाता है; logical:
and or not। - even/odd के लिए
%, पूर्ण भाग के लिए//। - Precedence control के लिए brackets use करें।