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?

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
While loops.
Introduction to Computing Science and Programming I
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Python
Lists in Python.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
For loops in programming Assumes you have seen assignment statements and print statements.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Decision Structures, String Comparison, Nested Structures
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Python Let’s get started!.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
Containers and Lists CIS 40 – Introduction to Programming in Python
Chapter 2 Assignment and Interactive Input
Lesson 3 - Repetition.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
While Loops in Python.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Conditions and Ifs BIS1523 – Lecture 8.
Data Structures – 1D Lists
T. Jumana Abu Shmais – AOU - Riyadh
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Loops CIS 40 – Introduction to Programming in Python
Introduction to TouchDevelop
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Lesson 09: Lists Class Chat: Attendance: Participation
Problem Solving Designing Algorithms.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
For loops Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 2 due today.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 3 due today.
While Loops in Python.
Software Development Techniques
Introduction to Computer Science
Presentation transcript:

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