2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.

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:
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Sequences The range function returns a sequence
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,
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
Lists in Python.
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.
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.
Introduction to Python September 26, /10/ Bioinformatics Languages Low-level, compiled languages: C, C++, Java… Pros: performance Cons:
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.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
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.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
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.
Lists CS303E: Elements of Computers and Programming.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
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.
Lists Michael Ernst CSE 140 University of Washington.
Lists Ruth Anderson University of Washington CSE 160 Winter
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
String and Lists Dr. José M. Reyes Álamo.
Containers and Lists CIS 40 – Introduction to Programming in Python
Ruth Anderson University of Washington CSE 160 Spring 2015
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Introduction to Strings
Introduction to Strings
Bryan Burlingame Halloween 2018
Ruth Anderson University of Washington CSE 160 Spring 2018
Introduction to Python
Ruth Anderson University of Washington CSE 160 Winter 2017
8 – Lists and tuples John R. Woodward.
String and Lists Dr. José M. Reyes Álamo.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Introduction to Strings
CS1110 Today: collections.
15-110: Principles of Computing
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Lecture 7: Python’s Built-in Types and Basic Statements
Topics Basic String Operations String Slicing
CSE 231 Lab 6.
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
COMPUTER SCIENCE PRESENTATION.
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

2/28/2008

>>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file

>>> Arrays in Python Python has a data type known as a list. For our purposes, lists are arrays. Declaration syntax: = [,,, …, ] = [ ] * Example:numbers = [12, 49, -2, 26, 5, 17, -6] zeros = [0] * 10 Indexing: Lists have zero based indexing from front Negative Indexing: You can also refer to an element by a negative index representing how far it is from the end. Example: index from front numbers = [ 13, 25, 39, 46, 54, 68] index from back

>>> Methods for Lists Basic Methods – directly modify the lists list.append(item) – appends the item to the end of the list list.insert(index, item) – inserts the item at the specified index list.remove(item) – removes the first occurrence of item from the list list.extend(second_list) – appends second_list to the list Mathematical Operators – behave as you would expect them to (+) Returns a new list by adding two lists. Appends the right-hand list to the left-hand list. (+=) Appends the right-hand list to the left-hand list. Modifies left list. Acts like extend() (*) Multiplies a list and an integer “n”. Returns a new list that has n-1 versions of original list appended to it Examples: list = [34, 21, 29, 86, 29] list.append(3) => [34, 21, 29, 86, 29, 3] list.insert(2, 3) => [34, 21, 3, 29, 86] list2 = [1, 2, 3, 4] list.remove(29) => [34, 21, 86, 29] list.extend(list2) => [34, 21, 86, 29, 1, 2, 3, 4] [0] * 5 => [0, 0, 0, 0, 0]

>>> More Methods More Methods list.count(element) – returns number of times element occurs in the list list.sort – sorts the element in place list.reverse – reverses the element in place Slicing – can get a sub list of a list [ : ] list = [4, 23, 16, 7, 29, 56, 81] list[3:6] => [16, 7, 29] Length of lists len(list) => 7 Split – returns a list “lets try some splitting here”.split(“ “)=> ['lets', 'try', 'some', 'splitting', 'here']

>>> Printing Lists There are two ways to print lists. list1 = [“elements”, “of”, “our”, “list”] list2 = [21, 29, 86, 19, 42] String concatenation and type conversion: print “This list is ” + str(list1)=> This list is [“elements”, “of”, “our”, “list”] print “This list is ” + str(list2)=> This list is [21, 29, 86, 19, 42] Comma separated arguments in the print method: print “This list is”, list1 => This list is [“elements”, “of”, “our”, “list”] print “This list is”, list2 => This list is [21, 29, 86, 19, 42]

>>> Ranges are Lists Recall how we used the method range() in for loops. Calling range returns a list with the patterns specified by range(). Example: range(5) =>[0, 1, 2, 3, 4] range(0, 10, 2) => [0, 2, 4, 6, 8] Using a for loop iterates over each element in a list. Example:Example 2:list = [3, 6, 5, 7, 15] for i in list:for i in range(len(list)) print ilist[i] = list[i] + 1

>>> Strings vs. Lists Although Strings are different from lists, Strings can be accessed like lists. Example: s = “Hello!” s[1] => ‘e’ s[-1] => ‘!’ s[1:5] => “ello” s.count(“l”)=> 2 Once a String has been created, it cannot be changed. Methods that alter a list cannot be called on Strings. Note: Python does not distinguish between characters and strings. Characters are just Strings of length 1.

>>> Tuples vs. Lists Additionally, tuples can be accessed like lists. However, tuples are not list. Tuples, like strings cannot be changed once they have been created. Example: s = (123, 456, 789, 246, 357) s[1] => 456 s[-1] => 357 s[1:4] => (456, 789, 246)

>>> Random with lists >>> from random import * >>> randint(0,9)‏ 1 >>> randint(0,9)‏ 4 >>> choice(range(10))‏ 7 random.randint(a,b)‏ returns an int between a and b inclusive random.choice(seq)‏ returns a random element of the sequence ‏

>>> Review - Files Opening files: open(filename) ~ defaults to read open(filename, "r") ~ specifies read open(filename, "w") ~ writes to this file File objects: (we won't really have to use these)‏ *.readlines () ~ file as a list of lines *.read () ~ file as a string *.readline (e) ~ next line as string filename = "imdb.txt“ f1 = open(filename)‏ for line in f1: print line.upper()‏ f1.close() f2 = open(filename, “w”) f2.write(“This will over write the file \n”) f2.close()‏ imdb.py

>>> Sections Example in Python Let’s solve the Sections problem. We want to take the following line from a file: …… And turn it into: Sections attended: [9, 6, 7, 4, 3] Sections scores: [20, 18, 20, 12, 9] Sections grades: [100.0, 90.0, 100.0, 60.0, 45.0] Sections attended: [6, 7, 5, 6, 4] Sections scores: [18, 20, 15, 18, 12] Sections grades: [90.0, 100.0, 75.0, 90.0, 60.0] Sections attended: [5, 6, 5, 7, 6] Sections scores: [15, 18, 15, 20, 18] Sections grades: [75.0, 90.0, 75.0, 100.0, 90.0]

>>> Map-Reduce: Map Python supports functional programming. Functional programming differs from what we have been doing, by treating programming as the evaluation of a series of mathematical functions. Map() and reduce() are functional language methods. They return a new list instead of modifying the one passed. Map – takes a function and a list and applies the function to each individual element in the list. def add_one(x) return x + 1 list = [0, 2, 4, 6, 8] new_list = map(add_one, list) Looking at our new list: new_list => [1, 3, 5, 7, 9]

>>> Map-Reduce: Reduce Reduce – takes a function and a list and reduces the list to a single element by combining the element using the given function. def multiply(x, y) return x * y list = [2, 4, 6, 8, 10] value = reduce(multiply, list) Looking at our value: value =>3840

>>> Sections Example - Map Let’s use the functional method map() to modify our Sections Example.

>>> Lambda Lambda is a keyword that designates an “anonymous function”. This is a lot of terminology, but lets see how we can use it. Instead of defining a method, and then applying it using map(): def add_one(x) return x + 1 list = [0, 2, 4, 6, 8] map(add_one, list) We can do it all in one line using lambda and anonymous functions: map(lambda x : x + 1, list) Lets use lambda to further simplify our Sections Example.