Head First Python: Ch 1. Everyone loves lists Aug 22, 2013 Hee-gook Jun.

Slides:



Advertisements
Similar presentations
1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 16 Dictionaries 5/10/09 Python Mini-Course: Lesson 16 1.
CSE (c) S. Tanimoto, 2007 Python Introduction 1 Introduction to Python for Artificial Intelligence Outline: Why Python? Interactive programming,
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Lists in Python.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
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.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
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.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
RUBY by Ryan Chase.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Files Tutor: You will need ….
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Python Let’s get started!.
Python  Monty or Snake?. Monty?  Spam, spam, spam and eggs  Dead parrots  Eric Idle, John Cleese, Michael Palin, etc.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
Head First Python: Ch 3. Files and Exceptions: Dealing with Errors Aug 26, 2013 Kyung-Bin Lim.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Whatcha doin'? Aims: To start using Python. To understand loops.
CS1022 Computer Programming & Principles
Python: Experiencing IDLE, writing simple programs
Python Let’s get started!.
A team of well-educated clowns
Variables and Expressions
Pamela Moore & Zenia Bahorski
Python: Control Structures
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Functions CIS 40 – Introduction to Programming in Python
File Handling Programming Guides.
Organizing Memory in Java
An Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data Structures – 1D Lists
T. Jumana Abu Shmais – AOU - Riyadh
ARRAYS 1 GCSE COMPUTER SCIENCE.
CSC1018F: Intermediate Python
Variables and Expressions
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders All assignments are now posted.
CHAPTER 4: Lists, Tuples and Dictionaries
15-110: Principles of Computing
Class code for pythonroom.com cchsp2cs
Tuple.
Presentation transcript:

Head First Python: Ch 1. Everyone loves lists Aug 22, 2013 Hee-gook Jun

2 / 15 Outline  Create simple Python lists  Lists are like arrays  Add more data to your list  Work with your list data  For loops work with lists of any size  Store lists within lists  Check a list for a list  Complex data is hard to process  Handle many levels of nested lists  Don’t repeat code; create a function  Create a function in Python  Recursion to the rescue!  Your Python Toolbox

3 / 15 Create simple Python lists  1 Convert each of the names into strings by surrounding the data with quotes  2 Separate each of the list items from the next with a comma  3 Surround the list of items with opening and closing square brackets  4 Assign the list to an identifier (movies in the preceding code) using the assignment operator (=)  It does not need to declare data type in Python movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life"]

4 / 15 Lists are like arrays  Access list data using the square bracket notation print(movies[1])

5 / 15 Exercises cast = ["Cleese", 'Palin', 'Jones', "Idle"] print(cast) print(len(cast)) print(cast[1]) cast.append("Gilliam") # add a single data item print(cast) cast.pop() # remove data from the end of the list print(cast) cast.extend(["Gilliam", "Chapman"]) # add a collection of data items to the end of the list print(cast) cast.remove("Chapman") # remove a specific data item from the list print(cast) cast.insert(0, "Chapman") # add a data item before a specific slot location print(cast)

6 / 15 Add more data to your list  Python lists can contain data of mixed type ["The Holy Grail", 1975, "The Life of Brian", 1979, "The Meaning of Life", 1983] movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life"] movies.insert(1, 1975) movies.insert(3, 1979) movies.append(1983) movies = ["The Holy Grail", 1975,"The Life of Brian", 1979,"The Meaning of Life", 1983]

7 / 15 Work with your list data # When you use “while”, you have to worry about state information that requires you to employ a counting identifier count = 0 while count < len(movies): print(movies[count]) count = count+1 # When you use “for”, the Phthon interpreter worries about the state information for you for each_item in movies: print(each_item) # Indented code is called as “suite”

8 / 15 Store lists within lists  Lists can hold collections of anything including other lists movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"] ] print(movies[4][1][3]) # The “for” loop prints each item of the outer loop only for each_item in movies: print(each_item)

9 / 15 Check a list for a list  Each time you process an item in your list, you need to check to see if the item is another list – If the item is a list, you need to process the nested list before processing the next item in your outer list # Ask if “each_item” is a list for each_item in movies: if isinstance(each_item, list): for nested_item in each_item: print(nested_item) else: print(each_item) # But it cannot print nested list of nested list

10 / 15 Complex data is hard to process # Ask if “each_item” is a list for each_item in movies: if isinstance(each_item, list): for nested_item in each_item: print(nested_item) else: print(each_item) movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"] ]

11 / 15 Handle many levels of nested lists  The solution is to add more code to handle the additionally nested list for each_item in movies: if isinstance(each_item, list): for nested_item in each_item: if isinstance(nested_item, list): for deeper_item in nested_item: print(deeper_item) else: print(nested_item) else: print(each_item) # But overly complex code is rarely a good thing

12 / 15 Don’t repeat code; create function  This code contains a lot of repeated code for each_item in movies: if isinstance(each_item, list): for nested_item in each_item: if isinstance(nested_item, list): for deeper_item in nested_item: print(deeper_item) else: print(nested_item) else: print(each_item)

13 / 15 Create a function in Python  A Function in Python is a named suite of code, which can also take an optional list of arguments if required

14 / 15 Recursion to the rescue!  Take advantage of function and recursion – To solve the code complexity problems that had crept into the earlier list- processing code def print_lol(the_list): for each_item in the_list: if isinstance(each_item, list): print_lol(each_item) else: print(each_item) print_lol(movies)

15 / 15 Terminology  Python philosophy: “Batteries included” – A way of referring to the fact that Python comes with most everything you’ll need to get going quickly and productively – Python let you do most things well, without having to rely on code from third parties to get going – e.g.) Many BIFs: isinstance()..  BIF – a built-in function – print(), isinstance(),..  Suite – a block of Python code, which is indented to indicate grouping