Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

Container Types in Python
Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
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.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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.
Sequences The range function returns a sequence
Sequences A sequence is a list of elements Lists and tuples
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
Python Crash Course Containers 3 rd year Bachelors V1.0 dd Hour 3.
Python Data Structures
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
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.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Built-in Data Structures in Python An Introduction.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Lists CS303E: Elements of Computers and Programming.
Tuples Chapter 10 Python for Informatics: Exploring Information
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
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.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Magic 8 ball. "Design and write a python program that emulates a Magic Eight Ball. Your program should continually prompt the user to enter a question.
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 Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
String and Lists Dr. José M. Reyes Álamo.
Fundamentals of Programming II Overview of Collections
Lecture 3 Python Basics Part 2.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
Lecture 10 Data Collections
LING 388: Computers and Language
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
Python Data Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python for Informatics: Exploring Information
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
15-110: Principles of Computing
For loop Using lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Stacks.
Sample lecture slides.
Dictionary.
Python List.
Python - Tuples.
Tuple.
Introduction to Computer Science
Presentation transcript:

Python Data Structures By Greg Felber

Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the same list List notation – A = [1,”This is a list”, c, Donkey(“kong”)]

Methods of Lists List.append(x) – adds an item to the end of the list List.extend(L) – Extend the list by appending all in the given list L List.insert(I,x) – Inserts an item at index I List.remove(x) – Removes the first item from the list whose value is x

Examples of other methods a = [66.25, 333, 333, 1, ] //Defines List – print a.count(333), a.count(66.25), a.count('x') //calls method – //output a.index(333) – //Returns the first index where the given value appears – 1 //ouput a.reverse()//Reverses order of list – a//Prints list a – [333, , 1, 333, -1, 66.25]//Ouput a.sort() – a//Prints list a – [-1, 1, 66.25, 333, 333, ]//Output

Using Lists as Stacks The last element added is the first element retrieved To add an item to the stack, append() must be used – stack = [3, 4, 5] – stack.append(6) – Stack is now [3, 4, 5, 6] To retrieve an item from the top of the stack, pop must be used – Stack.pop() – 6 is output – Stack is now [3, 4, 5] again

Using Lists as Queues First element added is the first element retrieved To do this collections.deque must be implemented

List Programming Tools Filter(function, sequence) – Returns a sequence consisting of the items from the sequence for which function(item) is true – Computes primes up to 25

Map Function Map(function, sequence) – Calls function(item) for each of the sequence’s items – Computes the cube for the range of 1 to 11

Reduce Function Reduce(function, sequence) – Returns a single value constructed by calling the binary function (function) – Computes the sum of the numbers 1 to 10

The del statement A specific index or range can be deleted

Tuples Tuple – A number of values separated by commas – Immutable Cannot assign values to individual items of a tuple However tuples can contain mutable objects such as lists – Single items must be defined using a comma Singleton = ‘hello’,

Sets An unordered collection with no duplicate elements Basket = [‘apple’, ‘orange’, ‘apple’, ‘pear’] Fruit = set(basket) Fruit – Set([‘orange’, ‘apple’, ‘pear’])

Dictionaries Indexed by keys – This can be any immutable type (strings, numbers…) – Tuples can be used if they contain only immutable objects

Looping Techniques Iteritems(): – for retrieving key and values through a dictionary

Looping Techniques Enumerate(): – for the position index and values in a sequence

Zip(): – for looping over two or more sequences

Comparisons Operators “in” and “not in” can be used to see if an item exists in a sequence Comparisons can be chained – a < b == c This tests whether a is less than b and that b equals c