#!/usr/local/bin/python2.7
'''
Learning For/While loops in Python
File name: for_while_loop_python.py
Author: Prabhath Kota
Date: June 22, 2018
Python Version: 2.7
'''
'''
Topics:
For loop
While loop
range
break
continue
enumerate
'''
print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each
print '----------------For loop Break---------------------'
print freedom_fighters
#Print only first 4 elements using Break statement
#Enumerate will get you index of the element as well - starting from 0
#enumerate(freedom_fighters) will start the index from 0
#enumerate(freedom_fighters, 1) will start the index from 1
for index,each in enumerate(freedom_fighters, 1):
print index, each
if index == 4:
break
print '----------------For loop Continue---------------------'
print freedom_fighters
#Print only even elements from the list
for index,each in enumerate(freedom_fighters, 1):
if index % 2 == 1: #Odd elements
continue
else: #Even Elements
print index, each
print '----------------For loop Range---------------'
squares = []
print range(1,10)
for x in range(0,10):
squares.append(x**2)
print (squares)
print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares
'''
Learning For/While loops in Python
File name: for_while_loop_python.py
Author: Prabhath Kota
Date: June 22, 2018
Python Version: 2.7
'''
'''
Topics:
For loop
While loop
range
break
continue
enumerate
'''
print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each
print '----------------For loop Break---------------------'
print freedom_fighters
#Print only first 4 elements using Break statement
#Enumerate will get you index of the element as well - starting from 0
#enumerate(freedom_fighters) will start the index from 0
#enumerate(freedom_fighters, 1) will start the index from 1
for index,each in enumerate(freedom_fighters, 1):
print index, each
if index == 4:
break
print '----------------For loop Continue---------------------'
print freedom_fighters
#Print only even elements from the list
for index,each in enumerate(freedom_fighters, 1):
if index % 2 == 1: #Odd elements
continue
else: #Even Elements
print index, each
print '----------------For loop Range---------------'
squares = []
print range(1,10)
for x in range(0,10):
squares.append(x**2)
print (squares)
print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares
Output:
----------------For loop---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
Lala Lajipati Rai
Dadabhai Naoroji
Rajendra Prasad
Sarojini Naidu
Dadabhai Naoroji
Lal Bahadur Shastri
----------------For loop Break---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
1 Lala Lajipati Rai
2 Dadabhai Naoroji
3 Rajendra Prasad
4 Sarojini Naidu
----------------For loop Continue---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
2 Dadabhai Naoroji
4 Sarojini Naidu
6 Lal Bahadur Shastri
----------------For loop Range---------------
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
----------------While loop---------------
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[Finished in 0.2s]
No comments:
Post a Comment