How to loop in Python and print found items

If you want to create a loop in Python, you can use the the following code. This code uses a list, and we are going to loop through that list with a simple Python loop.

The loop which is shown below uses the myLISTITEMS list which contains the items hacker, security and success.

The len(myLISTITEMS) command will count the items in the list. When you use this with print, it will print out the amount of items which are included in the myLISTITEMS list.

myLISTITEMS = ['hacker', 'security',  'success']
for theITEM in myLISTITEMS:
    print 'The item which was found :', theITEM 
print len(myLISTITEMS)

The code above will print the following

The item which was found : hacker 
The item which was found : security 
The item which was found : success
Share This Message