omegazero

loops.py

Jun 21st, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. """
  2. So, onto more control structures! Now we tackle loops, which are probably the most
  3. used structure you have. They're really good for repetitive tasks.
  4.  
  5. Here is a basic loop, called while. It executes exactly as you would expect.
  6. while ( condition is true ). It's like a looping "if". Also take note that I don't check
  7. equality with ==. Because "done" is a boolean itself, it doesn't need to be checked for equality.
  8. Saying (not False == True) evaluates to true, so why not just say True in the first place?
  9.  
  10. Also notice that I embedded an "if" inside our loop. That's completely fine, and very common.
  11. """
  12. done = False
  13. counter = 0
  14. while ( not done ):
  15.     print "done = "+str(done)+", counter = "+str(counter)
  16.     counter = counter + 2
  17.     #You may not have ran into the modulo operator (%) before
  18.     #Modulo means take the remainder. So if the remainer is 0, then we're done (e.g. we hit 10)
  19.     #Mods are used a lot in encryption to define mathematical groups of numbers
  20.     if ( counter % 10 == 0 ):
  21.         done = True
  22. print "Done loop 1\n"
  23.  
  24. """
  25. This is rather mathematical sounding, but if you just read it aloud it might be
  26. easier to think about. "for some variable i in the range of 0 to 5 (upper bound exclusive), do this thing"
  27. This means 'i' will take the value 0 and then execute it's body.
  28. 'i' will then become 1 and execute it's body.
  29. It will do this for 2, and then every number in the range-1 (in this case, 4).
  30. This might seem very strange, as you told it to go to 5, but there actually is
  31. a rational behind it. If we look at lists and data structures this will make more sense
  32.  
  33. Also, 'i' is available AFTER the loop ends. In some languages, 'i' is available ONLY inside the loop.
  34. By available, I mean 'i' still has a value after the loop.
  35.  
  36. 'i' is a very common variable name for counters (especially when looping). j and k also make appearances.
  37. """
  38.  
  39. #range(0,5) just gives us a list of numbers from 0-4 [0, 1, 2, 3, 4]
  40. #If you need to use really large numbers (something like range(0, 600000000) look up
  41. #xrange in the python documentation. Don't worry about this right now though.
  42. for i in range(0, 5):
  43.     #this will print i 5 times - 0, 1, 2, 3, 4.
  44.     print "i is currently "+str(i)
  45. print "After the loop, i's value is "+str(i) #4
  46. print "Done loop 2\n"
  47.  
  48. #Now, we can repeat this process, but this time we will use the break command. Break will stop the loop.
  49. for i in range(0,5):
  50.     print "i is currently "+str(i)
  51.     if ( i == 2 ):
  52.         break
  53. print "Done loop 3\n"
  54.  
  55. #Looping can also be really helpful with strings and lists.
  56. myName = "Nathan"
  57. #len(x) is a built in function to give you the length of a string or list.
  58. #This will print "N a t h a n"
  59. #Putting a comma after any print statement will make it print on the same line with a space after it.
  60. #Try changing myName to something else to see how len() works.
  61. for i in range(0, len(myName)):
  62.     print myName[i],
  63. print "\nDone loop 4\n"
  64.  
  65. #This is our first list. You build it with brackets ([]) and commas in between items. You
  66. #can access items with variableNameOfList[index]
  67. #e.g. myList = [3, 68, 13123]
  68. #myList[0] would be 3, as it is at index 0.
  69. #myList[1] would be 68.
  70. #myList[2] would be 13123.
  71. #myList[3] WILL cause an error!
  72. randomPeople = ["Amy", "Tyler", "Jenna", "Joseph"]
  73. print "There are "+str(len(randomPeople))+" random people in this list."
  74. #Just as a refresher, THIS is why range goes from x to y, lower bounds inclusive and upper bounds exclusive.
  75. #len(randomPeople) is 4, but our indexes are 0, 1, 2, and 3!
  76. #If you think that's strange, it's because this is something that is inherited from C, another programming language
  77. #that most languages are built in.
  78. #It's very common in every language - size or length usually gives you the number of ITEMS
  79. #and the indexes usually go from 0 to length-1.
  80. for i in range(0, len(randomPeople)):
  81.     print "Person "+str(i)+"'s name is "+randomPeople[i]
  82. print "Done loop 5\n"
  83.  
  84. #You can add items into a list using append, which adds the item at the end of the list
  85. #You can also use insert, by supplying two parameters to the insert function
  86. #The first parameter tells the item where to go, and the second parameter is the actual item
  87. randomPeople.append("Rahul")
  88. randomPeople.insert(2, "Bubba")
  89. for i in range(0, len(randomPeople)):
  90.     print "Person "+str(i)+"'s name is "+randomPeople[i]
  91. print "Done loop 6\n"
  92.  
  93. #and you can take them away using their actual values
  94. randomPeople.remove("Jenna")
  95. for i in range(0, len(randomPeople)):
  96.     print "Person "+str(i)+"'s name is "+randomPeople[i]
  97. print "Done loop 7\n"
  98.  
  99. #If you want to see more about lists, go to http://docs.python.org/tutorial/datastructures.html
Advertisement
Add Comment
Please, Sign In to add comment