Monday, 23 January 2017

LOOPING STATEMENTS IN PYTHON AND SOME USEFUL KEYWORDS

Looping Statements are used to execute the set of statements repeatedly. Python supports 2 types of looping statements –

1.     for loop – for loop executes the set of statements or blocks with respect to every element of the string/collection objects. Syntax –

for variablename in string/collection variable :
    -------------------------------
    ---------------------------------
    ---------------------------------

range() – range generates the group of values based on the given range and gives it as a list.

Example –
print range(10)

print range(1,11)

print range(0,30,5)

print range(0,10,3)

print range(0,-10,-1)

print range(0)

print range(1,0)
 
Output – 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 5, 10, 15, 20, 25]
[0, 3, 6, 9]
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
[]
[]
 
In python for loop every time condition is not verified so performance will be 
faster.
 
Example 1 –
for x in range(10):

    print x

for y in range(0,10,2):

    print y

for z in range(10):

    if z%2==0:

        print z
 
Output –
0
1
2
3
4
5
6
7
8
9
0
2
4
6
8
0
2
4
6
8
 
Example 2 –
x='rakesh'

print x

for p in x:

    print p,
 
Output –
rakesh
r a k e s h
 
 
2. while loop – while loop executes the set of statements or block repeatedly 
until condition will become false. Syntax
 
            while condition :
                        statement1
                        -------------
                        -------------
 
Example 1 –
sum=0

spam=1

while spam<11:

    sum=sum+spam

    spam=spam+1

print sum
 
Output –
55
 
 
Example 2 –
name=' '

while name!='rakesh':

    name=raw_input('Enter name : ')

print 'Thank you'
 
Output –
Enter name : barsha
Enter name : anjan
Enter name : bikash
Enter name : rakesh
Thank you
 
 
Example for infinite loop –
while True:

    name=raw_input('Enter name : ')

print 'Thank you'
 
Output –
Enter name : barsha
Enter name : bikash
Enter name : anjan
Enter name : runjun
Enter name : rakesh
Enter name : apurba
--------------
--------------
 
 
Break – break statements is used to exit from the loops. Generally, we use the 
break statement in infinite loops.
 
Example –
while True:

    name=raw_input('Enter name : ')

    if name=='rakesh':

        break

print 'Thank you'
 
Output –
Enter name : apurba
Enter name : barsha
Enter name : anjan
Enter name : bikash'
Enter name : rakesh
Thank you
 
 
Continue – continue statement can be used in the loops. Whenever we use 
continue statement without executing the remaining part of the loop control 
will goes to starting of the loop and continues the next iteration.
 
Example –
while True:

    name=raw_input('Enter name : ')

    if name!='rakesh':

        continue

    print 'hello rakesh, enter password'

    password=raw_input('Enter Password : ')

    if password=='hacker':

        break

print 'Access granted'
 
Output –
Enter name : apurba
Enter name : rakesh
hello rakesh, enter password
Enter Password : hacker
Access granted
 
Example –
count=1

while True:

    if count==5:

        count=count+1

        continue

    print 'hi ',count

    count = count+1

    if count==10:

        break
 
Output –
hi  1
hi  2
hi  3
hi  4
hi  6
hi  7
hi  8
hi  9
 
 
Pass – Syntactically block is required but we don’t want to perform any action 
logically then we can replace the block with ‘pass’. pass is a keyword it does 
nothing.
 
Example –
i=100

while i<=100:

    pass

if i<200:

    pass

for i in range(10):

    pass
 
 
 
Del – del is a keyword, which is used to remove the variables or elements from 
the memory location. After using the variables, then it is recommended to 
remove those variables from the memory location by using ‘del’ keyword. 
If you don’t remove the variables, after usage is over then those variables 
available in the memory location until execution of the program is over, so that 
performance of the application will be degraded. In garbage collector, it removes 
spaces after entire program is completed but del is deleted within the program.
 
Example –
i=1000

j=2000

k=i+j

print k

del k

x=i-j

print x

del x

print k

print x
 
Output –
3000
Traceback (most recent call last):
-1000
  File "C:/Users/om/PycharmProjects/Pythonn/if3.py", line 9, in <module>
    print k
NameError: name 'k' is not defined
 
 
Note – we can’t remove the elements of the immutable objects by using del 
keyword but we can remove the immutable objects by using del keyword.
 
Example –
i='rakesh'

del i

x='apurba'

del x[2]
 
Output –
Traceback (most recent call last):
  File "C:/Users/om/PycharmProjects/Pythonn/if3.py", line 4, in <module>
    del x[2]
TypeError: 'str' object doesn't support item deletion
 
 


No comments:

Post a Comment