Day 9: Booleans & Comparison

Using operators to compare values that yields True/Truthy or False/Falsey

print (4 == 3) # False
print (4 != 3) # True
print ("soumil" == "soumil") # True

print ("4" == 4) # False
# print ("10" <= 10) # Error

print (5.6 > 5.4) # True
print (1.23456789 > 1.23456788) # True
print (-1.23456789 < -1.23456788) # True
print (11.000001 > 11.0) # False

print (7.0 == 7) # True
print (10.0 == 10.0) # True
print (11.000001 == 11.0) # False
print (12.3 != 12.3) # False

Truthy or Falsey values

# Truthy & Falsey values
# Falsey: Empty string, 0, 0.0, None, False, (), [], {}
# Truthy: Anything else

print (bool("")) # Falsey
print (bool(0)) # Falsey
print (bool(0.0)) # Falsey
print (bool(None)) # Falsey
print (bool(False)) # Falsey
print (bool(())) # Falsey
print (bool([])) # Falsey
print (bool({})) # Falsey

print (bool(" ")) # True
print (bool(1)) # True
print (bool(1.0)) # True
print (bool(True)) # True

The In Operator

The in operator checks for an element within lists or strings or any iterable types.

# In operator, works with iterable types only, like strings & lists
# Doesnt work with numbers

print ("soumil" in "soumil") # True
print ("soumil" in "Soumil is a good") # False (case-sensitive)

print ("10" in "10") # True
print (45 in [10, 15, 12]) # False
print (45 in [10, 15, 12, 33.22, 45]) # True
print (33.22 in [10, 15, 12, 33.22, 45]) # True

The if/elif/else Keywords

In python if blocks should be indented else it is considered outside the block. Indent rules applies to nested conditions as well.

# if statement block is indentation based
# How are you perceived in society
age = input("How old are you? ");

if not age:
	print("You didn't enter your age!");
	exit();
  
age = int(age);

if age < 18:
	print("You are a minor.");
elif age < 21:
	print("You are a youth.");
elif age < 65:
	print("You are an adult.");
else:
	print("You are senior citizen.");
What’s your Reaction?
+1
0
+1
0
+1
0

Published by Soumil Roy

I'm a Full-Stack Developer with 7+ years of experience in Web design & development .Currently building expertise in React, TypeScript & GraphQL

Leave a comment

Your email address will not be published.

Open WhatApp
Hello 👋 Click the button below connect with me.