Friday, 21 March 2025

Python Conditional and Looping Statements Class 9 AI Notes

These AI Class 9 Notes Chapter 8 Python Conditional and Looping Statements Class 9 AI Notes simplify complex AI concepts for easy understanding.

Class 9 AI Python Conditional and Looping Statements Notes

Introduction Class 9 Notes

  • The two fundamental building blocks of Python programming: conditional statements and looping statements. These powerful tools allow your programs to make decisions and repeat actions.
  • Conditional statements in Python language decide the direction (Control Flow) of the flow of program execution. They evaluate a condition (often true or false) and execute different code blocks based on the outcome.
  • Looping statements are all about repetition. They allow you to execute a block of code multiple times, often iterating through a sequence of data like lists or strings.

Control Structure Class 9 Notes

A control structure is a programming language construct which affects the flow of the execution of program.
Various types of control structure are described below

Python Conditional and Looping Statements Class 9 AI Notes 1

Sequence Statements

Sequence statements refers to the instructions that are executed in the sequence in which they are written in the program. When a program is run, the CPU begins execution, executes some number of statements and then terminates at the end.

Python Conditional and Looping Statements Class 9 AI Notes

The sequence of statements that the CPU executes is called the program’s path. Many programs have been straight line programs. Straight line programs have sequential flow, i.e. they take the same path every time they are run.

Python Conditional and Looping Statements Class 9 AI Notes 2

For example,

Python Conditional and Looping Statements Class 9 AI Notes 3

Output
A program which adds two integers
Enter first integer : 5
Enter second integer: 11
Sum is: 16

Selection Statements

These statements allow a program to execute a statement or a set of statements depending upon a given condition.
These statements are also called conditional statements or decision statements. A selection statement is a control statement that allows choosing statement two or more execution paths in a program. Selection statements alter the sequential flow of instructions in a program. This is done using selection statements.

Python Conditional and Looping Statements Class 9 AI Notes 4

Python provides three types of selectors are described below

1. if statement

The simplest and most common selection structure is the if statement. The if statement allows branch depending upon the value or state of variables. If the condition evaluates true, an action is followed otherwise, the action is ignored.
Syntax

Python Conditional and Looping Statements Class 9 AI Notes 5

In the above example, the conditional statement under if( ) will be always evaluated as True because the value of x will become 4 before the comparison thus the expression (4>3) yields a True value letting the statement under if () to execute, i.e the output of the above code would be “x is greater”.

Python Conditional and Looping Statements Class 9 AI Notes

Following flowchart illustrated if statement:

Python Conditional and Looping Statements Class 9 AI Notes 6

2. if-else statement

This statement also tests a conditional expression. If the condition is True, the entire block of statements following the if will be executed.
Otherwise, the entire block of statements following the else will be executed.
Syntax

Python Conditional and Looping Statements Class 9 AI Notes 7

Following flowchart illustrates if-else statement:

Python Conditional and Looping Statements Class 9 AI Notes 8

e.g.
x=0
y=3
x=x+y
if (x>y):
print(“x is greater”)
else:
print(“we are in else part because x is not greater”)

If the conditional statement under if( ) is evaluated to be True, then the statements under if( ) block will be executed otherwise the statements under else block will be executed. In an if-else statement, either if block or else block executes, never both.
The above code, produces an output as “we are in else part because x is not greater”. Because the conditional statement under if( ) evaluates as False $x$ is not greater than y, it is same as that of y.
e.g.
if (a==b) :
c=a

In above case, the if will stop and the statements following this line will not be considered a part of if, these statements will be executed irrespective of whether the condition a==b is False or True.

Python Conditional and Looping Statements Class 9 AI Notes

3. if-elif-else statement

Sometimes, we need to execute different statement to perform in different conditions. It requires the evaluation of several conditions.

First of all, the first condition is evaluated, if it is true then respective statements are processed. Otherwise, the next condition is evaluated to process another say of statements. This process is continued for several times.

Python Conditional and Looping Statements Class 9 AI Notes 10

Expression is tested from the top towards the downside. As soon as the true condition is found, the associated set of statement is executed.
Following flowchart illustrates if-elif-else statement

Python Conditional and Looping Statements Class 9 AI Notes 11

e.g.

Python Conditional and Looping Statements Class 9 AI Notes 12

4. Nested if statement

Nested means if statements or if-else statements are placed in the statement block of if statement or else statement.

Syntax

Python Conditional and Looping Statements Class 9 AI Notes 13

The nested if can have one of the following four forms:
(i) if nested inside both, if part and else part

Python Conditional and Looping Statements Class 9 AI Notes 14

(ii) if nested inside if part

Python Conditional and Looping Statements Class 9 AI Notes 15

(iii) if nested inside else part

Python Conditional and Looping Statements Class 9 AI Notes 16

(iv) One if inside another if statements

Python Conditional and Looping Statements Class 9 AI Notes 17

range( ) Function Class 9 Notes

The range( ) function is used to generate a sequence of numbers overtime. At its simplest, it accepts an integer and returns a range object (a type of iterable).
The range ( ) function has two sets of parameters, as follows

range (stop)

stop number of integers to generate, starting from zero.

range ([start,] stop [,step])

  • start starting number of the sequence.
  • stop generate numbers upto, but not including this number.
  • step difference between each number in the sequence.

Python Conditional and Looping Statements Class 9 AI Notes

Iterative Statements

Iterative statements or loops enable a program with a cyclic flow of logic. Each statement which is written under the scope of a looping statement gets executed the number of times the iteration/looping continues for.
There are two basic types of looping statements available in Python, they are as follows.
A looping statement enables repetition of tasks under its scope based on a particular condition called as loop-condition.
This loop condition evaluates to either True or False. A loop may be continued till a loop-condition is evaluated as True.

Python Conditional and Looping Statements Class 9 AI Notes 18

1. while loop

A while loop tests for its ending condition before executing the statements enclosed in the loop body even the first time. So, if the ending condition is met when the while loop beings, the lines of instructions its contains will never be carried out.

Syntax

while(loop-condition):
statement(s)

Following flowchart illustrates while loop

Python Conditional and Looping Statements Class 9 AI Notes 19

A while continues iteration cycle till its loop condition is evaluated as true. If the loop-condition is false for the first time iteration, then loop will not execute even once.

e.g.

Python Conditional and Looping Statements Class 9 AI Notes 20

The loop is executed till the x is greater than 0 , as soon x becomes 0 the loop is terminated. We observe that within the scope of the loop the value of $x$ is decremented, so that it approaches to its next previous value. Thus, with each iteration the value of $x$ is added to a variable sum and is decremented by 1.

Let us assume that user inputs a value 4 for $x$ then at

Python Conditional and Looping Statements Class 9 AI Notes 21

In while loop, the looping condition is evaluated at the beginning of the loop’s scope, i.e. prior to entering into the scope of loop. This type of checking is called “Entry control checking”, if the condition fails this checking, then the entry into the scope of loop will not be allowed. So, while loops are often called as Entry Control Loop.
The while loop can accept expression and not just conditions, the following are all illegal expressions
(i) while (x =x+1)
(ii) while (n–)
(iii) while(count +=i)

When the result of x=x+1, n– or count +=i, evaluates to 0 , then the while condition fails and the loop will exit.

Python Conditional and Looping Statements Class 9 AI Notes

2. for loop

The for statement encloses one or more statements that form the body of the loop, the statements in the loop repeat continuously a certain number of times.
This loop is also an entry control loop, as condition is checked before entering into the scope of the loop.

Syntax

Python Conditional and Looping Statements Class 9 AI Notes 22

Output
A
B
A
B
A
B
A
B

Difference between ‘for’ loop and ‘while’ loop

Feature ‘for’ Loop ‘while’ Loop
Syntax for (initialization; condition; increment/decrement) while (condition)
Initialization Initialization is done within the loop structure Initialization is done before the loop structure
Condition The loop continues iterating until the condition is false The loop continues iterating until the condition is false
Increment/Decrement Increment or decrement is done within the loop structure Increment or decrement must be done explicitly within the loop
Use Cases Suitable for iterating over a known range or sequence Suitable for indefinite iteration or when the number of iterations is unknown
Control More control over the number of iterations Less control over the number of iterations, but can handle more complex conditions
Examples python for i in range(5): python while x<5:

Programs Class 9 Notes

1. For accepting an integer from user and checking if this integer is greater than 10.

Python Conditional and Looping Statements Class 9 AI Notes 23

Output
Enter a number: 56
Number is 56
56 is greater than 10

2. For adding a number to non-zero real number x.

Python Conditional and Looping Statements Class 9 AI Notes 24

Output
Please enter a real number: 45.9
Real number is 45.9
The sum is: 145.9

Python Conditional and Looping Statements Class 9 AI Notes

3. For asking a user to answer Yes or No to a question such as “Are you ready to provide your credit card number”?

Python Conditional and Looping Statements Class 9 AI Notes 25

Output
Are you ready to provide your credit card number
(1 = Yes 0 = No): 0
Your choice: 0
Now we will not continue the processing of transaction

4. To find the number is even or odd with compound statement.

Python Conditional and Looping Statements Class 9 AI Notes 26

Output
Enter a number:45
The number is 45
Remainder on division by 2 is: 1
The number is an odd number.

Python Conditional and Looping Statements Class 9 AI Notes

5. To accept a positive value from a user and prints the square of the number.

Python Conditional and Looping Statements Class 9 AI Notes 27

Output
Enter a number : 6
The square of 6 is 36

6. To enter a character and print whether a given character is an alphabet, digit or any character.

Python Conditional and Looping Statements Class 9 AI Notes 28

Output
Enter a character : a
You entered an alphabet
>>>
Enter a character:2
You entered a digit

7. To print the largest number out of 3 numbers.

Python Conditional and Looping Statements Class 9 AI Notes 29

Output
Enter first number: 78
Enter second number: 54
Enter third number : 23
i = 78
j = 54
k = 23
The largest number is 78

Python Conditional and Looping Statements Class 9 AI Notes

8. For fibonacci series using while loop.

Python Conditional and Looping Statements Class 9 AI Notes 30

Output
Enter the limit: 10
0
1
1
2
3
5
8
13
21

Glossary

  • Sequence Statements it refers to the instructions that are executed in the sequence in which they are written in the program.
  • Selection Statements These statements allow a program to execute a statement or a set of statements depending upon a given condition.
  • range( ) Function It is used to generate a sequence of numbers overtime.

The post Python Conditional and Looping Statements Class 9 AI Notes appeared first on Learn CBSE.



from Learn CBSE https://ift.tt/fGz091F
via IFTTT

No comments:

Post a Comment