Computer Science 101 Lists in Python. The need for lists Often we need many different variables that play a similar role in the program Often we need.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Computer Science 101 Lists in Python. The need for lists Often we need many different variables that play a similar role in the program Often we need.
Roles of Variables with Examples in Scratch
One Dimensional Arrays
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Introduction to Computing Dr. Nadeem A Khan. Lecture
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
C++ for Engineers and Scientists Third Edition
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
A revision guide for programming with python. 1.A program is a set of instructions that tells a computer what to do. 2.The role of a programmer is to.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
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.
COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
Inputting and presenting data. “ I know what a spreadsheet is, I can enter simple data into a spreadsheet.” By the end of this session, I want you to.
Section 2.4: Simple Comparative Experiments. Experiment – A planned intervention undertaken to observe the effects of one or more explanatory variables,
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
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.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Python Programing: An Introduction to Computer Science
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
1-2 Scientific Inquiry How do scientists investigate the natural world? What role do models, theories, and laws play in science?
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Learning Objectives 1. Understand how the Small Basic Turtle operates. 2. Be able to draw geometric patterns using iteration.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Randomness Exploring Computer Science Lesson 4-10 – Part 2.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
The Accumulator Pattern Motivating Example: Suppose that you want to add up (“accumulate”) 1 2 plus 2 2 plus 3 2 plus … plus You could use: total.
Sections 10.1 – 10.4 Introduction to Arrays
Recap: If, elif, else If <True condition>:
Starter You have been asked to write a program to store the names of everybody in a class. Assuming there are 20 pupils in the class, how would you do.
Module 5 Working with Data
LESSON 13 – INTRO TO ARRAYS
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Lists in Python Creating lists.
Scientific Thinking and Processes Notes
Variables, Lists, and Objects
Computer Science G10 T \2017 T shaikhah AlZeyoudi
For Loops (Iteration 1) Programming Guides.
ICS103 Programming in C Lecture 12: Arrays I
GCSE Computing.
Tuple.
Introduction to Computer Science
Presentation transcript:

Computer Science 101 Lists in Python

The need for lists Often we need many different variables that play a similar role in the program Often we need many different variables that play a similar role in the program For example, suppose we have a temperature reading for each day of the year and need to manipulate these values several times within a program. For example, suppose we have a temperature reading for each day of the year and need to manipulate these values several times within a program. With simple variables, With simple variables, We would need to use 365 variables.We would need to use 365 variables. We would not be able to loop over these variables.We would not be able to loop over these variables.

Lists A list is a numbered collection of variables. A list is a numbered collection of variables. The variables in a list share the same name and are distinguished from one another by their numbers or subscripts. The variables in a list share the same name and are distinguished from one another by their numbers or subscripts. We can loop through the variables of a list by using a variable for the subscript. We can loop through the variables of a list by using a variable for the subscript. The subscripts are always 0,1,…, size-1 The subscripts are always 0,1,…, size-1

Referencing List Elements Suppose we have a list, temperature, for the temperature readings for the year. Suppose we have a list, temperature, for the temperature readings for the year. The subscripts would be 0,1,…,364. The subscripts would be 0,1,…,364. To refer to the reading for a given day, we use the name of the list with the subscript in brackets: temperature[4] for the fifth day. To refer to the reading for a given day, we use the name of the list with the subscript in brackets: temperature[4] for the fifth day temperature temperature[4]

Python Session

Find Largest Example

Random numbers For testing purposes or for simulations, it is convenient to let the computer generate random data for us. For testing purposes or for simulations, it is convenient to let the computer generate random data for us. In Python there is a library, random, that has a lot of tools for working with random numbers. In Python there is a library, random, that has a lot of tools for working with random numbers.

Randrange random.randrange random.randrange random.randrange(num) gives a random number in range 0,…,(num-1) random.randrange(num) gives a random number in range 0,…,(num-1) random.randrange(num1,num2) gives random number in range num1,…,(num2-1)random.randrange(num1,num2) gives random number in range num1,…,(num2-1)

Randrange

Appending to a List If we have a list, say scores, we can add a value, say num, to the list by scores.append(num) If we have a list, say scores, we can add a value, say num, to the list by scores.append(num)

Random Lists To build a random list of values, we can To build a random list of values, we can Start with an empty list, []Start with an empty list, [] Then append random values to the listThen append random values to the list

Find Largest again