Chapter 5: Lists and Dictionaries

Slides:



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

Container Types in Python
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.
Guide to Programming with Python
Fundamentals of Python: From First Programs Through Data Structures
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
INTRODUCTION TO DATA STRUCTURE. Topics To Be Discussed………………………. Meaning of Data Structure Classification of Data Structure Data Structure Operations.
Computer Science 111 Fundamentals of Programming I Sequences: Lists.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
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.
October 17, 2005ICP: Chapter 5: Lists and Dictionaries 1 Introduction to Computer Programming Chapter 5: Lists and Dictionaries Michael Scherger Department.
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 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Numbers, lists and tuples Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
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.
Built-in Data Structures in Python An Introduction.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Lists. The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
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.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
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
Python Programing: An Introduction to Computer Science
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.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
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.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Data types: Complex types (List)
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
From Think Python How to Think Like a Computer Scientist
Lecture 10 Data Collections
Chapter 8 Arrays Objectives
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Bryan Burlingame Halloween 2018
Creation, Traversal, Insertion and Removal
Guide to Programming with Python
Chapter 8 Arrays Objectives
6. Lists Let's Learn Python and Pygame
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
Python Data Structures
Object Oriented Programming in java
CS 1111 Introduction to Programming Fall 2018
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
Microsoft Visual Basic 2005: Reloaded Second Edition
CS 1111 Introduction to Programming Spring 2019
Chapter 8 Arrays Objectives
Topics Sequences Lists Copying Lists Processing Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Lists.
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Topics Basic String Operations String Slicing
For loop Using lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
COMPUTER SCIENCE PRESENTATION.
Topics Basic String Operations String Slicing
Python List.
Presentation transcript:

Chapter 5: Lists and Dictionaries C10061 – Introduction to Computer Programming Kent State University Chapter 5: Lists and Dictionaries

Chapter Topics Covered Lists vs. tuples List mutability List creation, lengths and operators List indexing and slicing New features of lists List methods Chapter 5: Lists and Dictionaries

Lists Lists are another type of sequence. Similar to tuples and do everything tuples do Mutable (changeable) Use functions to find lengths Use operators to determine conditions Use indexing Use slicing Use methods for manipulation Chapter 5: Lists and Dictionaries

Lists Creating a list is the same as creating a tuple, only the elements are enclosed in [ ]. Example: empty_list = [] Example: string_list = [“value1”, “value2”] number_list = [2.5,50] combo_list = [7,”string”] Chapter 5: Lists and Dictionaries

Lists Use len(list_name) to find how many elements exist in the list. Use the in operator to check for conditions. Use indexing for sequential or random access. Use slicing with beginning and ending points. Chapter 5: Lists and Dictionaries

Lists: New Features Assigning a new value to an element Note: Not applicable for creating new elements in the list (will generate an IndexError). Example: current_list = [“One”,Two”,“Three”] current_list[1] = “Zero” Result: ['One', 'Zero', 'Three'] Chapter 5: Lists and Dictionaries

Lists: New Features Assigning a new value to a slice Note: Single list replaces a two-element slice. The number of elements decreases by 1. Example: current_list = [“One”,“Two”,“Three”,”Four”] current_list[1:3] = “Zero” Result: ['One', 'Zero', 'Three'] Chapter 5: Lists and Dictionaries

Lists: New Features Deleting a list element using the del command Note: The gap created by a deletion is filled automatically; all elements shift left one position. Example: current_list = [“One”,“Two”,“Three”,”Four”] del current_list[0] Result: [‘Two', ‘Three', ‘Four'] Chapter 5: Lists and Dictionaries

Lists: New Features Deleting a slice using the del command Note: The gap created by a deletion is filled automatically; all elements shift left one position. Example: current_list = [“One”,“Two”,“Three”,”Four”] del current_list[:3] Result: ['Four'] Chapter 5: Lists and Dictionaries

Lists: append( ) Function Add a new element to the end of the list Example: current_list = [“One”,“Two”,“Three”,”Four”] new_element = “Five” current_list.append(new_element) Result: ['One', 'Two', 'Three', 'Four', 'Five'] Chapter 5: Lists and Dictionaries

Lists: remove( ) Function Remove the first occurrence of a value Example: current_list = [“One”,“Two”,“Two”,”Four”] remove_element = “Two” if remove_element in current_list: current_list.remove(remove_element) Result: ['One', 'Two', 'Four'] Chapter 5: Lists and Dictionaries

Lists: sort( ) Function Sorts the elements of the list in ascending order Example: current_list = [“One”,“Two”,“Three”,”Four”] current_list.sort( ) Result: ['Four', 'One', 'Three', 'Two'] Chapter 5: Lists and Dictionaries

Lists: reverse( ) Function Sorts the elements of the list in descending order Example: current_list = [“One”,“Two”,“Three”,”Four”] current_list.reverse( ) Result: ['Four', 'Three', 'Two', 'One'] Chapter 5: Lists and Dictionaries

Lists: count( ) Function Returns the number of occurrences of a specific value Example: current_list = [“One”,“Two”,“Two”,”Four”] search = “Two” number = current_list.count(search) print number Result: 2 Chapter 5: Lists and Dictionaries

Lists: index( ) Function Returns the first position number of where a specific value occurs Example: current_list = [“One”,“Two”,“Two”,”Four”] search = “Two” number = current_list.index(search) print number Result: 1 Chapter 5: Lists and Dictionaries

Lists: insert( ) Function Inserts a specific value at a specific position Example: current_list = [“One”,“Two”,“Two”,”Four”] new = “Three” current_list.insert(2,new) Result: ['One', 'Two', 'Three', 'Two', 'Four'] Chapter 5: Lists and Dictionaries

Lists: pop([i]) Function Returns a value at a specific position (i) and removes the value from the list Example: current_list = [“One”,“Two”,“Two”,”Four”] new = current_list.pop(1) print new print current_list Result: Two ['One', 'Two', 'Four'] Chapter 5: Lists and Dictionaries

Lists: pop( ) Function from the list Returns and removes the last element from the list Example: current_list = [“One”,“Two”,“Two”,”Four”] last = current_list.pop( ) print last print current_list Result: Four ['One', 'Two', ‘Two'] Chapter 5: Lists and Dictionaries