pub-3785248829033902 | Python: Loops | - Big data with Python

| Python: Loops |

For Loops are traditionally used when you have a piece of code which you want to repeat n number of times.

As an alternative, there is the While Loop, however, while is used when a condition is to be met, or if you want a piece of code to repeat forever. 

The best way to learn new things is to take a practical approach of the things you want to learn. Here is a fragment of code that demonstrates the use of the Loop statement in Python:

Æ’ python command

for i in range(0,2):
      print ('Hi')

for i in range(1,4):
    print (i)

for i in range(0, 3):
    print ("This lesson started %d" % (i) + ' seconds ago')

for i in ['Joe', 'Zoe', 'Brad']:
    invitation = 'Hi ' + i + '. Please come to my party on Saturday!'
    print(invitation)

#Running through all the items in a list is called traversing the list, or traversal.

output

Hi
Hi

1
2
3

This lesson started 0 seconds ago
This lesson started 1 seconds ago
This lesson started 2 seconds ago

#traversing:

Hi Joe. Please come to my party on Saturday!
Hi Zoe. Please come to my party on Saturday!
Hi Brad. Please come to my party on Saturday!     


Previous
Next Post »