BCC 302 Unit2 Lecture5
BCC 302 Unit2 Lecture5
BCC-302 / Unit II 2
LOOPS
•Loops are used in programming to iteration a specific block of code.
•Iteration means executing the same block of code over and over, potentially many
times. A programming structure that implements iteration is called a loop.
•In programming, there are two types of iteration, indefinite and definite:
With indefinite iteration, the number of times the loop is executed isn’t specified
explicitly in advance. Rather, the designated block is executed repeatedly as long as
some condition is met.
With definite iteration, the number of times the designated block will be executed is
specified explicitly at the time the loop starts.
•The main difference is that we use while loop when we are not certain of the number
of times the loop requires execution, on the other hand when we exactly know how
many times we need to run the loop, we use for loop.
BCC-302 / Unit II 3
For Loop
•Like the while loop, the for loop provides a mechanism to repeat a task until a
particular condition is True.
•The For loop is usually known as a determinate or definite loop because the
programmer knows exactly how many times the loop will repeat. The number of
times the loop has to be executed can be determined mathematically checking the
logic of the loop.
•Definite iteration loops are frequently referred to as for loops because for is
the keyword that is used to introduce them in nearly all programming languages,
including Python.
BCC-302 / Unit II 4
Syntax of For Loop in Python-
BCC-302 / Unit II 5
There is a difference in for loop syntax. Python
syntax uses the range function which makes the
loop simpler, more expressive, and less prone to
error(s).
BCC-302 / Unit II 6
The for loops in python are slightly different from the for loops in other
programming languages.
The Python for loop iterates through a sequence of objects, i.e, it iterates through
each value in a sequence, where the sequence of object holds multiple items of data
stored one after another.
The for loop is a Python statement which repeats a group of statements for a
specified number of times. The keywords for and in are essential keywords to
iterate the sequence of values.
BCC-302 / Unit II 7
Range() Function
•The range() function is a built-in function in Python that is used to iterate over a
sequence of numbers. The syntax of range() is :
range(beg, end, [step])
•The range() produces a sequence of numbers starting with beg (inclusive) and
ending with one less than the number end.
BCC-302 / Unit II 8
So, Python has an inbuilt function called range(), which is used to generate a list
of integers.
BCC-302 / Unit II 9
Examples of range () Function:
Note: The second number, i.e. 6 is not included in the elements of this list. By
default, the difference between the two successive numbers is one.
The above range (1,6) is equivalent to range(6). The output of both the range
functions will be the same.
BCC-302 / Unit II 10
Example 2: Create a list of integers from 1 to 20 with a difference of 2 between
two successive integers.
Example 3:
BCC-302 / Unit II 11
Example of Range function Output
range(5) [0, 1,2,3,4]
range(5,0,-1) [5,4,3,2,1]
range(5,0,-2) [5,3,1]
range(-4,4) [-4,-3,-2,-1,0,1,2,3]
range(-4,4,2) [-4,-2,0,2]
range(0,1) [0]
range(1,1) Empty
range(0) Empty
BCC-302 / Unit II 12
PROGRAMS ON FOR LOOP
Program 1
Output-
enter the no.0
factorial of 0 is 1
enter the no.1
factorial of 1 is 1
enter the no.4
factorial of 4 is 24
BCC-302 / Unit II 13
Program 2
Output-
The range() function contains 3 different parameters, viz. (begin , end, step _
size). In the above program, the range function contains the values 65, 90 and 1. It
indicates to print the characters whose ASCII value starts from 65 and ends at 90.
Therefore, the statement print( chr ( i ), end=“ ”) is used to print equivalent
character value of ASCII value.
BCC-302 / Unit II 14
Program 3
WAP to print the numbers from one to ten in reverse order using for loop.
Output-
Numbers from 1 to 10 in Reverse Order:
10 9 8 7 6 5 4 3 2 1
End of The Program
BCC-302 / Unit II 15
Program 4
Output-
Enter the value of n :2
The sum of first”, n , “natural numbers is 3
The average of first”, n, “natural numbers is 1.5
Enter the value of n :10
The sum of first”, n , “natural numbers is 55
The average of first”, n, “natural numbers is 5.5
BCC-302 / Unit II 16
Program 5
num = int (input(“ Enter the no. of digits you want in series (minimum 2) :”))
first = 0
second = 1
print(“ \n fibonacci series is : ”)
print(first , “ , ”, second, end = “ , ”)
for i in range( 2, num):
next = first + second
print(next, end = “ , ”)
first = second
second = next
Output-
BCC-302 / Unit II 17
WAP TO DISPLAY THE PYRAMID PATTERN
row= int(input("enter the no of row."))
for i in range(0, row):
for j in range(0, row-i-1):
print(" ",end="")
for j in range(0,i+1):
print("* ", end="")
print()
Output
enter the no of row.6
*
**
***
****
*****
******
BCC-302 / Unit II 18
WAP to print *’s in pyramid style(also known as equivalent
triangle
BCC-302 / Unit II 21
WAP TO CHECK THE NO. IS ARMSTRONG OR NOT
num=int(input("enter the no. "))
Output:
orginal_num=num enter the no. 153
n=len(num) 153 is armstrong no.
rem=0
arm=0 enter the no. 370
while num>=1:
370 is armstrong no.
rem=num%10
arm=arm+rem**n
num=num//10 enter the no. 371
#print(rem) 371 is armstrong no.
#print(arm)
if(orginal_num==arm):
enter the no. 9
print(orginal_num, "is armstrong no.")
else: 9 is not armstrong no.
print(orginal_num, "is not armstrong no.")
BCC-302 / Unit II 22
References
• Text Books: Python Programing by Reema Thareja
• Reference Books: Python Programming by Anurag Gupta and G P
Biswas
BCC-302 / Unit II 23
Thank You
BCC-302 / Unit II 24