Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- So, onto more control structures! Now we tackle loops, which are probably the most
- used structure you have. They're really good for repetitive tasks.
- Here is a basic loop, called while. It executes exactly as you would expect.
- while ( condition is true ). It's like a looping "if". Also take note that I don't check
- equality with ==. Because "done" is a boolean itself, it doesn't need to be checked for equality.
- Saying (not False == True) evaluates to true, so why not just say True in the first place?
- Also notice that I embedded an "if" inside our loop. That's completely fine, and very common.
- """
- done = False
- counter = 0
- while ( not done ):
- print "done = "+str(done)+", counter = "+str(counter)
- counter = counter + 2
- #You may not have ran into the modulo operator (%) before
- #Modulo means take the remainder. So if the remainer is 0, then we're done (e.g. we hit 10)
- #Mods are used a lot in encryption to define mathematical groups of numbers
- if ( counter % 10 == 0 ):
- done = True
- print "Done loop 1\n"
- """
- This is rather mathematical sounding, but if you just read it aloud it might be
- easier to think about. "for some variable i in the range of 0 to 5 (upper bound exclusive), do this thing"
- This means 'i' will take the value 0 and then execute it's body.
- 'i' will then become 1 and execute it's body.
- It will do this for 2, and then every number in the range-1 (in this case, 4).
- This might seem very strange, as you told it to go to 5, but there actually is
- a rational behind it. If we look at lists and data structures this will make more sense
- Also, 'i' is available AFTER the loop ends. In some languages, 'i' is available ONLY inside the loop.
- By available, I mean 'i' still has a value after the loop.
- 'i' is a very common variable name for counters (especially when looping). j and k also make appearances.
- """
- #range(0,5) just gives us a list of numbers from 0-4 [0, 1, 2, 3, 4]
- #If you need to use really large numbers (something like range(0, 600000000) look up
- #xrange in the python documentation. Don't worry about this right now though.
- for i in range(0, 5):
- #this will print i 5 times - 0, 1, 2, 3, 4.
- print "i is currently "+str(i)
- print "After the loop, i's value is "+str(i) #4
- print "Done loop 2\n"
- #Now, we can repeat this process, but this time we will use the break command. Break will stop the loop.
- for i in range(0,5):
- print "i is currently "+str(i)
- if ( i == 2 ):
- break
- print "Done loop 3\n"
- #Looping can also be really helpful with strings and lists.
- myName = "Nathan"
- #len(x) is a built in function to give you the length of a string or list.
- #This will print "N a t h a n"
- #Putting a comma after any print statement will make it print on the same line with a space after it.
- #Try changing myName to something else to see how len() works.
- for i in range(0, len(myName)):
- print myName[i],
- print "\nDone loop 4\n"
- #This is our first list. You build it with brackets ([]) and commas in between items. You
- #can access items with variableNameOfList[index]
- #e.g. myList = [3, 68, 13123]
- #myList[0] would be 3, as it is at index 0.
- #myList[1] would be 68.
- #myList[2] would be 13123.
- #myList[3] WILL cause an error!
- randomPeople = ["Amy", "Tyler", "Jenna", "Joseph"]
- print "There are "+str(len(randomPeople))+" random people in this list."
- #Just as a refresher, THIS is why range goes from x to y, lower bounds inclusive and upper bounds exclusive.
- #len(randomPeople) is 4, but our indexes are 0, 1, 2, and 3!
- #If you think that's strange, it's because this is something that is inherited from C, another programming language
- #that most languages are built in.
- #It's very common in every language - size or length usually gives you the number of ITEMS
- #and the indexes usually go from 0 to length-1.
- for i in range(0, len(randomPeople)):
- print "Person "+str(i)+"'s name is "+randomPeople[i]
- print "Done loop 5\n"
- #You can add items into a list using append, which adds the item at the end of the list
- #You can also use insert, by supplying two parameters to the insert function
- #The first parameter tells the item where to go, and the second parameter is the actual item
- randomPeople.append("Rahul")
- randomPeople.insert(2, "Bubba")
- for i in range(0, len(randomPeople)):
- print "Person "+str(i)+"'s name is "+randomPeople[i]
- print "Done loop 6\n"
- #and you can take them away using their actual values
- randomPeople.remove("Jenna")
- for i in range(0, len(randomPeople)):
- print "Person "+str(i)+"'s name is "+randomPeople[i]
- print "Done loop 7\n"
- #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