10. Python Looping Statements (for, While) (2)
10. Python Looping Statements (for, While) (2)
Explanation:
range(10) converts into range(0,10,1). So generates 0,1,2,3,4,5,6,7,8,9 numbers
Here by default start-value is 0 and step-size is 1. Given value is stop-value.
Example:
>>> for i in range(10):
print(i)
output
0
1
2
3
4
5
6
7
8
9
>>>
To print output in same line (horizontal) format then use end=' ' attribute inside
print() method.
>>> for i in range(10):
print(i, end=' ')
Output:
0123456789
Loops or Iterative Statements
If you want to execute the certain statements multiple times then we have to use
looping statements.
Python loops are fundamental programming constructs that allow you to execute a
block of code repeatedly, based on a condition or over a sequence of elements.
They are widely used in various scenarios for tasks such as iterating over data,
automating repetitive actions, and managing control flow in programs.
In this case, the variable item is used to hold the value of every item present in the
sequence before the iteration begins until this particular iteration is completed.
Loop iterates until the final item of the sequence are reached.
Example2: Creating a List sequence and iterating each item from this
lst = [1,2,3.5,"Python",4+5j,True]
for i in lst:
print(i)
Example3: Creating a Tuple sequence and iterating each item from this
tup = (1,2,3,True,False,"Srinivas")
for i in tup:
print(i)
Example4: Creating a Set object and iterating each item from this
se = {2,3,'Python',0,0,True,2+6j,'Srinivas'}
for I in se:
print(i)
Example5: Creating a Dictionary object and iterating each item from this
dic = {1 : 'a', 2 : 'b', 'c' : 45}
for i in dic:
print(i)
Example:
num = 1
while(num <= 5):
print("the count is: ",num)
num += 1 # num = num + 1
output:
the count is: 1
the count is: 2
the count is: 3
the count is: 4
the count is: 5
Output:
0
01
012
0123
01234
Transfer Statements:
Transfer statements are used to transfer the program control from one location to
another location.
We have different types of transfer statements like
1. break
2. continue
3. pass
1. break
break is a keyword which is used only in looping statements.
when ever break keyword occurs it will stop entire iteration and control goes
outside the loop.
For example:
for i in range(1,10):
if(i % 2 == 0):
break
print(i, end=' ')
Output:
1
2. continue
continue is a keyword which is used only in looping statements.
when ever continue occurs it will stop current iteration and executes from
next iteration onwords.
For example:
for i in range(1,10):
if(i % 2 == 0):
continue
print(i, end=' ')
Output:
13579
3. pass
The pass statement is used as a placeholder for future code. When the pass
statement is executed, nothing happens, but you avoid getting an error when
empty code is not allowed.
Empty code is not allowed in loops, function definitions, class definitions, or
in if - else statements.
pass is a keyword which is used in program at the time of partial
development of code blocks.
Example1:
if 10 > 5:
pass
else:
pass
Example2:
for i in range(5):
pass
Example3:
while condition:
pass
Example4:
def addition(a,b):
pass
print('ok')
Output:
1 2 4 for loop iterations completed
ok