Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Iterations in python

Rating
-
Sold
-
Pages
29
Uploaded on
08-04-2025
Written in
2024/2025

This PDF contains comprehensive notes covering the fundamental concepts of Python programming. It is designed for beginners who are new to coding or looking to build a strong foundation in Python. The notes are organized in a clear and structured format, making them ideal for self-study, classroom learning, or quick reference.

Show more Read less
Institution
Course

Content preview

Python Application Programming (15CS664) Module II

MODULE II

2.1 ITERATION
The while statement, Infinite loops, “Infinite loops” and break, Finishing iterations with Continue,
Definite loops using for, Loop pattern ,Counting and summing loops, Maximum and minimum loops


2.2 STRINGS
A string is a sequence, Getting the length of a string using len, Traversal through a string with a loop,
String slices, Strings are immutable, Looping and counting, The in operator, String comparison string
methods, Parsing strings, Format operator


2.3 FILES

Files, Persistence, Opening files, Text files and lines, Reading files, Searching through a file, Letting the
user choose the file name, Using try, except, and open, Writing files, Debugging




Mamatha A, Asst Prof, Dept of CSE, SVIT Page 1

,Python Application Programming (15CS664) Module II

MODULE II

2.1 ITERATION
Iteration is a processing of repeating some task. In a real time programming, we require a set of
statements to be repeated certain number of times and/or till a condition is met. Every programming
language provides certain constructs to achieve the repetition of tasks. In this section, various such
looping structures are discussed.

 The while Statement
The while loop has the syntax as below –

while condition:
statement_1
statement_2
…………….
statement_n

statements_after_while


 Here, while is a keyword, the flow of execution for a while statement is as below.
 The condition is evaluated first, yielding True or False
 If the condition is false, the loop is terminated and statements after the loop will be executed.
 If the condition is true, the body will be executed which comprises of the statement_1 to
statement_n and then goes back to condition evaluation.
 Consider an example –
n=1
while n<=5:
print(n) #observe indentation
n=n+1
print("over")

The output of above code segment would be –
1
2
3
4
5
over
 In the above example, a variable n is initialized to 1. Then the condition n<=5 is being checked. As
the condition is true, the block of code containing print statement print(n) and increment statement
(n=n+1)are executed. After these two lines, condition is checked again. The procedure continues till

Mamatha A, Asst Prof, Dept of CSE, SVIT Page 2

, Python Application Programming (15CS664) Module II

condition becomes false, that is when n becomes 6. Now, the while-loop is terminated and next
statement after the loop will be executed. Thus, in this example, the loop is iterated for 5 times.
 Consider another example –
n=5
while n>0:
print(n) #observe indentation
n=n-1
print("Blast off!")

The output of above code segment would be –
5
4
3
2
1
Blast off!
 Iteration is referred to each time of execution of the body of loop.
 Note that, a variable n is initialized before starting the loop and it is incremented/decremented
inside the loop. Such a variable that changes its value for every iteration and controls the total
execution of the loop is called as iteration variable or counter variable. If the count variable is
not updated properly within the loop, then the loop may not terminate and keeps executing
infinitely.


 Infinite Loops, break and continue
 A loop may execute infinite number of times when the condition is never going to become false.
 For example,
n=1
while True:
print(n)
n=n+1
 Here, the condition specified for the loop is the constant True, which will never get terminated.
Sometimes, the condition is given such a way that it will never become false and hence by
restricting the program control to go out of the loop. This situation may happen either due to
wrong condition or due to not updating the counter variable.
 In some situations, we deliberately want to come out of the loop even before the normal
termination of the loop. For this purpose break statement is used.
 The following example depicts the usage of break. Here, the values are taken from keyboard until
a negative number is entered. Once the input is found to be negative, the loop terminates.
while True:
x=int(input("Enter a number:"))
if x>= 0:
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 3

Written for

Course

Document information

Uploaded on
April 8, 2025
Number of pages
29
Written in
2024/2025
Type
Class notes
Professor(s)
Bayregowda
Contains
All classes

Subjects

$7.09
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sanathmogaveer13

Also available in package deal

Get to know the seller

Seller avatar
sanathmogaveer13 VTU
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions