Python Operators Complete Notes

Python Operators Complete Notes

Complete Explanation (સંપૂર્ણ સમજૂતી)

1. Arithmetic (ગણિતીય)

👉 Used for mathematical operations (ગણિતના દાખલા ગણવા માટે).

  • + - * / : Addition, Subtraction, Multiplication, Division (સરવાળો, બાદબાકી, ગુણાકાર, ભાગાકાર).
  • % (Modulo) : Returns the remainder after division (ભાગાકાર પછી વધેલી શેષ આપે છે). Used to check Even/Odd (Even/Odd ચેક કરવા વપરાય છે).
  • ** (Power) : Used to find the exponent/power (ઘાત શોધવા માટે) (e.g., 2**3 = 8).
a = 10 b = 3 print(a + b) # 13 (Addition / સરવાળો) print(a / b) # 3.33 (Division / ભાગાકાર) print(a // b) # 3 (Floor Division / પૂર્ણાંક ભાગાકાર) print(a % b) # 1 (Remainder / શેષ) print(a ** 2) # 100 (Power / ઘાત)
📌 Exam Tip: / always returns a decimal result (હંમેશા decimal જવાબ આપે છે), while // removes the decimal point and returns only an integer (પોઈન્ટ કાઢીને માત્ર પૂર્ણાંક આપે છે).

2. Comparison (તુલનાત્મક)

👉 Used to compare values (કિંમતોની સરખામણી કરવા). The result is always True or False (જવાબ હંમેશા True અથવા False આવે છે). Mostly used to check conditions (શરતો ચેક કરવા સૌથી વધુ વપરાય છે).

x = 5 y = 3 print(x > y) # True (Greater than / મોટું છે) print(x < y) # False (Less than / નાનું છે) print(x == y) # False (Equal to / બંને સરખા છે?) print(x != y) # True (Not equal to / સરખા નથી?) # Strings can also be compared (Strings પણ compare કરી શકાય છે) print("Apple" == "apple") # False (Case-sensitive)

Note: == is used for comparison (સરખામણી કરવા માટે છે), while = is used to assign a value (વેલ્યુ અસાઇન કરવા માટે છે).

3. Logical (લોજિકલ)

👉 Used to combine conditional statements (એક કરતા વધુ શરતો ભેગી કરવા).

💡 Real-life Example: During login, both Username and Password must be correct (બંને સાચા હોવા જોઈએ). If only one of the options is needed, or is used (જો બેમાંથી કોઈ એક જ ઓપ્શન જોઈતો હોય તો or વપરાય).
x = 10 print(x > 5 and x < 20) # True print(x > 50 or x < 20) # True print(not(x == 10)) # False (Reverses True to False)
  • and: Both conditions must be True (બંને શરતો True હોવી જરૂરી છે).
  • or: At least one condition must be True (કોઈ એક શરત True હોય તો પણ ચાલે).
  • not: Reverses the result (જવાબ ઊંધો કરી દે - True નું False).

4. Assignment (સોંપવું)

👉 Shortcut for updating a variable's value (વેલ્યુ અપડેટ કરવાનો શોર્ટકટ). This makes the code shorter and cleaner (આનાથી કોડ ટૂંકો અને ક્લીન બને છે).

x = 10 # Direct value assigned (સીધી કિંમત આપી) x += 5 # Same as (જેવું જ છે): x = x + 5 (15) x -= 2 # Same as: x = x - 2 (13) x *= 2 # Same as: x = x * 2 (26) x /= 2 # Same as: x = x / 2 (13.0) print(x) # 13.0

5. Bitwise (બિટ લેવલ)

👉 Used to work directly on Binary numbers (0 & 1 પર સીધું કામ કરવા માટે). These operations are very fast (આ ઓપરેશન ખુબ ઝડપી હોય છે).

a = 5 # Binary: 101 b = 3 # Binary: 011 print(a & b) # 1 (001) - AND print(a | b) # 7 (111) - OR print(a << 1) # 10 (Left Shift - like multiplication / ગુણાકાર જેવું) print(a >> 1) # 2 (Right Shift - like division / ભાગાકાર જેવું)

6. Membership & Identity

Membership (in, not in): Checks if a value is present inside a String, List, or Tuple (ચેક કરે છે કે કોઈ વેલ્યુ અંદર રહેલી છે કે નહિ).

text = "python" print('p' in text) # True print('z' not in text) # True

Identity (is, is not): Checks if both variables point to the same memory location (ચેક કરે છે કે બંને વેલ્યુ એક જ મેમરી લોકેશન પર છે કે નહિ).

x = [1, 2] y = x z = [1, 2] print(x is y) # True (Same memory / એક જ મેમરી) print(x is z) # False (Different memory / અલગ મેમરી)

Note: x == z will be True because values are the same (કારણ કે વેલ્યુ સરખી છે), but x is z will be False because memory locations are different (કારણ કે મેમરી લોકેશન અલગ છે).

Interactive Python Console

Try simple mathematical expressions or print statements:

Output:

Knowledge Test (MCQ Quiz)

Certificate Locked

Score 60% or more in the Knowledge Test above to get your certificate (સર્ટિફિકેટ મેળવવા માટે ઉપરની ક્વિઝમાં 60% કે તેથી વધુ માર્ક્સ લાવો).

Claim Your Certificate

Generate your certificate for completing the course (કોર્સ પૂરો કરવા બદલ તમારું સર્ટિફિકેટ જનરેટ કરો).

Post a Comment

0 Comments