At the end of the lesson, you will be able to:
Let’s begin!
Decision-making is as important in any programming language as it is in life. Decision-making in a programming language is automated using conditional statements, in which Python evaluates the code to see if it meets the specified conditions.
The conditions are evaluated and processed as true or false. If this is found to be true, the program is run as needed. If the condition is found to be false, the statement following the If condition is executed.
Python has six conditional statements that are used in decision-making:-
Boolean represents one of the two value: True or False
For Example:
print(10 > 9)
print(10 == 9)
print(10 < 9)
For Example:
print(bool("Hello"))
print(bool(15))
Almost any value is evaluated to True if it has some sort of content. Any string is True, except empty strings. Any number is True, except 0. Any list, tuple, set, and dictionary are True, except empty ones.
a = bool("abc")
b = bool(123)
c = bool(["apple", "cherry", "banana"])
print(a,b,c)
In fact, there are not many values that evaluate to False, except empty values, such as (), [], {}, “”, the number 0, and the value None. And of course the value False evaluates to False.
a=bool(False)
b=bool(None)
c=bool(0)
d=bool("")
e=bool(())
f=bool([])
g=bool({})
print(a,b,c,d,e,f,g)
You can create functions that returns a Boolean Value:
def myFunction() :
return True
print(myFunction())
We can execute code based on the Boolean answer of a function:
Examples:
def myFunction() :
return True
if myFunction():
print("YES!")
else:
print("NO!")
x = 200
print(isinstance(x, int))
When you run a condition in an if statement, Python returns True or False. Python supports the usual logical conditions from mathematics as discussed in the previous session.
These conditions can be used in several ways, most commonly in “if statements” and loops. An “if statement” is written by using the if keyword.
Flowchart:
a = 33
b = 200
if b > a:
print("b is greater than a")
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
Example:
If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a")
# you will get an error
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
a=33
b=22
if a > b:
print("a is greater than b")
a = 2
b = 330
print("A") if a > b else print("B")
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
As a democratic country, India is built on the foundations of election. Our Parliament and Legislatures are of the people, by the people and for the people. Voting is a constitutional right that we are privileged to have. We take it for granted, but the constitution has given us the right to elect who we want, and the right to make the change.
There are certain eligibility conditions which a voter need to satisfy, the most important one is age criteria, if a person’s age is equal to or greater than 18 years then the person is eligible to vote otherwise the person is not eligible. Let’s create a program to check whether you are eligible to vote or not.
It can be better understood in terms of the flowchart below.
The code is pretty simple. Let’s get straight to it. Follow the steps below:
print("What is your age")
age=input()
print("What is your age")
age=input()
age=int(age)
print("What is your age")
age=input()
age=int(age)
if age >= 18:
print("You can vote")
else:
print("You cannot vote")
Click the flag to start the script.