Presentation on theme: "CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?"— Presentation transcript:
Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times? You can also use loops in Python to do things multiple times. Do you remember the last program we had you write? It probably got really tedious having to create separate variables for each number and ask for values however many times….there has to be an easier way!
For loops You can use a for loop to repeat a section of code multiple times. Lets take a look: open the file called forLoop.py What do you think this program will do? How do you know?
Breaking down forLoop.py Lets break the loop down, because youve seen every other part of the code for i in range(10): i is just a variable that the loop increases by 1 each time the loop completes. You can also use the variable i within the loop i starts with a value of 0 and ends with a value of 9 num = input(Please enter a number: ) Each time the loop completes, num is given a new value, which the user enters sum = sum + num Why does using this syntax work? Because most programming languages evaluate the right side of the equal sign first, and then assign that value to the left hand side of the equal sign
While loops There is also a different kind of loop called a while loop. While loops evaluate a condition at the beginning of each loop, such as checking to see if two numbers are equal, and if that condition is met (returns the boolean True in CS talk), then the loop is completed. Lets see an example: open the file called whileLoop.py
whileLoop.py Youve seen most of this syntax before, but there are some things that are new while x > 5: Each time the loop begins, the computer checks to make sure that x is greater than 10. If it is, then the loop completes x = x – 1 With the for loop, we did not have to increment or decrement i at all. The loop did that for us. While loops will not do this automatically, so you have to remember to do it yourself.
Using Lists A list is a data structure. Lists have names just like variables do A list is useful because it can hold a whole bunch of different values in one structure Each piece of information contained in a list is called an element Each element also has an index, which tells you where in the list that element is located Indexing always starts at ZERO and the last index is the size of the list minus one
Some useful list methods in Python To access an individual element of a list: list[index], where list is the name of the list, and index is the index of the element list.append(item) – allows you to add items to a list individually len(list) – returns the length of the list index(item) – returns the index of an item item in list – determines whether an item is in a given list del(list[index]) – deletes one element of a list list.sort() – sorts the items of the list Either an ascending alphabetical order (A – Z) or in numerical order. Strings that are capitalized are placed before strings that are lower case
An example Lets look at an example of lists in Python: open the file called listExample.py What do you think this program will do? How can you tell? Is there anything you dont understand? Lets run this program to see what it does
Breaking down listExample.py newList =  This creates an empty list called newList. There is literally nothing in there otherList = list() This uses the list constructor create an empty list In order to put values into newList, we created a whole bunch of variables, got a value for each one from the user, and then added it to the list In order to put values into otherList, we used a for loop. Which method would you want to use? Which method seems easier? You can use either of these to create a new, empty list
Performing actions on all the elements in a list You can use a for loop to access all the elements in a list However, this is exclusive to Python! To access all the elements in a list in another way you can use the following syntax:
Your turn! Open the file Lesson 3_exercises and do the exercises. Remember not to attempt challenge problems until you have done all the required problems.