Python Operators: Relational, Equality, and Logical Operators
Python Operators: Relational, Equality, and Logical Operators
Python mein operators ka use karke hum values ko compare, evaluate aur manipulate kar sakte hain. Is blog post mein hum discuss karenge relational operators, equality operators, aur logical operators ko, khaaskar non-boolean types ke sath unki performance ko dekhte hain.
Relational Operators
Relational operators use karke hum values ke beech comparison karte hain. Inka result True
ya False
hota hai.
>
(Greater Than):Usage: Check karta hai ki ek value doosri value se badi hai ya nahi.
Example:
print(5 > 3) # Output: True print("apple" > "banana") # Output: False
<
(Less Than):Usage: Check karta hai ki ek value doosri value se chhoti hai ya nahi.
Example:
print(3 < 5) # Output: True print("apple" < "banana") # Output: True
>=
(Greater Than or Equal To):Usage: Check karta hai ki ek value doosri value se barabar ya badi hai ya nahi.
Example:
print(5 >= 5) # Output: True print(4 >= 5) # Output: False
<=
(Less Than or Equal To):Usage: Check karta hai ki ek value doosri value se barabar ya chhoti hai ya nahi.
Example:
print(3 <= 5) # Output: True print(6 <= 5) # Output: False
Equality Operators
Equality operators values ko compare karte hain aur check karte hain ki kya dono values barabar hain ya nahi.
==
(Equal To):Usage: Check karta hai ki do values barabar hain ya nahi.
Example:
print(5 == 5) # Output: True print("hello" == "world") # Output: False
!=
(Not Equal To):Usage: Check karta hai ki do values different hain ya nahi.
Example:
print(5 != 3) # Output: True print("hello" != "hello") # Output: False
Logical Operators
Logical operators multiple conditions ko combine karte hain aur result True
ya False
hota hai. Yeh operators mainly boolean values ke saath use hote hain, lekin non-boolean types ko bhi evaluate kiya ja sakta hai.
and
(Logical AND):Usage: Dono conditions true hone par
True
return karta hai.Example:
print(True and False) # Output: False print(5 > 3 and 8 < 10) # Output: True print("apple" > "banana" and 10 > 5) # Output: False
or
(Logical OR):Usage: Kisi bhi ek condition ke true hone par
True
return karta hai.Example:
print(True or False) # Output: True print(5 > 10 or 8 < 10) # Output: True print("apple" < "banana" or 10 < 5) # Output: True
not
(Logical NOT):Usage: Condition ko invert karta hai. Agar condition
True
hai, tohFalse
return karega aur vice versa.Example:
print(not True) # Output: False print(not (5 > 10)) # Output: True
Boolean vs Non-Boolean Types
Boolean Types: True/False values ko directly use karke logical operators ka use kar sakte hain.
Example:
print(True and False) # Output: False
Non-Boolean Types: In types ko logical context mein evaluate kiya jata hai. Python mein, non-zero numbers, non-empty strings, aur non-empty collections ko
True
mana jata hai, aur zero, empty strings, aur empty collections koFalse
mana jata hai.Example:
print(5 and 0) # Output: 0 (0 is falsy, so result is 0) print("hello" or "") # Output: "hello" (non-empty string is truthy) print(not "") # Output: True (empty string is falsy)
Summary
Python mein relational, equality, aur logical operators ko samajhna aur unka sahi use karna important hai. Relational aur equality operators ko comparisons aur checks ke liye use kiya jata hai, jabki logical operators ko conditions ko combine aur invert karne ke liye use kiya jata hai. Non-boolean types bhi logical operators ke saath evaluate ho sakte hain aur yeh operators aapke code ko zyada flexible aur powerful banate hain.