For Loops CMSC 201. Overview Today we will learn about: For loops.

Slides:



Advertisements
Similar presentations
Lists CMSC 201. Overview Today we will learn about: For loops.
Advertisements

Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
Lists Introduction to Computing Science and Programming I.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
Last Week Doctests Lists List methods Nested Lists Aliasing.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Lists in Python.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
By the end of this session you should be able to...
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Last Week if statement print statement input builtin function strings and methods for loop.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
PYTHON LISTS. What are lists? An ordered set of elements A variable with 0 or more things inside of it Examples myList = [8, 6, 7, 5, 3, 0, 9] strList.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Count and add list of numbers From user input and from file.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
COMPSCI 105 SS 2015 Principles of Computer Science
Control flow Ruth Anderson UW CSE 160 Spring
The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g.
ITEC 109 Lecture 20 Arrays / Lists. Arrays Review For loops –Rationale –Syntax –Examples.
Control flow Ruth Anderson UW CSE 160 Winter
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Comparing Brand Rivalries Download from course website: data (then extract it) 3-4Starter.py.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Looping Examples I.
COMPSCI 107 Computer Science Fundamentals
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Generating Random Numbers
For loop, definite vs indefinite iteration, range, random
CMSC201 Computer Science I for Majors Lecture 09 – For Loops
CMSC201 Computer Science I for Majors Lecture 14 – For Loops
While Loops in Python.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CMSC201 Computer Science I for Majors Lecture 15 – For Loops
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Determinate Loops with the
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Loop Structures and Booleans Zelle - Chapter 8
Michael Ernst UW CSE 140 Winter 2013
CS 1111 Introduction to Programming Fall 2018
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Lists in Python Outputting lists.
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Some Common Issues: Iteration
UMBC CMSC 104 – Section 01, Fall 2016
CHAPTER 6: Control Flow Tools (for and while loops)
CS 1111 Introduction to Programming Spring 2019
The structure of programming
More Loops Topics Counter-Controlled (Definite) Repetition
2-Dimensional Lists (Matrices) in Python
More Loops Topics Counter-Controlled (Definite) Repetition
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
While Loops in Python.
More Loops Topics Counter-Controlled (Definite) Repetition
Control flow: Loops UW CSE 160.
Presentation transcript:

For Loops CMSC 201

Overview Today we will learn about: For loops

Motivation A lot of times, we have a pattern like this: myList = [1, 2, 3] a = 0 while a < len(myList): print(myList[a]) This is a way we can iterate over a list.

For Loops Equivalent for loop: myList = [1, 2, 3] for listItem in myList: print(listItem)

For Loops myList = [1, 2, 3] for listItem in myList: print(listItem) Loop control handled by for loop. The first time through the loop, listItem will be the first element of the list. The second time through the loop, it will be the second element, and so on until the list is done.

Example Finding the average using for loops: myList = [1, 2, 3, 4] sum = 0 for listItem in myList: sum = listItem + sum print( sum / len(myList) )

A Downside! What do you think this code does? myList = [1, 2, 3, 4] for listItem in myList: listItem = 4 print(myList)

A Downside! What do you think this code does? myList = [1, 2, 3, 4] for listItem in myList: listItem = 4 print(myList) Prints: [1, 2, 3, 4]

A Downside! What do you think this code does? myList = [1, 2, 3, 4] for listItem in myList: listItem = 4 print(myList) Changing listItem DOES NOT CHANGE THE ORIGINAL LIST! listItem is just a copy of each element.

For Loop Trick Frequently we want to do something like this: a = 0 while a < 10: print(a) a = a + 1 A lot of times, we want loops that count.

For Loop Trick Python built-in function for consecutive numbers! aList = range(0, 10) print(aList) Prints: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range(start, end) returns a list of numbers start, start+1, start+2,..., end - 1

For Loop Trick aList = range(0, 10) print(aList) So now we can create a list of numbers. Any ideas what we can do with that?

For Loop Trick for num in range(0, 10): print(num) An easier way to make a counting loop! Prints:

For Loop Trick We can even count by whatever number we want! for num in range(0, 10, 2): print(num) Prints:

Summary: For loops: o Use for iterating over lists o Also use in situations where we want loops to count. While loops: o Use when we have some condition that doesn’t fall into the previous two categories.