Session 10
Session 10
Output:
0123456789
Processing Involved
Print step by step
Test Cases
Input
-1
Output
Invalid input
Processing Involved
Boundary condition check fails
For iteration
In while loop, we cannot predict how many
times the loop will repeat
The number of iterations depends on the
input or until the conditional expression
remains true
While loop is ideal when stop criteria is not
explicit
Control flow of for statement
Syntax of for Statement
for target in object:
# Assign object items to target
statements
if test: break # Exit loop now, skip else
if test: continue # Go to top of loop now
else: statements # If we didn't hit a
'break
For and Strings
for iterating_var in sequence or range:
statement(s)
Example:
for letter in 'Python':
print 'Current Letter :', letter
For and Strings
When the above code is executed:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
For and Range
for n in range(1, 6):
print(n)
When the above code is executed:
1
2
3
4
5
range function call
Syntax - range( begin,end,step )
where
Begin - first value in the range; if omitted, then default value
is 0
end - one past the last value in the range; end value may not
be omitted
Step - amount to increment or decrement; if this parameter
is omitted, it defaults to 1 and counts up by ones
Output:
2
4
6
8