ENGR/CS 101 CS Session Lecture 12

Similar documents
ENGR/CS 101 CS Session Lecture 15

CS1 Lecture 11 Feb. 9, 2018

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

University of Texas at Arlington, TX, USA

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

The Practice of Computing Using PYTHON

Collections. Lists, Tuples, Sets, Dictionaries

ENGR/CS 101 CS Session Lecture 4

Python Lists and for Loops. Learning Outcomes. What s a List 9/19/2012

Objectives. Structure. Munster Programming Training

ENGR/CS 101 CS Session Lecture 9

Data Structures. Lists, Tuples, Sets, Dictionaries

Python Review IPRE

Using Lists (Arrays) Notes

Level 3 Computing Year 2 Lecturer: Phil Smith

Python Review IPRE

S206E Lecture 19, 5/24/2016, Python an overview

Python Programming Exercises 3

CMSC 201 Spring 2019 Lab 06 Lists

Python for Non-programmers

Spring INF Principles of Programming for Informatics. Manipulating Lists

Introduction to Programming

ENGR/CS 101 CS Session Lecture 13

Lists How lists are like strings

Topic 7: Lists, Dictionaries and Strings

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

Python Intro GIS Week 1. Jake K. Carr

Vocabulary Size of Moby Dick

CS1 Lecture 12 Feb. 11, 2019

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

Lecture 4. while and for loops if else test Tuples Functions. Let us start Python Ssh (putty) to UNIX/Linux computer puccini.che.pitt.

Algorithmic Thinking: Computing with Lists

CMSC201 Computer Science I for Majors

Introduction to programming using Python

ENGR/CS 101 CS Session Lecture 3

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

TUPLES AND RECURSIVE LISTS 5

CS 234 Python Review Part 2

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Types, lists & functions

Sequence Types FEB

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

LECTURE 3 Python Basics Part 2

Introduction to Problem Solving and Programming in Python.

More Examples Using Lists Tuples and Dictionaries in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #11

CMSC201 Computer Science I for Majors

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lists. Ruth Anderson University of Washington CSE 160 Spring 2015

CMSC201 Computer Science I for Majors

Lists. Ruth Anderson University of Washington CSE 160 Winter 2017

Chapter 1. Data types. Data types. In this chapter you will: learn about data types. learn about tuples, lists and dictionaries

Lecture: Functional Programming

Algorithmic Thinking: Computing with Lists

Algorithmic Approaches for Biological Data, Lecture #15

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

CMSC201 Computer Science I for Majors

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

Programming Languages

Iteration and For Loops

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

ENGR/CS 101 CS Session Lecture 5

CS 102 Lab 3 Fall 2012

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

CS1110 Lab 6 (Mar 17-18, 2015)

Lists. Lists Element Types. Creating Lists. CS 112 Lists Part II 2/22/09. Dan Fleck Spring 2009 George Mason University

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

18.1. CS 102 Unit 18. Python. Mark Redekopp

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

Computational Programming with Python

MEIN 50010: Python Data Structures

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

Introduction to: Computers & Programming: Strings and Other Sequences

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

Python Lists. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 5, 2011

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Introduction to Python! Lecture 2

John Perry. Fall 2009

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Compiler principles, PS1

Structure and Interpretation of Computer Programs

Programming for Engineers in Python. Recitation 2

Python debugging and beautification

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

More Counting Sort and Sorting-by-Key

Scripting Languages Course 1. Diana Trandabăț

CS-141 Final Exam Review May 16, 2015 Presented by the RIT Computer Science Community

Introduction to Python

Python: common syntax

Try and Error. Python debugging and beautification

CSC Software I: Utilities and Internals. Programming in Python

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012

ECE242 Data Structures and Algorithms Fall 2008

LING/C SC/PSYC 438/538. Lecture 20 Sandiway Fong

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

Slicing. Open pizza_slicer.py

Introduction to: Computers & Programming: Strings and Other Sequences

Transcription:

ENGR/CS 101 CS Session Lecture 12 Log into Windows/ACENET (reboot if in Linux) Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f14-courses/cs101.html Right-click on lecture12.py link. Save link/target to folder where your other CS 101 programs are. Right-click on numbers.dat link. Save link/target to same folder. Start Python, open lecture12.py file Lecture 12 ENGR/CS 101 Computer Science Session 1

Outline Questions about Homework 2? Arrays Python lists Problem: Finding the range of a collection of numbers Finding the maximum value Finding the minimum value Lecture 12 ENGR/CS 101 Computer Science Session 2

Storing Collections Last class we discussed searching and sorting algorithms. Today we will discuss how the data storage might be arranged. First, we want the data to be stored in a collection that is arranged to allow us easy access to each element. Also, we want to be able to identify an element of the collection by using the position (or index) of the element in the collection. Lecture 12 ENGR/CS 101 Computer Science Session 3

Arrays The simplest collection in most programming languages is the array. Formally, an array is an ordered collection of homogeneous elements that is accessed by an index. Lecture 12 ENGR/CS 101 Computer Science Session 4

Arrays Ordered means that the relative position of each element matters. Homogeneous means all elements are of the same type Indexed means that we identify positions using an integer. Lecture 12 ENGR/CS 101 Computer Science Session 5

Arrays To access an array element, you give the array name and the index of the element. Almost all programming languages (including Python) use: <name>[<index>] This is a compound name that can be used on either side of an assignment. Lecture 12 ENGR/CS 101 Computer Science Session 6

Arrays The indexes of an array usually start at 0 or 1. For technical reasons, most modern programming languages (including Python) start indexing at 0. Here is a picture of an array of 10 integers: anarray 23 45 76 39 5 87 16 92 54 63 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Lecture 12 ENGR/CS 101 Computer Science Session 7

Python Lists In Python, the basic collection is called a list. Like an array, a Python list is ordered and indexed. Unlike an array, the elements are heterogeneous. I.e., they may be of different types. Lecture 12 ENGR/CS 101 Computer Science Session 8

Python Lists A list is written as comma-delimited values enclosed in square brackets. A list with no elements is called the empty list. >>> mylist = [3, "cat", 4.5, True] >>> mylist [3, "cat", 4.5, True] >>> mylist[2] 4.5 >>> mylist[2] = 6.5 >>> mylist [3, "cat", 6.5, True] Lecture 12 ENGR/CS 101 Computer Science Session 9

Python Lists Python lists have a number of built-in operations including +, concatenate two lists together *, concatenate a repeated number of times len(), function that returns the number of elements in the list [<startidx>:<endidx>], slicing operation for extracting a part of a list starting at index <startidx> and ending at index <endidx>-1. Lecture 12 ENGR/CS 101 Computer Science Session 10

Python Lists Here are some examples >>> mylist+mylist [1, 'cat', 6.5, True, 1, 'cat', 6.5, True] >>> mylist*3 [1, 'cat', 6.5, True, 1, 'cat', 6.5, True, 1, 'cat', 6.5, True] >>> len(mylist) 4 >>> len (mylist*4) 16 >>> mylist[1:2] ['cat'] Lecture 12 ENGR/CS 101 Computer Science Session 11

Python Lists It turns out that Python also has built-in ways of searching a list (in operation) and sorting a list (sort method). >>> mylist = [12, 4, 63, 36, 28] >>> 36 in mylist True >>> mylist.sort() >>> mylist [4, 12, 28, 36, 63] Lecture 12 ENGR/CS 101 Computer Science Session 12

Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values are. The simplest measure is to find the range of the data values, which is the difference between the maximum and minimum values of the collection. We would like a program that given a file of numbers, computes and displays the range of the numbers stored in the file. Lecture 12 ENGR/CS 101 Computer Science Session 13

Main Program Design Design 1. Create an empty list 2. Ask the user for the name of a data file 3. Open the file for reading 4. For each line in the input file a. Convert the line to an integer b. Append the integer to the list 5. Compute and display the range of the data in the list Lecture 12 ENGR/CS 101 Computer Science Session 14

In-class Program lecture12.py is the start of program we will use to show how to compute various statistical quantities on a collection of random numbers. The provided code handles asking the user for a file name, opening the file and accessing each line. (Steps 2-4) The pass statement is used as a placeholder for code bodies. This allows the file to be compiled. Lecture 12 ENGR/CS 101 Computer Science Session 15

Creating a Python List In order to compute statistics on the data, we need to store the data into a list. First we create an empty list. items = [] Then in the loop, we convert each line into an integer number = int(line) And we append each number to the list items.append(number) Lecture 12 ENGR/CS 101 Computer Science Session 16

Finding the Maximum Value The first thing we need is to find the maximum value in the list. We will write a function that does this. Analysis Received: list to be searched Returned: maximum value Lecture 12 ENGR/CS 101 Computer Science Session 17

Finding the Maximum Value The basic idea is to go through the list keeping track of the current maximum value. Start by assuming the first element is the maximum value. Use a for-loop and an if-statement to check the rest of the elements. Lecture 12 ENGR/CS 101 Computer Science Session 18

findmax Function Code def findmax (alist): # assume first item is max maxsofar = alist[0] # look at the rest of the items for item in alist[1:]: if item > maxsofar: # a new max maxsofar = item return maxsofar Lecture 12 ENGR/CS 101 Computer Science Session 19

In-class Exercise In the main program, write a function call to findmax using the items list and display the result. Run the program and make sure you are getting the correct answer, then delete this code. Write a function findmin that finds and returns the minimum value in a received list. Hint: how does this differ from finding the maximum value? Lecture 12 ENGR/CS 101 Computer Science Session 20

In-class Exercise In the main program, write a function call to findmin using the items list and display the result. Again, make sure you are getting the correct answer, then delete the code. Write a function computerange that receives a list, computes the range of the values using the findmax and findmin functions, and returns the range of the values. Lecture 12 ENGR/CS 101 Computer Science Session 21

In-class Exercise Finish the main program by calling computerange and displaying the range of the data values. Lecture 12 ENGR/CS 101 Computer Science Session 22