CS105 STRING LIST TUPLE DICTIONARY. Characteristics of Sequence What is sequence data type? It stores several objects Each object has an order Each object.

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology
Advertisements

Course A201: Introduction to Programming 10/28/2010.
Container Types in Python
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
1 Python Chapter 3 Reading strings and printing. © Samuel Marateck.
 1 String and Data Processing. Sequential Processing Processing each element in a sequence for e in [1,2,3,4]: print e for c in “hello”: print c for.
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 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.
Python for Informatics: Exploring Information
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.
If statements while loop for loop
Built-in Data Structures in Python An Introduction.
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.
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.
9/28/2015BCHB Edwards Basic Python Review BCHB Lecture 8.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Notes on Python Regular Expressions and parser generators (by D. Parson) These are the Python supplements to the author’s slides for Chapter 1 and Section.
GE3M25: Computer Programming for Biologists Python, Class 5
Tuples Chapter 10 Python for Informatics: Exploring Information
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python - 2 Jim Eng Overview Lists Dictionaries Try... except Methods and Functions Classes and Objects Midterm Review.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
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.
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
CS106 Mid-term Quiz Not counted in your grade.. Q1 Write code to create a variable message that refers to a string Hello world. Answer: message = “Hello.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing 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.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
String and Lists Dr. José M. Reyes Álamo.
Lecture 3 Python Basics Part 2.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Generating Random Numbers
Notes on Python Regular Expressions and parser generators (by D
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
CMPT 120 Topic: Python strings.
Section 6: Sequences Chapter 4 and 5.
Bryan Burlingame Halloween 2018
Ruth Anderson University of Washington CSE 160 Spring 2018
CHAPTER THREE Sequences.
Python - Strings.
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
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Topics Sequences Introduction to Lists List Slicing
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Nate Brunelle Today: Dictionaries
CS1110 Today: collections.
Topics Basic String Operations String Slicing
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Topics Basic String Operations String Slicing
CSE 231 Lab 6.
Sample lecture slides.
CMPT 120 Topic: Python strings.
Topics Basic String Operations String Slicing
Python List.
Introduction to PYTHON
Presentation transcript:

CS105 STRING LIST TUPLE DICTIONARY

Characteristics of Sequence What is sequence data type? It stores several objects Each object has an order Each object can be referred by an index Data types in sequence List Tuple String

Operations in sequence Indexinge.g., s[i] Slicinge.g., s[1:5] Concatenatione.g., s + t Repetitione.g., s * 5 Membership teste.g., ‘a’ in s Lengthe.g., len(s)  IR ML CS

String Characteristics of sequence data types Definition of string Modification of string Formatting String methods String modules

String definition One line string Use ’ ’ or ” ” Multiple line string Use ’’’ ’’’ or ””” ””” Escape sequence Use backspace

Modification of string Not possible to modify it directly Substitute with a new name >>> s = ‘spam and egg’ >>> s = s[:5] + ‘cheese ‘ + s[5:] >>> s ‘spam cheese and egg’

Formatting of string Separate format field and data E.g., >>> S = ‘spam and egg’ >>> names = [‘H. Cho’, ‘Bora Cho’] >>> for name in names: print ‘Hi, %s’ % name Hi, H. Cho Hi, Bora Cho

Methods in string upper() lower() capitalize() count(s) find(s) rfind(s) index(s)

Methods in string (cont.) strip(), lstrip(), rstrip() replace(a, b) expandtabs() split() join() center(), ljust(), rjust()

Formatting of string Separate format field and data E.g., >>> S = ‘spam and egg’ >>> name = [‘H. Cho’, ‘Bora Cho’] >>> for name in names: print ‘Hi, %s’ % name Hi, H. Cho Hi, Bora Cho

List Operations Nested lists Methods List comprehension Range Examples

Operations in List Indexinge.g., L[i] Slicinge.g., L[1:5] Concatenatione.g., L + L Repetitione.g., L * 5 Membership teste.g., ‘a’ in L Lengthe.g., len(L)  IR ML CS

Nested List List in a list E.g., >>> s = [1,2,3] >>> t = [‘begin’, s, ‘end’] >>> t [‘begin’, [1, 2, 3], ‘end’] >>> t[1][1] 2

Methods in List append insert index count sort reverse remove pop extend

List Comprehension Function to create list E.g., >>> L = [k * k for k in range(10)] >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> L = [] >>> for k in range(10): L.append(k*k) >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

List Comprehension (cont.) E.g., >>> [(i, j, i*j) for i in range (2,100,2) for j in range(3,100,3) if (i + j) % 7 == 0]

Tuple Operations in Tuple List vs. Tuple

Operations in Tuple How to make a tuple? E.g., >>> t = () >>> t = (1, 2, 3) >>> t = 1, 2, 3 >>> t = (1, ) >>> t = 1,

Operations in Tuple Indexinge.g., T[i] Slicinge.g., T[1:5] Concatenatione.g., T + T Repetitione.g., T * 5 Membership teste.g., ‘a’ in T Lengthe.g., len(T)  IR ML CS

List vs. Tuple What are common characteristic? Both store arbitrary data objects Both are of sequence data type What are differences? Tuple doesn’t allow modification Tuple doesn’t have methods Tuple supports format strings Tuple supports variable length parameter in function call.

Dictionary Operations in Dictionary Methods in Dictionary Symbol table

Dictionary What is dictionary? Refer value through key E.g., >>> member = {‘basketball’:5, ‘soccer’:11, ‘baseball’:9} >>> member[‘baseball’] 9

Operations in Dictionary >>> member[‘volleyball’] = 7 >>> member[‘volleyball’] = 6 >>> member {‘soccer’:11, ‘volleyball’:6, ‘basketball’:5, ‘baseball’:9} >>> len(member) 4 >>> del member[‘basketball’]

Methods in Dictionary keys() values() items() has_key(key) clear() copy() get(key[,x]) setdefault(key[,x]) update(D) popitem()

Symbol table (represented in Dictionary) What is symbol table? A table that stores a value corresponding to a symbol Global / local symbol table Globals() Locals() Symbol table of objects __dict__ Example of dictionary Example of function