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.

Slides:



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

Tutorial #6 PseudoCode (reprise)
Flow Charting, Structured English and PseudoCode
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Computer Science 111 Fundamentals of Programming I Iteration with the for Loop.
Programming Training Main Points: - Working with Functions in Python
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
PYTHON PROGRAMMING Week 10 – Wednesday. TERMS – CHAPTER 1 Write down definitions for these terms:  Computation  Computability  Computing  Artificial.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Fibonacci Numbers Damian Gordon. Fibonacci Numbers.
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Searching Damian Gordon. Google PageRank Damian Gordon.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
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.
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
Introduction to Python Damian Gordon. The Python Programming Language Python was developed by Guido van Rossum in the Netherlands in Van Rossum.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways.
For Loops CMSC 201. Overview Today we will learn about: For loops.
GCSE Computing: Programming GCSE Programming Remembering Python.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
Python: Linked Lists and Recursion Damian Gordon.
Data Structures: Multi-Dimensional Arrays Damian Gordon.
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
Recursion Damian Gordon. Recursion Factorial Fibonacci Decimal to Binary conversion Travelling Salesman Problem Knight’s Tour.
Introduction to Python Damian Gordon e:
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
Selection: CASE Statement Damian Gordon. Selection As well as the IF Statement, another form of SELECTION is the CASE statement.
Learning outcomes 5 Developing Code – Using Flowcharts
Introduction to Python
Python: Recursion Damian Gordon.
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Determinate Loops with the
Boolean Logic Damian Gordon.
Python: Array Slicing Damian Gordon.
Count Controlled Loops (Nested)
Python: Algorithms Damian Gordon.
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Problems with repetitions.
Simple Statistics on Arrays
Simple Statistics on Arrays
Some Common Issues: Iteration
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
For Loops (Iteration 1) Programming Guides.
Sorting Damian Gordon.
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.
Python: Sorting – Selection Sort
Introduction to Computer Science
Print the following triangle, using nested loops
Control Operations Basic operations are input, output, arithmetic, etc. Control operations allow for sequencing other operations Choose between several.
Some Common Issues: Common Algorithms
CSE 231 Lab 2.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
PseudoCode Damian Gordon.
Presentation transcript:

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 loop

Python: WHILE loop Damian Gordon

Python: WHILE loop The WHILE loop works as follows: while CONDITION: STATEMENTS

Python: WHILE loop But we’ll do: while CONDITION: # DO STATEMENTS # ENDWHILE;

Python: WHILE loop Let’s print out the numbers 1 to 5:

# PROGRAM Print1To5: a = 1 while a != 6: # DO print(a) a = a + 1 # ENDWHILE; # END.

Python: WHILE loop Let’s print the sum of the numbers 1 to 5:

# PROGRAM Sum1To5: a = 1 total = 0 while a != 6: # DO total = total + a a = a + 1 # ENDWHILE; print(total) # END.

Python: WHILE loop Let’s do factorial:

Python: WHILE loop Let’s do factorial: – Remember: – 5! = 5*4*3*2*1 – 7! = 7*6 *5*4*3*2*1 – N! = N*(N-1)*(N-2)*…*2*1

# PROGRAM Factorial: value = int(input("Please input value:")) total = 1 while value != 0: # DO total = total * value value = value - 1 # ENDWHILE; print(total) # END.

Python: FOR loop Damian Gordon

Python: WHILE loop The FOR loop works as follows: for RANGE: STATEMENTS

Python: WHILE loop But we’ll do: for RANGE: # DO STATEMENTS # ENDFOR;

Python: FOR loop Let’s remember the program to print out the numbers 1 to 5:

# PROGRAM Print1To5: a = 1 while a != 6: # DO print(a) a = a + 1 # ENDWHILE; # END.

Python: FOR loop We can do it as follows as well:

# PROGRAM Print1To5For: for a in range(1,6): # DO print(a) # ENDFOR; # END.

Python: DO loop Damian Gordon

Python: DO loop Python doesn’t implement the DO loop. 

Python: DO loop But a WHILE loop is OK to do the same thing.

Python: LOOP loop Damian Gordon

Python: LOOP loop Python doesn’t implement the LOOP loop. 

Python: LOOP loop But it does have a BREAK statement, so we can create our own LOOP loop:

Python: LOOP loop x = 1 while x == 1: # DO if CONDITION: # THEN break # ENDIF; # ENDWHILE;

etc.