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.
JavaScript Arrays Arrays allow us to store lists of items Arrays are created with square brackets:  var a = []; // an empty array  var b = [10]; // an.
Understanding Randomness
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.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
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.
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.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Lists in Python.
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.
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.
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.
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.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Bell Ringers Calculate the mean median and mode for the following sets of data. 1.15, 16, 19, 6, 16, 17, 19 Mean: Median: Mode: 2. 68, 74, 20, 45, 96,
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
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.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g.
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.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Learning Objectives 1. Understand how the Small Basic Turtle operates. 2. Be able to draw geometric patterns using iteration.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Randomness Exploring Computer Science Lesson 4-10 – Part 2.
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
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
Microsoft Visual Basic 2005: Reloaded Second Edition
Flooding © 2018.
LESSON 13 – INTRO TO ARRAYS
Data Structures – 1D Lists
Lists in Python Creating lists.
Scientific Thinking and Processes Notes
Variables, Lists, and Objects
Computer Science G10 T \2017 T shaikhah AlZeyoudi
Arrays Part 2.
ICS103 Programming in C Lecture 12: Arrays I
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

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)

… and all the children are above average

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

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 Example