0% found this document useful (0 votes)
2 views24 pages

BCC 302 Unit2 Lecture5

This document covers Loop Control statements in Python, specifically focusing on the 'for' loop and its syntax. It explains the concept of iteration, the difference between definite and indefinite loops, and provides examples of using the range() function. Additionally, it includes several programming exercises demonstrating the application of for loops in various scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

BCC 302 Unit2 Lecture5

This document covers Loop Control statements in Python, specifically focusing on the 'for' loop and its syntax. It explains the concept of iteration, the difference between definite and indefinite loops, and provides examples of using the range() function. Additionally, it includes several programming exercises demonstrating the application of for loops in various scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Subject Name:-Python Programming

Subject Code:- BCC-302/402


Unit No.:- 2
Lecture No.:- 5

Topic Name :-Loop Control statements –Part II

Mr. Beerbal Solanki


Assistant Professor
Department of CSE
BCC-302 / Unit II 1
Contents
1:- For loop
2:-Examples of For loop
3:-References

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.

•The for…in statement is a looping statement used in Python to iterate over a


sequence of objects, i.e., go through each item in a sequence. Here, by sequence we
mean just an ordered collection of items.

•Python "for" Loops are an example of Definite Iteration.

•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-

for <variable> in <sequence>:


# body_of_loop that has set of statements
# which requires repeated execution

Figure: for loop construct

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).

When a for loop is used, a range of sequence is


specified (only once). The items of the sequence
are assigned to the loop control variable one after
the other.

The for loop is executed for each item in the


sequence. With every iteration of the loop, a check
is made to identify whether the loop control
variable has been assigned all the values in the
range. If all the values have been assigned , the
statement block of the loop is executed else, the Fig: Flowchart of for loop in
statements comprising the statement block of the Python
for loop are skipped and the control jumps to the
immediate statement following the for loop body.

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 syntax of for loop is given as follows:

for var in sequence:


statement(s)
………………….
………………….
………………….

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.

•The step argument is option (that is why it is placed in brackets).

• By default, every number in the range is incremented by 1 but we can specify a


different increment using step. It can be both negative and positive, but not zero.

BCC-302 / Unit II 8
So, Python has an inbuilt function called range(), which is used to generate a list
of integers.

The general form of the range function is:


range( begin, end, step)
where,
The ‘begin’ is the first beginning number in the sequence at which the list starts.
The ‘end’ is the limit, i.e. the last number in the sequence.
The ‘step’ is the difference between each number in the sequence.

BCC-302 / Unit II 9
Examples of range () Function:

Example 1: Create a list of integers from 1 to 5.


>>> list (range(1,6 ) )
[1,2,3,4,5]

range(1,6) function is used in the above example. It generates a list of integers


starting from 1 to 5.

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.

>>> list (range(1,20,2 ) )


[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

range(1,20,2) function is used in the above example. It generates a list of integers


starting from 1 with a difference of two between two successive integers up to 20.

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

Table 1 : Examples of range() function

BCC-302 / Unit II 12
PROGRAMS ON FOR LOOP
Program 1

WAP to calculate factorial of a number.

num = int(input("enter the no."))


fact = 1
for i in range(1, num+1):
fact = fact * i
print("factorial of",num,"is",fact)

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

WAP to display capital letters from A to Z.

print(“The Capital Letters A to Z are as follows:”)


for i in range (65, 91,1):
print (chr (i) , end=“ ” )

Output-

The Capital Letters A to Z are as follows:


ABCDEFGHIJKLMNOPQRSTUVWXYZ

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.

Print(“ Numbers from 1 to 10 in Reverse Order:”)


for i in range(10,0, -1):
print (i, end =“ ”)
print(“\ n End of The Program”)

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

WAP to calculate the average of first n natural numbers.

n = int (input(" Enter the value of n :"))


avg = 0.0
s =0
for i in range(1, n+1):
s= s + i
avg = s / i
print("The sum of first”, n , “natural numbers is ", s)
print("The average of first”, n, “natural numbers is" , avg)

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

WAP to print Fibonacci series up to a specified number.

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-

Enter the no. of digits: 6


fibonacci series is:
0,1,1,2,3,5

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

n=int(input("enter the row"))


for i in range(1,n+1):
print( " "*(n-i), end= "")
print("* "*i)

enter the row3


*
**
***
BCC-302 / Unit II 19
WAP to check the no is prime or not

num=int(input("enter the no :")) Output


enter the no :1
if num>1:
for i in range(2, num//2+1): 1 no. is not
if num%i==0: prime
print(num, " is not prime")
break
enter the no :2
else: 2 is prime
print(num, " is prime")
enter the no :8
else: print(num, "no. is not prime") 8 is not prime
enter the no :9
9 is not prime
BCC-302 / Unit II 20
WAP to check the no is prime or not

num=int(input("enter the no: "))


count=0
Output:
if num>1: enter the no: 1
for i in range(1,num+1): 1 is not prime
if num%i==0:
count+=1 enter the no: 2
if count==2:
2 is prime no.
print(num, "is prime no.")
else: enter the no: 9
print(num, "is not prime") 9 is not prime
else:
print(num, "is not prime")
enter the no: 5
5 is prime no.

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.")

enter the no. 1


1 is 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

You might also like