Decision Making statements in Python 3
So today we will learn what are decision making statements what are conditional statements what is if else statement if-elif Nested-elif statements. You will get here all the articles for free.
What are decision making statements in Python 3?
The order of execution of the program is known as the flow of control and decision making statements are used to control the flow of execution, it will depend upon the conditions we apply.
If statement-:
It is the easiest method for making a decision in Python.
It simply states that if something is true then Python should perform the steps that follow.
It simply states that if something is true then Python should perform the steps that follow.
If is a keyword followed by the condition and ended with Colon (:).
For example-:
if condition :
Statement (s)
Header
Statement (s)
Header
- If the condition is true then the indented system gets executed.
- The Indented statement implies that its execution is dependent on the header.
- There is no limit on the number of the statements and statement can appear under an if block.
How we use if statement in a program?
- We can also use () brackets outside the condition but it is not mandatory.
- The statements are written inside the if statement after 4 spaces.
- The statement would be printed if the if statement is true.
- Otherwise it will skip the block of statement.
Let us see how if statement works through flowchart -:
Now look at the example to understand if statement in Python 3.
Click here to learn mathematical operators
Program 1.Write a program to determine whether the person is eligible to vote.
age = int(input("Enter the age of a Person : ")) # stat-1
if age>=18 : # stat-2
print("You are eligible to vote")
print("I am not in if ")
if age>=18 : # stat-2
print("You are eligible to vote")
print("I am not in if ")
Output of the program-:
If input is = 10
I am not in if
if input is = 20
You are eligible to vote
I am not in if
- Statement #1 ask the user to input the age of a person
- Statement #2 is the test expression where it check whether the ages greater than equal to 18 or not.
- If Statement #2 is correct the it will be executed and it will print that you are eligible to vote.
- If statement #2 is incorrect then it will print I am not in if.
Program 2. Write a program to display the grade of a topper in the class.
grade = input("Enter the grade of Topper : ")if grade == 'A' :
print("Well Done")
Output of the program-:
If input is = A
Well Done
- Hair also statement #1 is the input statement.
- Statement #2 is the text expression and if grade = A.
- Then it will print well done.
Click here to learn mathematical operators
Learning Tip :
Statement or statements indented inside if statement are executed only if, if condition evaluates to true otherwise the control skips the block of statement inside if and control goes to the outside of the if block.
Some Questions for you based on the if-statement.
1. Write a Program to print fail in number of a student is less than 33 out of 100.
2. Write a program to print your name if your first letter is input ? for eg. [ my name is Piyush so it will print Piyush of i enter P ].
0 Comments