Boolean values and operators TRB CSI › Python Programming › Boolean values and operators,

Boolean values and operators

Boolean is a built-in data type in Python that can have only two values: True and False. தமிழில்: ஒரு condition உண்மையா அல்லது பொய்யா என்பதை காட்டும் Python data type தான் Boolean.

Free Intermediate 16 min 187 cards 36 programs 10 MCQs v3

Boolean Values and Operators - Python
Tamil + English Mixed | Inch by Inch | TRB / NET / SET / Interview Style

Python-ல் ஒரு condition True அல்லது False என்பதை represent செய்ய பயன்படுத்தப்படும் values தான் Boolean values.

Boolean என்பது programming-ல்:

Yes / No
Correct / Wrong
True / False

போன்ற இரண்டு நிலைகளை represent செய்கிறது.

Definition
Meaning

Boolean is a built-in data type in Python that can have only two values: True and False.

ஒரு condition உண்மையா அல்லது பொய்யா என்பதை காட்டும் Python data type தான் Boolean.

Explanation
Boolean values
Python-ல் இரண்டு Boolean values மட்டுமே உள்ளன

True
False

Important
முக்கியம்
  1. True

  2. False

  3. இவற்றின் முதல் letter Capital letter ஆக இருக்க வேண்டும்.

  4. Correct

    True

  5. False

  6. Wrong

    true

  7. false

Simple Explanation
Core idea

10 என்பது 5-ஐ விட பெரியது.

Suppose

10 > 5

அதனால் result

True

Program
Example
Python 1 lines
1print(10 > 5)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
Program
ஆனால்
Python 1 lines
1print(10 < 5)
Verified output
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
False
Explanation
Boolean data type
Python Boolean data type

bool

Program
Example
Python 1 lines
1x = True
Program
Python 1 lines
1print(type(x))
Verified output
<class 'bool'>
Learning Run Mode — this is the output the author verified, not a live execution.
Output
<class 'bool'>
Flow

Boolean condition எப்படி work ஆகிறது?

  1. Expression
  2. Condition Check
  3. True / False
Explanation
Example
Core idea
  1. 10 > 5
  2. Is 10 greater than 5?
  3. Yes
  4. True
Detail 01

Boolean variable:

Syntax
Python 1 lines
1variable = True
Explanation
Core idea

or

Syntax
Python 1 lines
1variable = False
Program
Example
Python 1 lines
1is_student = True
Program
Python 2 lines
1a = True
2b = False
Program
Python 2 lines
1print(a)
2print(b)
Verified output
True
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
False
Explanation
Core idea

a = True

Detail 01

a என்ற variable-க்கு Boolean value True assign செய்கிறோம்.

Detail 02

b = False

Detail 03

b variable-க்கு False assign செய்கிறோம்.

Explanation
Boolean expression
Core idea

ஒரு expression result True அல்லது False ஆக வந்தால் அது Boolean expression.

Detail 01

5 > 2

Output
Result
True
Explanation
Example
Core idea

10 == 20

Output
Result
False
Explanation
Boolean operators
Core idea

Boolean conditions உருவாக்குவதற்கு முக்கியமாக இரண்டு வகை operators புரிந்துகொள்ள வேண்டும்:

Detail 01

Comparison Operators
Logical Operators

Explanation
Core idea

1. COMPARISON OPERATORS

Explanation
Core idea

இரண்டு values-ஐ compare செய்து True அல்லது False result தரும் operators தான் Comparison Operators.

Table
OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
Explanation
1. == equal to
Core idea

== இரண்டு values equal-ஆ இருக்கிறதா என்று check செய்கிறது.

Program
Example
Python 1 lines
1print(10 == 10)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
Program
Example
Python 1 lines
1print(10 == 20)
Verified output
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
False
Important
  1. = மற்றும் == இரண்டும் different.

  2. x = 10

  3. = → Assignment operator.

  4. x == 10

  5. == → Comparison operator.

Memory Trick
=
Store
==
Compare
Explanation
2. != not equal to
Core idea

!= இரண்டு values unequal-ஆ இருக்கிறதா என்று check செய்கிறது.

Program
Example
Python 1 lines
1print(10 != 20)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True

Because:

10 and 20 are different

Program
Example
Python 1 lines
1print(10 != 10)
Verified output
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
False
Explanation
3. > greater than
Core idea

Left-side value right-side value-ஐ விட பெரியதா?

Detail 01

print(20 > 10)

Output
True
Explanation
4. < less than
Core idea

print(5 < 10)

Output
True
Explanation
5. >= greater than or equal to
Core idea

இதில் இரண்டு possibilities.

Detail 01
  • Greater than
  • OR
  • Equal to
Program
Example
Python 1 lines
1print(10 >= 5)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
Program
Example
Python 1 lines
1print(10 >= 10)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True

இங்கே 10 == 10 என்பதால் கூட True.

Program
Example
Python 1 lines
1print(5 <= 10)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
Program
Example
Python 1 lines
1print(10 <= 10)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
Program
Comparison program
Python 2 lines
1a = 10
2b = 20
Program
Python 6 lines
1print(a == b)
2print(a != b)
3print(a > b)
4print(a < b)
5print(a >= b)
6print(a <= b)
Verified output
False
True
False
True
False
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
False
True
False
True
False
True
Dry Run
Object walkthroughFollow creation, available members and the resulting object state1 stage
  1. Given
    a = 10
    b = 20

    a == b
    10 == 20

    False

    a != b
    10 != 20

    True

    a > b
    10 > 20

    False

    a < b
    10 < 20

    True

1. LOGICAL OPERATORS

Explanation
Core idea
  • Python-ல் three major logical operators உள்ளன:
  • and
  • or
  • not
Table
Logical operators table
OperatorMeaning
andBoth conditions must be True
orAny one condition can be True
notReverses Boolean value
Explanation
1. and operator
Core idea

and operator-ல் இரண்டு conditions-மும் True ஆக இருந்தால் மட்டுமே result True.

Detail 01

Truth Table

Table
ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
Memory Trick

AND = ALL True

எல்லாமே True இருந்தால் மட்டுமே True.

Program
Python 1 lines
1a = 10
Program
Python 1 lines
1print(a > 5 and a < 20)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Check

10 > 5 → True

10 < 20 → True

So

True and True

Output
Result
True
Real Life Example
And example 2
Context
a = 10

print(a > 5 and a > 50)
  • 10 > 5 → True
  • 10 > 50 → False

True and False

Output
Result
False
Real Life Example
Real time example
  • Theory >= 40
  • AND
  • Practical >= 40
  • இரண்டிலும் pass ஆக வேண்டும்.
  • theory = 60
  • practical = 50
  • print(theory >= 40 and practical >= 40)
Output
True
Explanation
2. or operator
Core idea

or operator-ல் minimum ஒரு condition True இருந்தாலும் result True.

Detail 01

Truth Table

Table
ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
Memory Trick

OR = ONE True is enough

ஒரு condition True இருந்தாலே result True.

Program
Python 1 lines
1a = 10
Program
Python 1 lines
1print(a > 5 or a > 100)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Check

10 > 5 → True

10 > 100 → False

Therefore

True or False

Output
True
Real Life Example
Real time example
  • Email verified
  • OR
  • Mobile verified
  • ஒரு verification இருந்தாலும் access allow செய்வதாக வைத்துக்கொள்வோம்.
  • email_verified = False
  • mobile_verified = True
  • print(email_verified or mobile_verified)
Output
True
Explanation
3. not operator
Core idea

not existing Boolean value-ஐ reverse செய்கிறது.

Detail 01
True
False
False
True
Table
Truth table
Anot A
TrueFalse
FalseTrue
Program
Python 1 lines
1x = True
Program
Python 1 lines
1print(not x)
Verified output
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
False
Real Life Example
Another example
Context

print(not 10 > 5)

True

not True

Output
Result
False
Explanation
Boolean and if
Core idea

Boolean values மிகவும் அதிகமாக if condition-ல் பயன்படுத்தப்படுகிறது.

Program
Example
Python 1 lines
1age = 20
Program
Python 2 lines
1if age >= 18:
2    print("Eligible")
Explanation
Core idea

Boolean expression.

Here

age >= 18

Detail 02

For:

Program
Python 1 lines
1age = 20
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Condition

20 >= 18

Output
Result
True

அதனால் if block execute ஆகிறது.

Explanation
Bool function
Core idea

Python-ல் bool() function பயன்படுத்தி ஒரு value-ஐ Boolean-ஆ evaluate செய்யலாம்.

Detail 01

bool(value)

Program
Python 2 lines
1print(bool(10))
2print(bool(0))
Verified output
True
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
False
Explanation
Truthy and falsy
Core idea

Python-ல் True மற்றும் False மட்டும் இல்லாமல் other values-உம் Boolean context-ல் True/False போல behave செய்யும்.

இதை

Truthy
Falsy

Detail 02

என்று சொல்வார்கள்.

Explanation
Falsy values
Common False-like values

False
0
0.0
None
""
[]
()
{}
set()

Program
Example
Python 3 lines
1print(bool(0))
2print(bool(""))
3print(bool([]))
Verified output
False
False
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
False
False
False
Explanation
Truthy values
Core idea

Most non-empty / non-zero values are Truthy.

Program
Example
Python 4 lines
1print(bool(100))
2print(bool(-5))
3print(bool("Python"))
4print(bool([1, 2]))
Verified output
True
True
True
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
True
True
True
Important

கவனிக்கவும்:

bool("False")

Output
True

ஏன்?

Because "False" என்பது Boolean False கிடையாது.

அது ஒரு non-empty string.

Therefore:

True

Common Mistake

bool("0")

Result False என்று நினைக்கக்கூடாது.

True

Because "0" என்பது non-empty string.

bool(0)

Output
False

But:

bool("0")

Output
True
Explanation
Boolean with numbers
Python-ல் internally
True = 1
False = 0
Program
Example
Python 1 lines
1print(True + True)
Verified output
2
Learning Run Mode — this is the output the author verified, not a live execution.
Output
2

Because:

1 + 1 = 2

Program
Example
Python 1 lines
1print(True + False)
Verified output
1
Learning Run Mode — this is the output the author verified, not a live execution.
Output
1
TRB Point
Important exam point

Python-ல்:

True == 1

Output
Result
True

And:

False == 0

Output
Result
True
Program
Python 2 lines
1print(True == 1)
2print(False == 0)
Verified output
True
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
True
Explanation
Boolean with strings
Core idea

Strings-ஐ கூட compare செய்யலாம்.

Detail 01

print("Python" == "Python")

Output
True
Program
But
Python 1 lines
1print("Python" == "python")
Verified output
False
Learning Run Mode — this is the output the author verified, not a live execution.
Output
False

Because Python is case-sensitive.

Comparison
Chained comparison
Core idea

Python supports chained comparisons.

Program
Example
Python 1 lines
1x = 10
Program
Python 1 lines
1print(5 < x < 20)
Program
Equivalent
Python 1 lines
1print(5 < x and x < 20)
Verified output
True
Learning Run Mode — this is the output the author verified, not a live execution.
Output
True
Explanation
Operator precedence
Core idea

Boolean operations-ல் precedence முக்கியம்.

General order
  1. Comparison
  2. not
  3. and
  4. or
Among logical operators

not > and > or

Memory Trick

NOT first, AND next, OR last

Real Life Example
Example

print(True or False and False)

First:

False and False

Output
Result
False

Then:

True or False

Output
Result
True
Explanation
Parenthesis
Core idea

Confusion avoid செய்ய parentheses use செய்வது best.

Result

= (a > 5) and (b < 20)

Comparison
and vs or
andor
All conditions TrueAt least one True
StrictFlexible
False one வந்தாலும் FalseTrue one வந்தாலும் True
Comparison
= vs ==
===
AssignmentComparison
Value store செய்கிறதுValues compare செய்கிறது
x = 10x == 10
Common Mistake
Error 1

true

True

Common Mistake
Error 2

Wrong:

if x = 10:

Correct:

if x == 10:

Common Mistake
Error 3

True AND False

True and False

  • and
  • or
  • not
Common Mistake
Error 4

10 > 5 & 10 < 20

Boolean logic-க்கு normally:

10 > 5 and 10 < 20

Use செய்ய வேண்டும்.

Memory Trick
Context
  • Boolean
  • Boolean → True / False
  • AND

All conditions True.

OR

One True enough.

NOT
NOT → Opposite

TRB Point

1. Boolean type name in Python?

TRB Point

bool

TRB Point

1. Boolean values?

TRB Point
  1. True

  2. False

TRB Point

1. Logical operators?

TRB Point
  1. and

  2. or

  3. not

TRB Point

1. Equality operator?

TRB Point

==

TRB Point

1. Not equal operator?

TRB Point

!=

TRB Point

1. True and False?

TRB Point

False

TRB Point

1. True or False?

TRB Point

True

TRB Point

1. not True?

TRB Point

False

TRB Point

1. bool(0)?

TRB Point

False

TRB Point

1. bool("Python")?

TRB Point

True

Interview Point
Context

Question: What is Boolean in Python?

True
False
Question: What is the difference between and and or?

and returns True only when all required conditions are True.

or returns True when at least one condition is True.

Question: Is Boolean a subclass of integer in Python?

Yes.

True == 1

  • True
  • and
  • False == 0

True

MCQ
175Which are valid Boolean values in Python?
Choose one answer
MCQ
176What is the output? print(5 > 3)
Choose one answer
MCQ
177What is the output? print(True and False)
Choose one answer
MCQ
178What is the output? print(True or False)
Choose one answer
MCQ
179What is the output? print(not False)
Choose one answer
MCQ
180Which operator checks equality?
Choose one answer
MCQ
181What is the output? print(bool(0))
Choose one answer
MCQ
182What is the output? print(bool("Hello"))
Choose one answer
MCQ
183What is the output? print(True + True)
Choose one answer
MCQ
184Which has highest priority among these?
Choose one answer
Practice

Predict the outputs:

1.
print(10 >= 10)

Answer:

True
2.
print(20 != 20)

Answer:

False
3.
print(False or True)

Answer:

True
4.
print(False and True)

Answer:

False
5.
print(not (5 > 2))

Answer:

False
6.
print(bool([]))

Answer:

False
7.
print(bool([0]))

Answer:

True

Important: [0] list empty இல்லை. அதனால் True.

Summary
  1. Boolean
  2. ├── True
  3. └── False
  4. Comparison Operators
  5. == Equalal
  6. != Not Equalal
  7. Greater Than
  8. < Less Thanhan
  9. = Greater Than or Equalal
  10. <= Less Than or Equalal
  11. Logical Operators
  12. and → All True required
  13. or → One True enoughh
  14. not → Reverse
  15. Truth table shortcut
  16. True and True → Trueue
  17. True and False → Falsee
  18. True or False → Trueue
  19. False or False → Falsee
  20. not True → False→ False
  21. not False → True→ True
Explanation
Short description
Core idea

Boolean என்பது Python-ல் True மற்றும் False என்ற இரண்டு logical values-ஐ represent செய்யும் built-in data type. Comparison operators conditions உருவாக்கும்; and, or, not போன்ற logical operators multiple Boolean conditions-ஐ combine அல்லது reverse செய்ய பயன்படும்.

Text size
17px
Theme
Contents
Print / PDF
Program