Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology
Course A201: Introduction to Programming 10/28/2010.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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.
Lists Introduction to Computing Science and Programming I.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Python Programming Chapter 8: Lists Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Python Programing: An Introduction to Computer Science
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
String and Lists Dr. José M. Reyes Álamo.
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists Part 1 Taken from notes by Dr. Neil Moore
Pass by Reference, const, readonly, struct
Lists in Python.
4. sequence data type Rocky K. C. Chang 16 September 2018
Lists A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are.
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Tuples.
Topics Sequences Lists Copying Lists Processing Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 4: Lists, Tuples and Dictionaries
15-110: Principles of Computing
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Lists/Tuples

Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next semester?

Lists Ordered set of values scores=[90.5, 73, 87] Each value accessed by using [ ] and index scores[1] #would be 73 A list can contain different types -- including other lists scores=[90.5, 73, 87, “Mickey”, [67, 89]] A list can be empty scores=[]

Example scores=[90.5, 73, 87] scores:

Subscripts Subscript describes which box of the array you are dealing with List of size N has subscripts –0, 1, 2, … (n-1) –list of size 3 – 0, 1, 2 Subscript can be a variable or expression which produces an integer –scores[i] –scores[i+5] If subscript specified does not exist – runtime error

Range Lists of consecutive integers common Use range() to generate –range(5) #0, 1, 2, 3, 4 –range(1,5) #1, 2, 3, 4 –range(1, 10, 2) #1, 3, 5, 7, 9

Accessing List Elements Print all elements of the list scores –Use only one print statement

Accessing List Elements Print all elements of the list scores –Use only one print statement scores = [90.5, 73, 82] i=0 while(i<3): print scores[i] i = i+1

Accessing List Elements Using 3 as length is error prone scores = [90.5, 73, 82] i=0 while(i<len(scores)): print scores[i] i = i+1

Accessing List Elements Another method -- short cut Membership scores[90.5, 73, 82] if 100 in scores: print “There is a perfect score!” scores = [90.5, 73, 82] for i in scores: print i

Operators -- A lot like strings Concatenation first_scores=[90,56,87] second_scores=[67,100,89] all_scores = first_scores+second_scores Slices all_scores[2:4] #yields [87,67] more_scores=all_scores[:] #makes a copy of all_scores

Mutability Lists are mutable - they can be changed scores[90.5, 73, 82] scores[1] = 100 #scores is now [90.5, 100, 82] Lists can grow - using append function –append takes 1 argument scores.append(12) scores.append([56, 78, 34]) Lists can shrink - using del function –del takes an element or slice del scores[2] del scores[1:3] Other functions - sort, reverse

Aliasing Reference assignment scores=[90.5, 73, 82] new_scores = scores scores[0] = 100 #new_scores[0] changes scores new_scores

Cloning scores=[90.5, 73, 82] new_scores = scores[:] scores[0] = 100 #new_scores[0] DOES NOT change scores new_scores

Exercises 1.Use the interpreter to help you determine the outcome of each of the following statements. Why are the invalid statements invalid? list1 = [1,2,3] list2 = list1 print list2 list2 = list1[2] print list2 list2.append(8) print list2 list2 = list1 list2.append(8) print list2 list1[9] = "hello" print list2 list1[2] = "hello" print list2

List Elements as Parameters Write a function to add two elements of a list

List Elements as Parameters Write a function to add two elements of a list –How is the function called? def adder(num1, num2): return (num1 + num2)

List Elements as Parameters Write a function to add two elements of a list –How is the function called? adder(scores[0], scores[1])

Passing Lists Would like to pass an entire list into a function –Sum all elements, prompt user for values

Example sum(scores) def sum(list): sum = 0 for i in list: sum += i return sum

Nested Lists exam_scores=[[90, 67, 34], [46, 99, 87], [89,78,67],[87,87,85]] #could represent three scores for four students Access second score of third student

Nested Lists exam_scores=[[90, 67, 34], [46, 99, 87], [89,78,67],[87,87,85]] #could represent three scores for four students Access second score of third student exam_scores[2][2]

Tuples Very similar to lists, but immutable –You can copy, but you cannot modify (append, del, assign new values) Typically enclosed in ( ) scores=(90.5, 73, 87) An integer - scores = (70) A tuple of one element - scores = (70,) Convert a list to a tuple tuple_scores = tuple(list_scores) Convert a tuple to a list - list_scores = list(tuple_scores)

Swapping Typical swap requires a temp variable tmp = b b = a a = tmp Tuple assignment a, b = b, a x, y, z = 4, 6, 8

Return Values def getnums() : n1 = input(“Enter number 1:”) n2 = input(“Enter number 2:”) return n1, n2 x, y = getnums() print x #prints first number entered print y #prints second number entered