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’

Slides:



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

Course A201: Introduction to Programming 10/28/2010.
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?
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.
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.
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.
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!
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
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.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
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.
Collections Michael Ernst CSE 190p University of Washington.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
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.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
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
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bryan Burlingame Halloween 2018
CS190/295 Programming in Python for Life Sciences: Lecture 6
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
Intro to Computer Science CS1510 Dr. Sarah Diesburg
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
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Python Review
Ruth Anderson UW CSE 160 Spring 2018
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
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
Introduction to Computer Science
Presentation transcript:

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’ ‘lists’ If it is a mathematical/scientific program we will care more about correctness. A math error in HW1 is more pardonable than one in HW2 If it is a game, the fun element is important. If it is a user interface (we’ll have one later) – make it look nice! Pay a ton of attention to function names and specs Style is hard to define but you know bad style when you see it Plagiarism is often detectable and will be dealt with harshly Do not post your question on stack overflow!

Agenda Lists The concept of side effect Be careful with list manipulations Tuples

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 Count – count the number of occurrences of an element

lists 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, zip Range basically produces a list range(1,10) 1 through 9 range(1,17,4) – in steps of 4 [1, 5, 9, 13] Just like range produces lists you can use the function zip for tuples >>> zip([1,2],[1,2]) [(1, 1), (2, 2)]

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

Sorted lists You can use the in-built ‘sorted’ function You can use ls.sort You can get fancy and use ls.sort with an argument First time we are passing a function as an arguments For writing a function that works as an argument to sort def sortingFunc(x, y): #return 0 when x and y are ‘equal’ # return -1 when x is ‘less’ than y # return 1 otherwise fancySort

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 and 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. Fine to do it for strings For lists you often actually want b = a[:] 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 things like union and intersection

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

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