← Back to Python Course | Chapter 1: Basics | Lesson 12 of 14

Python Booleans

एक boolean एक लाइट स्विच जैसा है जो सिर्फ on या off हो सकता है। Python में यह या तो True होता है या False, और प्रोग्राम इसका उपयोग निर्णय लेने के लिए करते हैं।
Syntax
python
variable = True  # or False
bool(value)
result = expression1 and expression2

Boolean क्या है

bool type के ठीक दो values हैं, True और False, और Python भीतर से इन्हें integers का विशेष रूप मानती है — संख्यात्मक संदर्भों में True बराबर 1 और False बराबर 0 है।

Booleans वही हैं जो हर comparison operator (जैसे == या >) असल में लौटाता है, इसीलिए वे conditions में लगातार दिखते हैं।

उदाहरण: What Is a Boolean

python
print(True == 1)
print(False == 0)

Comparison के नतीजे

हर relational operator — ==, !=, <, >, <=, >= — एक boolean में evaluate होता है, इसीलिए आप comparison का नतीजा variable में रखकर बाद में तुलना दोहराए बिना दोबारा इस्तेमाल कर सकते हैं।

हर if statement भीतर से इसी तंत्र पर निर्भर करता है।

उदाहरण: Comparison Results

python
is_adult = 20 >= 18
print(is_adult)

Truthy और Falsy Values

Python में केवल गिने-चुने values falsy माने जाते हैं: 0, 0.0, None, और '', [] या {} जैसा कोई भी खाली collection।

बाकी हर value — ऋणात्मक संख्याओं और एक-space वाली strings समेत — truthy है, इसीलिए 'if my_list:' यह जाँचने का सामान्य तरीका है कि list में कोई element है या नहीं।

उदाहरण: Truthy and Falsy Values

python
print(bool(0), bool(""), bool([]), bool(None))
print(bool(-1), bool(" "))

bool() Function

किसी भी value पर सीधे bool() बुलाने से Python के truthiness नियम लागू होते हैं और स्पष्ट True या False लौटता है, जो debugging में या किसी condition की truthiness को असली boolean के रूप में रखने में उपयोगी है, बजाय इसके कि if statement उसे परोक्ष रूप से समझे।

उदाहरण: The bool() Function

python
value = "some text"
print(bool(value))

Boolean Logic Operators

and, or और not keywords booleans को जोड़ते या उलटते हैं — and पहला falsy operand लौटाता है, या सब truthy हों तो आख़िरी value, जबकि or पहला truthy operand लौटाता है, और यह short-circuiting व्यवहार मतलब है कि x and x.value जैसी expression तब भी सुरक्षित है जब x None हो सकता हो।

उदाहरण: Boolean Logic Operators

python
x = None
print(x and x.upper())  # short-circuits at None, so x.upper() is never called
print(True or False)  # or returns the first truthy operand
print(not False)  # not inverts the boolean
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.