Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.

Slides:



Advertisements
Similar presentations
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?
Advertisements

Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
While loops.
Container Types in Python
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
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.
CS0004: Introduction to Programming Repetition – Do Loops.
Introduction to Computing Using Python Imperative Programming  Python Programs  Interactive Input/Output  One-Way and Two-Way if Statements  for Loops.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Lilian Blot TPOP Practical Assignment Optional, but you are strongly encourage to do it, especially if you are new to programming Available on Module.
Structured programming
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Guide to Programming with Python
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Programming for Linguists An Introduction to Python 24/11/2011.
Lists in Python.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
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.
For loops in programming Assumes you have seen assignment statements and print statements.
I Power Higher Computing Software Development High Level Language Constructs.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Functional Processing of Collections (Advanced) 6.0.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
ECE Application Programming
CSc 120 Introduction to Computer Programing II Adapted from slides by
Computer Programming Fundamentals
Python - Lists.
CS 100: Roadmap to Computing
Chapter 3 Instructor: Zhuojun Duan
Intro to Computer Science CS1510 Dr. Sarah Diesburg
While Loops in Python.
Topics Introduction to Repetition Structures
CS190/295 Programming in Python for Life Sciences: Lecture 5
Lists in Python.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
COS 260 DAY 10 Tony Gauvin.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
Intro to Computer Science CS1510 Dr. Sarah Diesburg
I210 review.
Coding Concepts (Basics)
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
CISC101 Reminders Assignment 2 due today.
Introduction to Computer Science
Topics Introduction to Repetition Structures
CS 100: Roadmap to Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
While Loops in Python.
Class code for pythonroom.com cchsp2cs
Introduction to Computer Science
Presentation transcript:

Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1

Lilian Blot What we have seen so Far What variables are Introduced the notion of Functions & Parameters Selection Structure (branching) How to do simple repetitions/iterations Autumn 2014 TPOP 2

Lilian Blot Overview How to represent a collection of values?  Tuples  Lists Mutable, Immutable object For Loops While Loops Autumn 2014 TPOP 3

Lilian Blot Data Types We have seen numbers  int: whole number  float: decimal point  boolean (True, False) String  str: “a word” What about a collection of data? Autumn 2014 TPOP 4

Lilian Blot text text TPOP String (str) A kind of collection of characters Autumn 2014 TPOP 5 >>> text = ‘TPOP’ >>> letter = text[2] # Accessing the third element >>> letter ‘O’ letter text[2]

Lilian Blot Tuples in Python Tuples are immutable objects, e.g. we CANNOT modify their contents. Autumn 2014 TPOP 6 >>> my_tuple = (1, 10, 4, 5) # tuple creation (,) >>> my_tuple[2] # Accessing the third element 4 >>> my_tuple (1, 10, 4, 5) >>> my_tuple[2] = 9 # Modifying the third element Traceback (most recent call last): File " ", line 1, in my_tuple [2] = 9 TypeError: 'tuple' object does not support item assignment >>> Code

Lilian Blot Tuples in Python Tuples are immutable objects, e.g. we CANNOT modify their contents. Note that numbers and strings are immutable too. Autumn 2014 TPOP 7 >>> my_t = (1, 10, 4, 5) # tuple creation (,) >>> my_t[1] 10 >>> my_t = my_t[:2] + (9,) + my_t[3:] # Modifying the third element >>> my_t (1, 10, 9, 5) Code

Lilian Blot Lists in Python Lists are mutable objects, e.g. we can modify their contents. Autumn 2014 TPOP 8 >>> my_list = [1, 10, 4, 5] # List creation [ ] >>> my_list[2] # Accessing the third element 4 >>> my_list [1, 10, 4, 5] >>> my_list[2] = 9 # Modifying the third element >>> my_list # The modified list [1, 10, 9, 5] >>> Code

Lilian Blot Be Careful Example: Autumn 2014 TPOP 9 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> lst1 [2, 4, 6, 8] >>> lst2 [2, 4, 6, 8] >>> t1 (1, 3, 7, 9) >>> t2 (1, 3, 7, 9) >>> Code

Lilian Blot Be Careful Example: Autumn 2014 TPOP 10 >>> t2 = t2[:2] + (5, ) + t2[3:] # Modifying the third element t2 >>> lst2[2] = 5 # Modifying the third element of lst2 >>> t1 (1, 3, 7, 9) >>> t2 (1, 3, 5, 9) >>> Code A change in t2 does not affect t1

Lilian Blot Be Careful Example: Autumn 2014 TPOP 11 >>> t2 = t2[:2] + (5, ) + t2[3:] # Modifying the third element t2 >>> lst2[2] = 5 # Modifying the third element of lst2 >>> t1 (1, 3, 7, 9) >>> t2 (1, 3, 5, 9) >>> lst1 [2, 4, 5, 8] >>> lst2 [2, 4, 5, 8] >>> Code A change in t2 does not affect t1 A change in lst2 does affect lst1

Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 12 >>> lst1 = lst2 = [2,4,6,8] >>> Code lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 13 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 14 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 15 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> lst2[2] = 5 >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 16 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> lst2[2] = 5 >>> >>> lst2 = [2, 4, 5, 8] >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 17 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> lst2[2] = 5 >>> >>> lst2 = [2, 4, 5, 8] >>> lst2[2] = 0 Code t1 t2 lst1 lst

Lilian Blot Swapping Values State Diagram: Autumn 2014 TPOP 18 >>> num_one = 5 >>> num_two = 9 >>> Code num_two num_one 5 9

Lilian Blot Swapping Values State Diagram: First Attempt Autumn 2014 TPOP 19 >>> num_one = 5 >>> num_two = 9 >>> num_one = num_two >>> num_two = num_one >>> Code num_two num_one 5 9

Lilian Blot Swapping Values State Diagram: Second Attempt Autumn 2014 TPOP 20 >>> num_one = 5 >>> num_two = 9 >>> temp = num_two >>> num_two = num_one >>> num_one = temp >>> Code num_two num_one temp 5 9

Lilian Blot Personal work Investigate what is meant by SLICING strings, lists and tuples. Autumn 2014 TPOP 21

Lilian Blot DEFINITE & INDEFINITE LOOPS Iteration Autumn 2014 TPOP 22

Lilian Blot Iteration We need a structure to execute a sequence of statements multiple times in succession How can we proceed if we are not sure how many times it needs to be repeated? Autumn 2014 TPOP 23

Lilian Blot Definite Loop: For statement The simplest kind of loop It will execute a definite amount of times The for loop statement for in :  Iterable can return its element one at a time  The body of the loop can be any sequence of Python statements  The variable after the keyword for is called the loop index. Autumn 2014 TPOP 24

Lilian Blot Example Autumn 2014 TPOP 25 print "Before the for loop" for val in [1,2,3]: square_val = val * val print "--> inside loop: square of", str(val), "is", str(square_val) print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 After the for loop Python shell

Lilian Blot Example Autumn 2014 TPOP 26 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" CodePython shell

Lilian Blot Example Autumn 2014 TPOP 27 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop Python shell 123 val

Lilian Blot Example Autumn 2014 TPOP 28 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop Python shell val square_val

Lilian Blot Example Autumn 2014 TPOP 29 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell val square_val

Lilian Blot Example Autumn 2014 TPOP 30 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell val square_val

Lilian Blot Example Autumn 2014 TPOP 31 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell 23 4 val square_val

Lilian Blot Example Autumn 2014 TPOP 32 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 Python shell 23 4 val square_val

Lilian Blot Example Autumn 2014 TPOP 33 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 Python shell 23 4 val square_val

Lilian Blot Example Autumn 2014 TPOP 34 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 Python shell 3 9 val square_val

Lilian Blot Example Autumn 2014 TPOP 35 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 Python shell 3 9 val square_val

Lilian Blot Example Autumn 2014 TPOP 36 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 After the for loop Python shell 3 9 val square_val

Lilian Blot Indefinite Loop: While Statement An indefinite loop keeps iterating until certain condition are met (conditional loop) There is no guarantee ahead of time regarding how many times the loop will go around  zero time  x-times  indefinitely Autumn 2014 TPOP 37

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 38 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 39 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 40 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 41 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 42 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 43 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 44 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 45 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 46 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 47 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 48 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 49 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 50 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 51 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 52 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : The usually contains statements that modify the evaluation of at some point Autumn 2014 TPOP 53 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : The usually contains statements that modify the evaluation of at some point The may never be executed! Autumn 2014 TPOP 54 False True

Lilian Blot General Form The while loop general form  Two new Keywords: 1. break 2. continue while : if : break if : continue Autumn 2014 TPOP 55 False True if : break if : continue

Lilian Blot General Form The while loop general form  Two new Keywords: 1. break 2. continue while : if : break if : continue  Test A is True Autumn 2014 TPOP 56 False True if : break if : continue

Lilian Blot General Form The while loop general form  Two new Keywords: 1. break 2. continue while : if : break if : continue  Test A is False & Test B is True Autumn 2014 TPOP 57 False True if : break if : continue

Lilian Blot General Form The for loop general form for in : if : break if : continue Autumn 2014 TPOP 58

Lilian Blot Summary We have seen mutable and immutable data type  consequences when modifying data Definite iteration  for loops Indefinite iteration (a.k.a conditional loop)  while loops General form using break and continue Autumn 2014 TPOP 59

Lilian Blot Next week Scope of variables and parameters Parameter passed by value or by reference  Understanding mutable and immutable object is essential Autumn 2014 TPOP 60

Lilian Blot Exercises Consider a problem where we want to record and store the marks of many students, each students having more than one mark (several modules with same credits).  what form the data structure would look like  how would you write a program to enter the marks of one student  how would you proceed to enter the marks of more than one students  how would you calculate the average mark of each student  what if modules don’t have the same credits (e.g. 10, 20, 30) Autumn 2014 TPOP 61

Lilian Blot Exercises Calculate the average of values in a list Calculate the average of values entered by a user, we don’t know how many values the user want to enter. The user should enter an empty value to signal the end of data input, then a result must be returned. Autumn 2014 TPOP 62