COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
One Dimensional Arrays
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.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
Intro to Robots Lists in Python. Intro to Robots What is a List? An ordered set of values: –Ordered: 1 st, 2 nd, 3 rd, … –Values: can be anything, integers,
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Guide to Programming with Python
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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
I Power Int 2 Computing Software Development High Level Language Constructs.
Built-in Data Structures in Python An Introduction.
Last Week if statement print statement input builtin function strings and methods for loop.
PYTHON LISTS. What are lists? An ordered set of elements A variable with 0 or more things inside of it Examples myList = [8, 6, 7, 5, 3, 0, 9] strList.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
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.
Python Programing: An Introduction to Computer Science
Week 6 - Friday.  What did we talk about last time?  Loop examples.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
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 Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Introduction to Strings
Introduction to Strings
Bryan Burlingame 03 October 2018
Lists in Python.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
15-110: Principles of Computing
CHAPTER 4: Lists, Tuples and Dictionaries
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Introduction to Strings
Introduction to Computer Science
Introduction to Computer Science
2-Dimensional Lists (Matrices) in Python
Introduction to Computer Science
Introduction to Strings
Python Open source Many applications Python 3 jupyter notebook
Python List.
Tuple.
Introduction to Computer Science
Presentation transcript:

COMPUTER SCIENCE FEBRUARY 2011 Lists in Python

Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings Think of a CD box  The box : list  CDs : elements  Fixed size  CDs stored in order

Why Use Lists? Faster than defining variables for each element Can perform operations on each element  Think of Robot game: had to copy/paste code for each item More flexible  Easy to add/remove elements Can pass an entire list to a function

Lists in Python Most languages: elements must be of same data type Python: can mix data types  Integers, floats (decimals), string, even other lists.

Creating Lists & Getting Length Create by specifying elements int_list=[202,10, 54, 23] print(int_list)..or create an empty list empty_list=[] print(empty_list) Use len() to return the number of elements print len(int_list) print len(empty_list)

Accessing elements Access individual elements using brackets First element is 0. int_list=[202,10, 54, 23] int_list[1] int_list[3] Access from end using negative indices int_list[-1] int_list[-3]

Specifying slices Access a slice (sub-list) using ranges Includes first element in range, excludes last in range int_list=[202,10, 54, 23] int_list[0:1] int_list[2:3] int _list[0:0] Note: this is different from int _list[0] Omit values to get start or end of list int_list[:2] int_list[2:]

Using a while loop Using for value in list int_list=[202,10, 54, 23] index=0 sum_values=0 while (index<len(int_list)): value = int_list[index] sum_values+=value index+=1 for value in int_list: sum_values+=value Iterate through lists

Manipulating lists Lists are mutable (changeable), unlike strings. Replace a single element int_list=[202,10, 54, 23] int_list[1]=11 Replace a slice int_list[1:3]=[12,99] Delete by replacing a slice with an empty list int_list[1:3]=[] Add an element by ‘squeezing’ it in int_list[4:4]=100 int_list[ ] = 20

Nested Lists Lists can contain other lists int_list=[202,10, 54, 23] mixed_list=[“a”, int_list, 23] List length is number of ‘top level’ elements print len(mixed_list)