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