Topic: Loops Loops Idea While Loop Introduction to ranges For Loop

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
For loops in programming Assumes you have seen assignment statements and print statements.
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
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.
CS 100 Introduction to Computing Seminar October 7, 2015.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
1 CS Review, iClicker -Questions Week 15. Announcements 2.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Adapted from slides by Marty Stepp and Stuart Reges
More about Iteration Victor Norman CS104. Reading Quiz.
Announcements Project 2 Is now posted Will be due on Oct. 1 Week 7 Midterm on Wed. Oct. 3 in EE 129 Come early, EE is a complicated building Review session.
Python Boot Camp Booleans While loops If statements Else clauses
Topic: Iterative Statements – Part 1 -> for loop
REPETITION CONTROL STRUCTURE
Python: Experiencing IDLE, writing simple programs
Python Loops and Iteration
Topics Introduction to Repetition Structures
Python Programming: An Introduction to Computer Science
Python: Control Structures
Python Programming: An Introduction to Computer Science
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CS 115 Lecture 8 Structured Programming; for loops
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 5: Repetition Structures
Repetition: the for loop
While Loops in Python.
Topics Introduction to Repetition Structures
Control Structures - Repetition
Writing Functions( ) (Part 5)
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
4. sequence data type Rocky K. C. Chang 16 September 2018
Loops CIS 40 – Introduction to Programming in Python
More Looping Structures
Topics Introduction to Repetition Structures
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Python programming exercise
M150: Data, Computing and Information
15-110: Principles of Computing
Introduction to Repetition Structures
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
15-110: Principles of Computing
CHAPTER 6: Control Flow Tools (for and while loops)
For loops Taken from notes by Dr. Neil Moore
Topics Introduction to Repetition Structures
Repetition: the for loop
Chopin’s Nocturne.
Python While Loops.
Comparing Python and Java
More Looping Structures
While Loops in Python.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
LOOP Basics.
Introduction to Python
Presentation transcript:

Topic: Loops Loops Idea While Loop Introduction to ranges For Loop Comparison between while and for loops

Loops Loops allow for code to be run multiple times There are many kinds of loops the most basic kind is a while loop count = 0 while (count < 8): print 'The count is: ', count ) count = count + 1

count = 0 print ( 'The count is: ', count ) count = count + 1

Loops Note: The count variable is incremented after the last print statement. Is this the case with the loop code? Verify this! In addition to saving you from copying code over and over, it allows the condition to be a variable Suppose the number of times we want to print depends on some other code.

Example Revisited: motivation behind loops Intuition: we can leverage functions + arguments to get input, we can use the input to parameterize the loop! def printCountNTimes(n): count = 0 while (count < n): print ( 'The count is: ', count ) count = count + 1

Loop Types The two common types of loops you will use in python are the While loop For loop Which type you use depends on the particular situation

While Loop The while loop checks whether a condition is true or not. In this example “is count < 9” If this is true it executes the body of the loop If it is false it skips past the loop body to the next line of code count = 0 while (count < 9): print ('The count is: ', count ) count = count + 1

print ('The count is: ', count ) True False while count < 9 print ('The count is: ', count ) count = count + 1 End

CQ:Are these programs equivalent? def printCountNTimes(n): count = 0 while (count < n): print ('The count is: ', count ) count = count + 1 2 1 printCountNTimes(4) printCountNTimes(8) A: yes B: no

While Loop Dangers While loops do not require that the loop ever stop. In this example, count starts at 0 and gets smaller. Thus it will always be < 9 count = 0 while (count < 9): print ('The count is: ', count ) count = count - 1

Example Print every other character of a string stg = “hello there” ‘h’ pos = 0 ‘l’ while pos < len(stgr): ‘o’ print(stg[pos]) ‘t’ pos = pos+2 ‘e’ ‘e’

Our First For Loop # File: chaos.py # A simple program illustrating chaotic behavior def main(): print("This program illustrates a chaotic function") x = eval(input("Enter a number between 0 and 1:")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main()

Executing our First For Loop >>> This program illustrates a chaotic function Enter a number between 0 and 1: .5 0.975 0.0950625 0.335499922266 0.869464925259 0.442633109113 0.962165255337 0.141972779362 0.4750843862 0.972578927537 0.104009713267

Definite Loops In chaos.py, what did range(10) do? >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range is a built-in Python function that generates a sequence of numbers, starting with 0. list is a built-in Python function that turns the sequence into an explicit list (sequence) The body of the loop executes 10 times.

Ranges Python allows us to specify a range of values range(n) 0, 1, …, n-1 Example: list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Ranges Continued But what if we don’t want to start from 0? range(n) is short hand for range(0, n) Example: list(range(-4, 4)) [-4, -3, -2, -1, 0, 1, 2, 3]

Ranges Continued But what if I don’t want to count up by 1 Python allows us to “step” by a given integer range(start, end, step) Example: list(range(0, 10, 2)) [0, 2, 4, 6, 8]

Ranges … again But what if I want to count down? Python allows us to “step” by a given integer range(start, end, step) Lets try: list(range(0,10, -1)) [] Example that works: list(range(10, 0, -1)) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Python Boot Camp Introduction to Ranges Definite Loops Sequences Definite Loops For Loops Comparison of For and While loops

Definite Loops A definite loop executes a definite number of times, i.e., at the time Python starts the loop it knows exactly how many iterations to do. for <var> in <sequence>: <body> The beginning and end of the body are indicated by indentation. Sequences: ranges, lists, strings

Definite Loops for <var> in <sequence>: <body> The variable after the for is called the loop index. It takes on each successive value in sequence. You can reassign the index in the body, but this won’t change the loop execution: for k in range(5): print(k) k = k+3 print(“* “,k)

For (Definite) Loops These loops have a more controlled structure and cannot run infinitely This type of loop is commonly used with the range(x,y) function for x in range(0,10): print (x) for x in list(range(0,10)): print (x)

Definite Loops >>> for i in [0,1,2,3]: print (i) 1 2 3

Definite Loops >>> for i in range(0,4): print (i) 1 2 3

Definite Loops >>> for i in list(range(0,4)): print (i) 1 2 3

Definite Loops >>> for odd in [1, 3, 5, 7]: print(odd*odd) 1 9 25 49

Q:Are these programs equivalent? 1 2 for a in range(0, 10, 1): print(a) for a in range(10): print(a) A A: yes B: no

Definite Loops for loops alter the flow of program execution, so they are referred to as control structures. more items in <sequence> no yes <var> = next item <body>

More Complex Examples x = 0 for a in range(10): x = x + a print(x)

How to “visualize” a loop You can use print statements to figure out what is happening inside a loop This works for For loops and While loops What should we print? The loop index This will give us information of how the loop index changes as the loop executes Any calculations we may perform

More Complex Examples x = 0 for a in range(10): print(“a is: ”, a) x is: 0 a is: 1 x is: 1 a is: 2 x is: 3 a is: 3 x is: 6 a is: 4 x is: 10 a is: 5 x is: 15 a is: 6 x is: 21 a is: 7 x is: 28 a is: 8 x is: 36 a is: 9 x is: 45 x = 0 for a in range(10): print(“a is: ”, a) x = x + a print(“x is: ”, x) print(x)

More Complex Examples x = 1 for a in range(1,10): print(“a is: ”, a) x is: 1 a is: 2 x is: 2 a is: 3 x is: 6 a is: 4 x is: 24 a is: 5 x is: 120 a is: 6 x is: 720 a is: 7 x is: 5040 a is: 8 x is: 40320 a is: 9 x is: 362880 x = 1 for a in range(1,10): print(“a is: ”, a) x = x * a print(“x is: ”, x) print(x)

Q:Are these programs equivalent? A: Yes B: No 1 2 x = 0 y = 0 for k in range(5): x = x + k y = x + k print (y) x = 0 y = 0 for k in range(5): x = x + k y = x + k print (y) No

Loops can be nested def nested(a, b): for x in range(0, a): for y in range (0, b): print(x*y)

Visualizing a Nested Loop x is: 0 y is: 0 y is: 1 y is: 2 x is: 1 1 2 def nested(a, b): for x in range(0, a): print(“x is: ”, x) for y in range (0, b): print(“y is:”, y) print(x*y) Nested(2,3)

Q: Do these functions have the same output? def nested1(a,b): for x in range(0, a): for y in range (0, b): print(x*y) A: yes B: no def nested2(a,b): for y in range(0,b): for x in range (0, a): print(x*y) B

When might they be equivalent? What about when a=b? That is, we call the functions and provide the same values for a and b Output of nested1(2,2) = output of nested2(2,2)

Differences between While and For For loops provide a a finite and enumerated “range” of values This determines the “length” of the loop For loops explicitly rebind the loop index for X in … While loops express an execution condition It executes until that condition no longer holds

Q:Are these programs equivalent? 1 2 a = 0 while(a < 10): print(a) a = a+1 for a in range(10): print(a) A A: yes B: no