Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
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.
Lists Introduction to Computing Science and Programming I.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Introduction to Computing Using Python Regular expressions Suppose we need to find all addresses in a web page How do we recognize addresses?
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
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.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
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.
Lists CS303E: Elements of Computers and Programming.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
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.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Hossain Shahriar Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19.
Lists Michael Ernst CSE 140 University of Washington.
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.
Lists Ruth Anderson University of Washington CSE 160 Winter
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
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.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
String and Lists Dr. José M. Reyes Álamo.
Strings, Characters and Regular Expressions
Containers and Lists CIS 40 – Introduction to Programming in Python
More on Lists.
TMF1414 Introduction to Programming
Data Structures: Lists
Ruth Anderson University of Washington CSE 160 Spring 2015
File Access (7.5) CSE 2031 Fall July 2018.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Ruth Anderson University of Washington CSE 160 Spring 2018
Ruth Anderson University of Washington CSE 160 Winter 2017
8 – Lists and tuples John R. Woodward.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Fundamentals of Python: First Programs
Topics Sequences Lists Copying Lists Processing Lists
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Topics Sequences Introduction to Lists List Slicing
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
COMPUTER SCIENCE PRESENTATION.
Introduction to Computer Science
Presentation transcript:

Hossain Shahriar

Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s) List File Regular expression

List A flexible data structure that can store one or more items L = [1, 2, 3] Some useful methods of list list.append(x): Add an item to the end of the list list.insert(i, x): Insert an item at a given position list.remove(x): Remove the first item whose value is x. It is an error if there is no such item (use with exception)

List (cont.) list.pop([i]): Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. list.index(x): Return the index in the list of the first item whose value is x. list.count(x): Return the number of times x appears in the list. list.sort(): Sort the items of the list, in place. list.reverse(): Reverse the elements of a list.

List -operators (cont.) Slice operator allows to choose a subset from a list L = [1, 2, 3, 4, 5, 6] Slice [ :3] = [4, 5, 6] #select elements from 0 to (3-1) Slice [3 :3] = [ ] #select elements from 3 to (3-1) Slice [2 : ] = [3, 4, 5, 6] #select elements from 2 to (6-1) Concatenating two lists (+) L1= [1, 2, 3], L2= [4, 5, 6] L = L1+ L2 = [1, 2, 3, 4, 5, 6] Multiplication (*) L1= [1, 2, 3] L2 = L1*2 = [1, 2, 3, 1, 2, 3]

List -operators (cont.) del operator deletes an element L = [1, 3, 5, 7] del L[1] = [1, 5, 7] in tests whether an element is in a list or not Returns true or false L = [1, 3, 5, 7] 3 in L = true 3 not in L = false # not operator

File Stores data permanently How to read a file? Open a file Read data by each line Close the file Do these steps in try-exception blocks def fileread(name): try: f = open(name, "r") lines = f.readlines() f.close() for line in lines: print line except IOError: raise ValueError("Filename does not exist, or cannot be read.") fileread("C:/Users/shahriar/Desktop/CISC101/test.txt")

File How to write a file? Open a file Write data Close the file Do the steps in try-exception blocks def filewrite(name): try: f = open(name, “w") # this will erase any previous content f.write(‘hello world') f.close() except IOError: raise ValueError("Filen cannot be open for writing") fileread("C:/Users/shahriar/Desktop/CISC101/test.txt")

File More one opening mode f = open(name, “w") 'r‘: file will be opened for read only 'w‘: file will be opened for writing only and it will erase all the contents if a file already exists 'a‘: fill will be opened for appending; any data written to the file is automatically added to the end

Regular expression Very useful for string data processing Tokenization First, we import regular expression library re We need to know how to write regular expression Match any sequence of alpha numeric characters– ‘\w+’ str = “I know that you do not know” Mylist = re.findall (‘\w+’, str) ['I', 'know', 'that', 'you', 'do', 'not', 'know'] We have just performed string tokenization!! Each element of the list is a token that we obtained from str We could have obtained Mylist using str.split() too!

Regular expression \d – identifies all numbers in a string and return in a list How can we recognize numbers in a string? -- \d+ numlist = re.findall ('\d+', "i am 19 years old ") print numlist >>> [19] Take home assignment Can you find two more examples of meta characters and show examples how they provide outputs?

Regular expression \d: Matches any decimal digit; [0-9]. \D: Matches any non-digit character; [^0-9]. \s: Matches any whitespace character; [ \t\n\r\f\v]. \S: Matches any non-whitespace character; [^ \t\n\r\f\v]. \w: Matches any alphanumeric character; [a-zA-Z0-9_]. \W: Matches any non-alphanumeric character; [^a-zA-Z0- 9_]. + : One or more character *: zero or more character

Regular expression match(): Determine if the RE matches at the beginning of the string. search(): Scan through a string, looking for any location where this RE matches. findall(): Find all substrings where the RE matches, and returns them as a list.