Easy notes for AI Class 9 Python if else statements

In this article – Easy notes for AI Class 9 Python if else statements you will learn about the python flow of control and conditions. So lets we begin!

Easy notes for AI Class 9 Python if else statements

Now you are familiar with python statements and how its executed! In python, the statements are executed in the sequence from top to bottom. Sometimes we need to follow some conditions and rules in programs. So whenever you have some conditions, python flow of control and conditions help you to solve your problem. So in this article Easy notes for AI Class 9 Python if else statements we will focus on them.

What is the flow of control?

The flow of control means executing the statement according to the conditions. Suppose, you have two choices for going out according to the weather conditions. One if weather conditions are good you can go for play otherwise stay at home. So here your flow will be something like this if weather is good then Go for play, otherwise stay at home.

The above said example is an example of flow of control. Python offers three types of flow of control and conditions.

  1. If statement
  2. For statement
  3. While statement
Python flow of control and conditions - AI Class 9 Python if else
Python flow of control and conditions – AI Class 9 Python if else

In the next section of Easy notes for AI Class 9 Python if else statements, we are going to cover the if else statement in detail. So lets we start now!

Python if else statements

In real life many times we are facing such situations where we have to make some decisions based on certain conditions what we need to do to overcome those situations or to get success! Similarly in programming also this type of conditions arise where a programmer has to take the decision to solve a particular problem.

The flow of control statements are also known as decision making statements in programing. As you are familiar now, if else statement is also one of the part of them. Python offers following types of if else statemtns:

  1. Simple if
  2. If-else
  3. If-elif-else ladder

Let’s we start the Easy notes for AI Class 9 Python if else statements with simple if!

Simple if Statement

If you have only one condition to be execute or one possibility of output, simple if statement is useful for the same. Simple it executes the True condition block. Suppose, If gate is open you are allowed to go inside! Let’s see the syntax:

if <condition>:
  statement(s)
gate_status = "open"

if gate_status=="open":
   print("Entry is permitted")

In the above example, we have taken one variable to store the status of gate as “open”. In next line, the condition is checking the value of gate_status with “open”. If both are equals then the message will be displayed on the screen.

Let’s have a look at another example.

age = 14
if age>12 and age<20:
  print("You are teenager")
print("Be yourself!!!")

age=12
if age>12 and age<20:
  print("You are teenager")
print("Either your are child or adult")

Understand the above code by yourself and Comment in the comment section with Output for example 1 is ____________.

In python, the indentation plays an important role. After a block of code python automatically start a new line with indentation. Observe the statement written after the colon (:). So while writing the python code in your notebook, be careful!

Python if-else statement

In the next section of Easy notes for AI Class 9 Python if else statements, you will learn about python if-else.

Python If-else statement

Python if-else statement is useful when you have two choices or possibilities. “If this then this, otherwise that” like condition is considered as python if-else statement.

For example, if a number is odd, otherwise even. If the gate status is open Entry is permitted, otherwise go back to home! Look at the syntax of python if-else statement:

if <condition>:
  statement(s)
else:
  statement(s)

Observe the following code:

gate_status = "open"

if gate_status=="open":
   print("Entry is permitted")
else:
   print("You have to go back to your home, Bye - Bye")

Here, if the status is open then it will execute the if block, otherwise else block. So here we have the gate_status variable and the value is “open” so, if block will execute. Try to understand following code:

age = 10
if age>12 and age<20:
  print("You are teenager")
else:

  print("Either your are child or adult")

What will be the output of the above code? Comment in the comment section with Output for example 2 is ____________.

Now in the next section of Easy notes for AI Class 9 Python if else statements, you will learn about if-elif-else ladder.

Python if-elif-else ladder

Python if-elif-else ladder is used in the case when you have more than two choices or possibilities. The elif is a short form of else if. The ladder means it will continue upto n possiblities. Like if this then this, or if this then this,…., otherwise this. The syntax for python ef-elif-else ladder is as following:

if <condition1>:
  statement(s)
elif <condition2>:
  statement(s)
elif <condition3>:
  statement(s)
....
....
....
else:
 statement(s)

Now we will see the example of if-elif-else ladder for Easy notes for AI Class 9 Python if else statements. Observe this code:

This program is all about to check whether the given number is of one-digit number or two-digit number or three-digit number or more than three-digit number.

n = int(input("Enter no. to check:"))
if n>0 and n<10:
  print("The no. is one digit number")
elif n>0 and n<10:
  print("The no. is two digit number")
elif n>0 and n<10:
  print("The no. is three digit number")
else:
  print("The no. is more than 3 digit number")

In the above program for Easy notes for AI Class 9 Python if else statements we have 4 possibilities such as the number is 1 digit number or 2 digit number or 3 digit number or more than 3 digit number. So started with if…then elif…. at the last we have used else block. So it will check the statements as per the number values entered as input.

Nested if statement

The nested if statement consists of if within if statement. One if statement contains another if statement inside is called nested if. Look at the syntax:

if <condition>:
   if <condition>:
       statement(s)
...
...

In nested if the in one condition another condition will be checked.

Follow the below given link for the programs based on if-else statement:

Python Program list on if-else

Leave a Reply