#In this example we will look at some loops, the differences and their use #We will only be looking at two loops in this example but they're many more #the two loops in which we'll be looking at are FOR and WHILE LOOPS #1. FOR LOOP #The first loop we are going to look at is the for loop, this is one of the most common used loops #A for loop in python is really a foreach loop, they are all one in python under for loop #A for loop can be used when checking something e.g. 1 is less than 2 #there's many different uses for loops in python, we shall look at some of the ocmmon ones #let us look at an example below: for i in range(1, 5): print i #the above will loop through all values between one and 5 and then print them all out #NOTE that it prints 1 but does not print out 5, the last vaue is the number you wish to go up to #the value i equals the current item we are looping i.e. i will first equal 1 #The range defines that we wish to search a range of numbers in the range 1 to 5 #Let us look at another example of a for loop, this time we shall loop through each item in a list #we first set up a list called mylist and give it some values, these can be both integer or a string mylist = [1,2,3,4,5,"hello","world"] #we will now loop through each item in mylist printing out each element within it for i in mylist: print i #that is all, we tell i to equal the current item within the list and then go through each item #we then print out each item. We use i because it is easy to write :-) #also notice that we mix both integers and strings within the list as this is allowed #2. WHILE LOOP #ok it is time to look at a different type of loop, this time the while loop. #this loop is very effective if used correctly but has many side effects if used wrongly #one of these side effects being tieing up the whole CPU which stops you doing anything within a program #the while loop is used while something is happening e.g. while 1 is less than 5 do something #the following example shows an example of a while loop in operation count = 0 #we first create a variable called count while (count < 9): #we say while count is less than 9 we wish to do something print 'The count is:', count # we then print out the words "the count is:" followed by count value count = count + 1 #next we increment the value of count by one and we go around the loop again #NOTE:- I always try and stay away from while loops unless i need to use them, for loops are better #For loop are also easier on the CPU but sometimes you do need to use a while loop as the following shows: #while something: #do something #the above experession is perfectly correct syntax in python, it means an expression like that would be allowed #the above while loop style can be used when reading lines from a text file etc. #a final advanced while loop statement is shown below #we create a variable called var and tell it to equal to one var = 1 while var == 1 : # This constructs an infinite loop, while it is equal to 1 we want the following to happen num = raw_input("Enter a number :") #num variable equals the raw_input, this is data in which user enters #the raw input will say "enter a number :" and the user will type in a number which is then printed print "You entered: ", num #we tel var to equal 2 to get out of the loop var = 2 #good bye is then printed here, notice though the while loop keeps looping until user types something in and hits enter print "Good bye!" #THAT IS ALL, YOU NOW KNOW HOW TO USE LOOPS IN PYTHON #YOU CAN LOOK UP OTHER TYPES OF LOOP TOO WUCH AS 'DO' AND 'DO WHILE' LOOPS BUT THEY ARE NOT COMMONLY USED DEPENDING ON YOUR PURPOSE