Class x-A.I-Python-notes-loops
Class x-A.I-Python-notes-loops
Class x-A.I-Python-notes-loops
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.
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.
Syntax:
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.
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
for I in range(5):
Example
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
Output
2,4,6,8,10,12,
Example
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
14
Example: Following is the script to display first “n” natural numbers, where “n” is input from the
user.
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).
STATEMENTs
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.
17