CIT 590 Intro to Programming Lecture 5 – completing lists.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
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.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
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.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
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.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
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.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Containers and Lists CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson CSE 140 University of Washington
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists in Python.
Bryan Burlingame Halloween 2018
Data types Numeric types Sequence types float int bool list str
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Programming Lecture 3
String and Lists Dr. José M. Reyes Álamo.
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Basic String Operations
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Lists Copying Lists Processing Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Ruth Anderson CSE 160 University of Washington
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Review
Ruth Anderson UW CSE 160 Spring 2018
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Tuple.
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CIT 590 Intro to Programming Lecture 5 – completing lists

Textbook sections skipped Writing a function that calls itself (Recursion) – postponed till later Raising exceptions – postponed Please read your textbook and work through the examples by actually typing them up.

Agenda Lists The concept of side effect Be careful with list manipulations Strings Tuples Hoping to complete Chapter 4

lists ls = [1, 4, 5] Very similar to strings in terms of some of the basic operations Slicing is your best friend What is [2, 3, 4] + [7, 8, 9]? ls * 3 will repeat the list 3 times List functions Append – add elements Extend - add this other list at the end Index - find and return index Iterating over the elements of a list for listelement in lst: Python has no issue with mixed types >>> [1, 0.45, True, 'abc', [1,2,5]] [1, 0.45, True, 'abc', [1, 2, 5]]

List functions work by side effect ls.append() automatically changes the value of ls The function append actually returns the value ‘None’, so if you were to do ls = ls.append(5) or ls = ls.extend([5, 6, ‘t’]) Disaster! >>> lst = [45, '6', True] >>> lst.append(lst.append(576)) What will print lst give me now?

Fixed sized lists (arrays?) You can initialize a fixed length list by doing something like [None] * 10 [0] *5 Fixed length lists behave very similar to the concept of arrays found in other programming languages

Range Range basically produces a list range(1,10) 1 through 9 range(1,17,4) – in steps of 4 [1, 5, 9, 13] Xrange on the other hand, is a better thing to use when you are not interested in actually creating the list.

Assignment by reference as opposed to value(copy) A variable is always assigned by reference in Python If you’ve seen pointers in C, this is equivalent to that concept You can make a true copy by doing a complete slice Function arguments are also passed by reference Be very careful when doing this See sneakyAppend for an example

Identity and equality Use the identity testing operator ‘is’ if you want to ensure two variables hold references to the same object Use ‘==‘ operator if you want to ensure that the two variables have the same value, even if they are not the same object a is b implies a == b, but not necessarily the other way around

Tuples Defined using () Immutable - once defined I cannot change individual elements Similar to the list function there is a tuple function which makes a tuple out of a string or for that matter any list Very useful when you want to return multiple things from a function – getMaxAndMin.py Why not return things as a list? The mutability of a list makes it a somewhat dangerous datastructure

Strings are somewhat like lists but … Strings are immutable Lists are mutable If a is a list I can do things like a[0] = ‘hahaha’ When you do b = a for a list you have to careful String to list conversion list(‘computer science rocks!’) Stringifying a list ''.join(lst) Join is a powerful function that basically uses the string as a separator between elements of the list argument '||'.join(['45','56','56'])

Strings and cool string functions Split Join Both split and join will work essentially with any symbol for concatenation or separation Grab sentences using the ‘.’ separator

Sets They do not allow repeated elements set() Since sets are unordered collections, they do not support things like indexing However they do support most standard set operations – union, intersection, difference. Best way to eliminate dupes?

A few examples from the book Encryption (simpleEncryption.py)

Hands on example.. Most common starting letter for first names of students in this class Any guesses?