Class x-A.I-Python-notes-loops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CLASS X

ARTIFICIAL INTELLIGENCE
Review Concepts - Python
(LOOPS)
Iteration Constructs (loops)
Certain set of statements are executed again and again based upon conditional test. i.e
executed more than one time. Loops are used to repeatedly execute the same code in a
program.

They execute statement/statements as long as the specified condition is evaluates to


Boolean true(which controls the execution of the statements) and execution of
statements stop when the condition evaluates to boolean false.
The flowchart for a loop can be represented as follows:
Python provides two types of looping constructs:

1) for statement
2) while statement
3) while True
for loop
“for” loop
The second type of iteration is for loop which is used for repeated execution over the elements
of a sequence. For loop is a deterministic loop as the number times the loop is to execute
is known in advance.
In its simplest form, the syntax of for loop is:
for variablename in range(start, stop, step):

statements
else:
statements

Here:
(i) variable is any variable name defined by the user.

ii) Python range() function syntax and arguments

Syntax:

 range(start, stop[, step])

 range() takes three arguments.


 Out of the three, 2 arguments are optional. I.e., Start and Step are the
optional arguments.

11
1. A start argument is a starting number of the sequence. i.e., lower limit. By default,
itstarts with 0 if not specified.
2. A stop argument is an upper limit. i.e. generate numbers up to this number,
The range() function doesn’t include this number in the result.
3. The step is a difference between each number in the result. The default value of
thestep is 1 if not specified.

Let see all the possible scenarios now of range()


range(10) produces a list of integers 0, 1, 2, 3,4, 5, 6, 7, 8, 9.
With two parameters: range(start, stop) produces a sequence of integers from start to stop-1 in
steps of 1. For example range(2,10) produces a list of integers 2, 3, 4, 5, 6, 7, 8, 9. The value of
start must be less than stop, otherwise range() will generate an empty sequence.
With three parameters: range(start, stop, step) produces a sequence of integers from start
tostop-1 in steps of step. For example range(2,10,3) produces a list of integers 2, 5, 8.

Statement Values generated

range(10) 0,1,2,3,4,5,6,7,8,9

range(5,10) 5,6,7,8,9

range(3,7) 3,4,5,6

range(5,15,3) 5,8,11,14

range(9,3,-1) 9,8,7,6,5,4

range(10,1,-2) 10,8,6,4,2

12
for i in range(1,10,-1): ????

Question : Write a program to print the name of a student and print it 20 times

Example one – Using only one argument in range()

for I in range(5):

print(I, end=', ')


Output
0, 1, 2, 3, 4,

Example one – Using 2 arguments in range()

Example

Example one – Using 3 arguments in range()

The range() function defaults to increment the sequence by 1, however it is possible to specify
the increment value by adding a third parameter: range(2, 30, 3):

Example

Increment the sequence with 3 (default is 1):

for x in range(2, 30, 3):


print(x)
2
5
8
11
14
17
20
23
13
26
29

for i in range(2, 14, 2):

print(i, end=', ')

Output

2,4,6,8,10,12,

Example

for number in range(4,-1,-1):

print (number, end=', ')

Output
4,3,2,1,0,
How can we remove the comma coming in the end????

Solved Example
To find the sum of numbers 1-7.
sum=0
for n in range(1,8):
sum+=n
print(“the sum of numbers 1-7 is”, sum)

Output
the sum of numbers 1-7 is 28

predict the output


print('A')
print('B')
for I in range(5):
print('C')
for I in range(5):
print('D')
print('E')

14
Example: Following is the script to display first “n” natural numbers, where “n” is input from the
user.

n = int(input("Enter a number: "))


for i in range(1,n+1): #n+1 because otherwise it will display 1 less than the number
print(i)

Questions
1. Accept 2 numbers and print the numbers in that range. Eg. If 2 and 26 is entered then print
from 2 to 26
2. Accept 5 numbers and print the sum
3. Accept 5 numbers and print the count of odd numbers, and their sum
4. Print the series 1,8,64,125……..1000

“while” loop

It is also called pre tested or entry controlled loop. Ie. Statements of the loop are
executed when the condition returns a true value and the loop terminates when the
condition becomes false statement(s) will be executed as long as Boolean expression
evaluates to true.

While loop may not execute even once, if the condition evaluates false, initially, as the
condition is tested before entering the loop (entry controlled / pre-tested loop).

Its syntax is:


while condition:

STATEMENTs

Indentation is important in loops like we learnt in “if” statement

15
Example : to print nos. from 1 to 5
i=1
while (i<=5):
print(i)
i = i+1 #could be also be written as i+=1

Output is
1
2
3
4
5

16
Infinite loop or Endless loop

When a loop doesn’t terminate and executes repeatedly it is termed as an endless loop. It
executes so because the test expression doesn’t return a false value and so the
statements of the loop are executed endlessly.

i=1 i=1 i=1


while (i<=5): while (i<=5): while (i<=5):
print(i) print(i) print(i)
In the above code, the valueof i=i+1 i=i+1
the variable is not
incremented so the loop In the above code, the value of i=i-1
condition will never become i is incrementedafter
termination of the loop In the above code, the value of
false and hence the loop I is incrementedand then
executes repeatedly decremented simultaneously

17

You might also like