Python Chapter - 4 Notes

Python Operators and Expressions – Complete Beginner's Guide

An operator in Python is a special symbol that performs operations on values called operands to produce a result.
Python में ऑपरेटर विशेष चिन्ह होते हैं जो मान (operand) पर कार्य करके परिणाम देते हैं।


Types of Python Operators / Python ऑपरेटर के प्रकार

Python operators are categorized by how many operands they handle:
Python ऑपरेटर को उनके उपयोग में आने वाले मानों की संख्या के आधार पर वर्गीकृत किया जाता है:

  • Unary Operator: Works with one operand (e.g., -x)
    एकल मान वाले ऑपरेटर (जैसे: -x)
  • Binary Operator: Works with two operands (e.g., a + b)
    द्वि मान वाले ऑपरेटर (जैसे: a + b)
  • Ternary Operator: Works with three operands (conditional) (e.g., x if condition else y)
    त्रि मान वाले ऑपरेटर (शर्तीय, जैसे: x if condition else y)

Arithmetic Operators / अंकगणितीय ऑपरेटर

These operators perform basic math calculations:
ये ऑपरेटर गणितीय गणना करते हैं:

Operator Meaning Example Result
+Addition / जोड़2 + 35
-Subtraction / घटाव5 - 23
*Multiplication / गुणा4 * 312
/Division / भाग10 / 25.0
%Modulus (Remainder) / शेषफल7 % 31
**Exponent (Power) / घात2 ** 38
//Floor Division / पूर्णांक भाग9 // 24

Operator Precedence / ऑपरेटर प्राथमिकता

Python evaluates operators by the following priorities:
Python निम्न प्राथमिकताओं के अनुसार ऑपरेटरों का मूल्यांकन करता है:

  1. Parentheses ( ) (कोष्ठक)
  2. Exponentiation ** (घातांक)
  3. Multiplication, Division, Modulus, Floor Division (गुणा, भाग, शेषफल, पूर्णांक भाग)
  4. Addition and Subtraction (जोड़ और घटाव)

Example:
उदाहरण:
2 + 3 * 4 = 14 (multiplication before addition)
(2 + 3) * 4 = 20 (parentheses first)
(पहले गुणा और जोड़ या पहले कोष्ठक क्रम से)


Logical Operators / तार्किक ऑपरेटर

  • and: True if both conditions are true ✔ (जब दोनों शर्तें सही हों तो)
  • or: True if any condition is true ✔ (जब कोई भी एक शर्त सही हो तो)
  • not: Reverses the boolean value ✘ (सत्य को असत्य और असत्य को सत्य बनाता है)

Example / उदाहरण

print(5 > 3 and 2 < 4)    # Output: True
print(5 < 3 or 2 > 4)     # Output: False
print(not (5 == 5))          # Output: False

# 'and' returns True only if both conditions are true.
# 'or' returns True if at least one condition is true.
# 'not' reverses the boolean value.

# 'and' तभी True होता है जब दोनों शर्तें सही हों।
# 'or' तब True होता है जब कम से कम एक शर्त सही हो।
# 'not' बूलियन मान को उलट देता है।
  

Practice changing the values and observe how outputs change.
मान बदलकर अभ्यास करें और परिणाम देखें।

Comments