DEV Community

Cover image for Learn Python: Booleans & Comparisons
Rishi
Rishi

Posted on • Updated on

Learn Python: Booleans & Comparisons

Simply Booleans

Checking for truthy or falsy.

Description Operator
Truthy True
Falsy False
Greater than >
Less than <
Greater or equal to >=
Less or equal to <=
Equals to ==
Assignment =
truthy = True
falsy = False
print(truthy);
print(falsy);

LEGAL_AGE = 18
age = int(input("Enter your age:"));
is_over_legal_age = age >= LEGAL_AGE;
print(f"User is OVER legal age: {is_over_legal_age}")

is_under_legal_age = age < LEGAL_AGE;
print(f"User is UNDER legal age: {is_under_legal_age}")

is_twenty = age == 20;
print(f"User is 20 years old: {is_twenty}")

is_eighteen = age == 18;
print(f"User is 18 years old: {is_eighteen}")
Enter fullscreen mode Exit fullscreen mode

bool()

bool() is a function which takes in a parameter and evaluates it to either True or False.

print(f"Integer 0 is evaluated to: {bool(0)}")
print(f"An empty character '' is evaluated to: {bool('')}")
print(f"Positve number 1 is evaluated to : {bool(1)}")
print(f"Negative -1 is evaluated to: {bool(-1)}")
print(f"A string 'abc' is evaluated to : {bool('abc')}")
Enter fullscreen mode Exit fullscreen mode

and

and gives you the first value if it is False, otherwise, it gives you the second value.

print(f"True and True: { True and True}");
print(f"False and False: { False and False}");
print(f"True and False: { True and False}");
print(f"False and True: { False and True}");
Enter fullscreen mode Exit fullscreen mode

Let's compare some numbers and some strings.

print(f"True and True: { bool(10) and bool(10)}");
print(f"False and False: { bool(0) and bool(10)}");
print(f"True and False: { bool(10) and bool(0)}");
print(f"False and True: { bool(0) and bool(10)}");
print()
print(f"10  and  10: { 10 and 10}");
print(f"0  and   0:  { 0 and 0}");
print(f"10  and   0:  { 10 and 0}");
print(f"0  and  10:  { 0 and 10}");
print()
print(f"'abc'  and  'xyz': { 'abc' and 'xyz'}");
print(f"''  and  '': { '' and 'xyz'}");
print(f"'abc'  and  '': { 'abc' and ''}");
print(f"''  and  'xyz': { '' and 'xyz'}");
Enter fullscreen mode Exit fullscreen mode

or

or gives you the first value if it is True, otherwise it gives you the second value.

print(f"True or True: { True or True}");
print(f"False or False: { False or False}");
print(f"True or False: { True or False}");
print(f"False or True: { False or True}");

Enter fullscreen mode Exit fullscreen mode

Let's compare some numbers and some strings.

print(f"True or True: { bool(10) or bool(10)}");
print(f"False or False: { bool(0) or bool(10)}");
print(f"True or False: { bool(10) or bool(0)}");
print(f"False or True: { bool(0) or bool(10)}");
print()
print(f"10  or  10: { 10 or 10}");
print(f"0  or   0:  { 0 or 0}");
print(f"10  or   0:  { 10 or 0}");
print(f"0  or  10:  { 0 or 10}");
print()
print(f"'abc'  or  'xyz': { 'abc' or 'xyz'}");
print(f"''  or  '': { '' or 'xyz'}");
print(f"'abc'  or  '': { 'abc' or ''}");
print(f"''  or  'xyz': { '' or 'xyz'}");
Enter fullscreen mode Exit fullscreen mode

not

not negates any boolean value.
not True is evaluated to False.
not False is evaluated to True.

print(f"True: { True}" );
print(f"not True: {not True}" );
print()
print(f"False: { False}" );
print(f"not False: {not False}" );
print()
print(f"35: { 35}" );
print(f"not 35: {not 35}" );
print()
print(f"'': { ''}" );
print(f"not '': {not ''}" );
print()
print(f"'abc': { 'abc'}" );
print(f"not 'abc': {not 'abc'}" );
Enter fullscreen mode Exit fullscreen mode



Top comments (0)