1 / 12

CATHERINE AND ANNIE

Python: Part 3. CATHERINE AND ANNIE. 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.

Download Presentation

CATHERINE AND ANNIE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python: Part 3 CATHERINE AND ANNIE

  2. 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!

  3. For loops • You can use a for loop to repeat a section of code multiple times. • Let’s take a look: open the file called “forLoop.py” • What do you think this program will do? How do you know?

  4. Breaking down forLoop.py • Let’s break the loop down, because you’ve 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

  5. 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. • Let’s see an example: open the file called “whileLoop.py”

  6. whileLoop.py • You’ve 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.

  7. 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

  8. 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

  9. An example • Let’s 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 don’t understand? • Let’s run this program to see what it does

  10. 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

  11. 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:

  12. 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.

More Related