Python Lists and Such CS 4320, SPRING 2015. List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
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
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.
Python Dictionary.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
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.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
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.
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.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
If statements while loop for loop
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.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
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.
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.
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.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
String and Lists Dr. José M. Reyes Álamo.
Python Variable Types.
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER THREE Sequences.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Sequences Introduction to Lists List Slicing
(more) Python.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 4: Lists, Tuples and Dictionaries
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Lists [] First = [1, 2, 3, 4] Second = list[range(1,5)]
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionary.
Introduction to Computer Science
Presentation transcript:

Python Lists and Such CS 4320, SPRING 2015

List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds x to the end of list s s.pop(i) removes and returns the element at index I ◦ s.pop() is the same as s.pop(-1) x in s and x not in s test for containment del s[i:j:k] removes a slice (or a single element) s.insert(i,x) inserts x at index i 1/7/2015CS 4320, SPRING

Command Line Parameters When a program is run from the command line, parameters can be added. Parameters can also be provided when running in PyCharm (most IDE’s support this feature as well) The parameters are made available in a list named sys.argv To use the arguments, include the line import sys at the beginning of the script The values of the arguments are of type string The parameter at index 0 is always the path to the script 1/7/2015CS 4320, SPRING

Examples Get the list of values from the command line Apply the various functions and print the results 1/7/2015CS 4320, SPRING

Tuples Tuples are immutable lists As literals, tuples are given by a comma separated list of values surrounded by parentheses ◦(1,2,3) Beware! ◦(1) is not a tuple, it is simply the integer 1 ◦(1,) is a tuple with one element, the integer 1 Tuples turn up in some places, especially as the return values from some functions. ◦Just be aware that tuples cannot be modified 1/7/2015CS 4320, SPRING

Dictionaries Also known as ‘hash maps’, ‘associative memory’, and ‘maps’ A dictionary associates keys to values A literal dictionary uses curly braces with pairs ‘key:value’ Indexing retrieves values for a given key 1/7/2015CS 4320, SPRING

Examples Create some dictionaries and access values Demonstrate what happens when access is attempted with a key not present 1/7/2015CS 4320, SPRING

Dictionary Functions d.get(key) gets the value associated with key d.get(key,y) gets the value associated with key, returns y if there is none key in d returns true if key is in dictionary d d.keys() returns an ‘iterable’ of the keys in d (will use with loops) del d[key] deletes the key from the dictionary 1/7/2015CS 4320, SPRING

Examples Use the various functions and display the results 1/7/2015CS 4320, SPRING

Comprehensions A syntactical shortcut for creating lists from other lists [ expr for var in list ] This can be extended with more ‘for’ clauses Filters can be added with ‘if’ clauses 1/7/2015CS 4320, SPRING

Examples Create some list comprehensions Create a list from a range by applying a simple expression Modify the comprehension to select based on remainder modulo something Create a list of pairs (x,y) where x divides y evenly 1/7/2015CS 4320, SPRING