Programming for Engineers in Python

Slides:



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

Container Types in Python
15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH.
Perkovic, Chapter 7 Functions revisited Python Namespaces
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Modular Programming With Functions
Dictionaries: Keeping track of pairs
Introduction to Computing Using Python Namespaces and Exceptions, revisited  Encapsulation in Functions  Global versus Local Namespaces  Exceptional.
Recitation 5 Programming for Engineers in Python.
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.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
Concepts when Python retrieves a variable’s value Namespaces – Namespaces store information about identifiers and the values to which they are bound –
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Outline 4.1 Introduction 4.2 Program Components in Python 4.3 Functions 4.4Module.
 2002 Prentice Hall. All rights reserved. 1 Functions Purpose –Building blocks for the program (separate functionality in independant parts) –Avoid code.
EGR 105 Foundations of Engineering I Session 3 Excel – Basics through Graphing Fall 2008.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
Writing Solid Code Introduction to Python. Program 1 python -c "print 'Hello World' “ python -c "import time; print time.asctime()“ Start an interactive.
Introduction to Computing Using Python Dictionary container class + modules  Container Class dict  Modules, revisited.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar.
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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.
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!
Built-in Data Structures in Python An Introduction.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Introduction to Python Aug 22, 2013 Hee-gook Jun.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
String and Lists Dr. José M. Reyes Álamo.
Chapter 6 - Functions modular programming general function format
G. Pullaiah College of Engineering and Technology
Chapter 6: User-Defined Functions I
Containers and Lists CIS 40 – Introduction to Programming in Python
CS 100: Roadmap to Computing
User-Defined Functions
MATLAB DENC 2533 ECADD LAB 9.
CISC101 Reminders Slides have changed from those posted last night…
Programming for Engineers in Python
String and Lists Dr. José M. Reyes Álamo.
Chapter 6: User-Defined Functions I
G. Pullaiah College of Engineering and Technology
Terminal-Based Programs
CS 100: Roadmap to Computing
Presentation transcript:

Programming for Engineers in Python Recitation 3

Plan Frequency Counter Modules / Packages Tuples Dictionaries Mutable / Imutable Dictionaries Functions: Scope Call by Ref / Call by Val Frequency Counter

Python Code Hierarchy Statement function Module Package

Modules All modules and their contents (functions, constants) can be found at http://docs.python.org/modindex.html >>> import math # mathematical functions >>> dir(math) ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] >>> math.pi # a constant 3.141592653589793 >>> math.sqrt(16) # a function 4.0 >>> math.log(8, 2) 3.0

Modules – Cont. >>> import os #operating system interfaces >>> os.rename(‘my_file.txt’, ‘your_file.txt’) >>> os.mkdir(‘new_dir’) >>> for root, dirs, files in os.walk('lib/email'): print root, "consumes", print sum(getsize(join(root, name)) for name in files), print "bytes in", len(files), "non-directory files" >>> os.times()[0] # user time 10.1244649

Packages NumPy support for large, multi-dimensional arrays and matrices high-level mathematical functions SciPy Optimization linear algebra integration special functions signal and image processing And more

Installing Packages Download NumPy: http://sourceforge.net/projects/numpy/files/NumPy/ Choose Download numpy-1.6.1-win32-superpack- python2.7.exe SciPy: http://sourceforge.net/projects/scipy/files/ Choose Download scipy-0.9.0-win32-superpack- python2.6.exe

Mutable vs. Immutable Can we change lists? >>> a_list = [1,2,3] >>> a_list[2] 3 >>> a_list[2] = ‘Inigo Montoya’ >>> a_list [1,2,’Inigo Montoya’] The assignment has mutated the list!

Mutable vs. Immutable Immutable! What about strings? >>> name = ‘Inigo Montoya’ >>> name[0] ' I' >>> name[5] ' ' >>> name[3] = ‘t’ Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> name[3] = 't' TypeError: 'str' object does not support item assignment Immutable!

Assignments to List Variables >>> orig_list = [1,2,3] >>> copy_list = orig_list >>> orig_list = [6,7,8,9] >>> copy_list [1,2,3] >>> orig_list [6,7,8,9] So far - no surprises

Assignments to List Variables >>> copy_list = orig_list >>> orig_list[0] = 1000 >>> orig_list [1000,7,8,9] >>> copy_list Surprise!

Assignments to List Variables List assignment orig_list = [6,7,8,9] creates: a list object, [6,7,8,9] a reference from the variable name, orig_list, to this object. Memory Memory orig_list [6,7,8,9]

Assignments to List Variables The assignment copy_list = orig_list does not create a new object, just a new variable name, copy_list, which now refers to the same object. Memory orig_list [6,7,8,9] copy_list

Assignments to List Variables Mutating list elements, orig_list[0] = 1000, does not create a new object and does not change existing references. Memory orig_list [1000,7,8,9] copy_list

Tuples A tuple is just like a list, only it is immutable. Syntax: note the parentheses! >>> t = (‘don’t’,’worry’,’be’,’happy’) # definition >>> t (‘dont’,’worry’,’be’,’happy’) >>> t[0] # indexing 'dont' >>> t[-1] # backwords indexing ‘worry' >>> t[1:3] # slicing (’worry’,’be’)

Tuples >>> t[0] = ‘do’ # try to change Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> t[0]=‘do’ TypeError: 'tuple' object does not support item assignment No append / extend / remove in Tuples!

Tuples So what are tuples good for? Faster than lists Safe code When immutable types are required (coming next).

Dictionaries A dictionary is a set of key-value pairs. >>> dict_name = {key1:val1, key2:val2,…} Keys are unique and immutable, values are not. Example: Map names to heights: >>> heights = {‘Alan Harper’:1.76, ‘John Smith’:1.83, ‘Walden Schmidt’:1.90} >>> heights {'Walden Schmidt': 1.9, 'John Smith': 1.83, 'Alan Harper': 1.76} Note:The pairs order changed

Dictionaries Solutions? Add a new person: >>> heights[‘Lyndsey Makelroy’] = 1.70 >>> heights {'Lyndsey Makelroy': 1.7, 'Walden Schmidt': 1.9, ‘John Smith': 1.83, 'Alan Harper': 1.76} What happens when the key already exists? >>> heights[‘John Smith’] = 2.02 {'Lyndsey Makelroy': 1.7, 'Walden Schmidt': 1.9, 'John Smith': 2.02, 'Alan Harper': 1.76} Solutions?

Dictionaries Idea: Add address to the key >>> heights = {[‘Alan Harper’, ‘Malibu’]:1.76} Traceback (most recent call last): File "<pyshell#46>", line 1, in <module> heights = {['Alan Harper', 'Malibu']:1.76} TypeError: unhashable type: 'list‘ What’s the problem? Fix: >>> heights = {(‘Alan Harper’, ‘Malibu’):1.76} >>> heights {('Alan Harper', 'Malibu'): 1.76}

Dictionaries Useful methods: D.get(k[,d]) - D[k] if k in D, else d (default – None). D.has_key(k) - True if D has a key k, else False D.items() - list of (key, value) pairs, as 2-tuples D.keys() - list of D's keys D.values() - list of D's values D.pop(k[,d]) - remove specified key and return the value. If key is not found, d is returned.

Functions – Scope Consider the following function, operating on two arguments: def linear_combination(x,y): y=2*y return (x+y) The formal parameters x and y are local, and their “life time" is just the execution of the function. They disappear when the function is returned. linear_combination x, y 3, 4 11

Functions – Scope >>> a=3 >>> b=4 >>> linear combination(a,b) 11 # this is the correct value >>> a 3 >>> b 4 # b has NOT changed The change in y is local - inside the body of the function.

Functions – Scope >>> x=3 >>> y=4 >>> linear_combination(x,y) 11 # this is the correct value >>> x 3 >>> y 4 The y in the calling environment and the y in the function are not the same variable!

Functions – Call by Val / Call by Ref Functions have arguments that appear in their prototype: def linear_combination(x,y): When you call a function, you must assign values to the function’s arguments: >>> linear_combination(3, 4) What happens when you pass variables? >>> linear_combination(a, b)

Functions – Call by Val / Call by Ref What is the difference? Run ta3_switch.py to find out!

Functions – Call by Val / Call by Ref What is the difference? Run ta3_add.py to find out!

Functions – Call by Val / Call by Ref There are two possibilities: Call by Value – pass a’s value to x and b’s value to y Call by Reference – pass a’s address to x and b’s address to y a, b A_function x = 3 y = 4 a=3 b=4 a, b Another_function x y a=[1,2] b=[3, 4]

Functions – Call by Reference In Python – Call by reference A function can mutate its parameters. When the address does not change, the mutations effect the original caller's environment. Remember: Assignment changes the address! That explains why y=2*y is not visible in the caller's environment.

Functions – Call by Reference def increment(lst): for i in range(len(lst)): lst[i] = lst[i] +1 # no value returned Now let us execute it in the following manner >>> list1=[0,1,2,3] >>> increment(list1) >>> list1 [1, 2, 3, 4] # list1 has changed! lst was mutated inside the body of increment(lst). Such change occurs only for mutable objects.

Functions – Call by Reference Consider the following function: def nullify(lst): lst=[ ] # no value returned Now let us execute it in the following manner >>> list1=[0,1,2,3] >>> nullify(list1) >>> list1 [0, 1, 2, 3] # list1 has NOT changed! Why?

Functions - Information Flow To conclude, we saw two ways of passing information from a function back to its caller: Using return value(s) Mutating a mutable parameter

s u p e r c a l i f r a g i l i s t i c e x p i a l i d o c i o u s Frequency Counter Assume you want to learn about the frequencies of English letters. You find a long and representative text and start counting. Which data structure will you use to keep your findings? s u p e r c a l i f r a g i l i s t i c e x p i a l i d o c i o u s

Frequency Counter str1 = 'supercalifragilisticexpialidocious‘ # count letters charCount = {} for char in str1: charCount[char] = charCount.get(char, 0) + 1 # sort alphabetically sortedCharTuples = sorted(charCount.items())

Frequency Counter # print for charTuple in sortedCharTuples: print charTuple[0] , ‘ = ‘, charTuple[1] a = 3 c = 3 d = 1 e = 2 f = 1 g = 1 …

Simulation – Guess Cards צורה שם עברי שם אנגלי צבע ♠ עלה/פיק Spades שחור ♥ לב Hearts אדום ♦ יהלום/מעוין Diamonds ♣ תלתן Clubs

Simulation – Reminder In class: Simulated a rare disease by: Generating its distribution in the entire population, assume we received 2% Generating a sample of 1000 people. For every person – “toss a coin” with 2% chance of “heads” (have the disease) and 98% of “tails” (don’t have the disease) Check whether the number of sick people is significantly higher then expected (20 people) Repeat for 100 diseases

Simulation - Cards Scenario: Generate 25 playing cards. Guess which series every card belongs to. One card - there is an equal chance that the card belongs to each of the four series, hence - 25% chance for being correct. Two cards – 16 options with equal chance  6.25% chance n cards – 4n options, 1/ 4n chance of being correct in all guesses. For 25 cards the chance of being always correct drops to almost 0.

Simulation - Comparison Cards Diseases Role 1000 people 100 diseases Number of Guesses 25 cards Number of Observations 25% per series Generated per each disease Rate (Expectation)

Exercise - Dice Odds In backgammon, there are 36 combinations of rolling the dice. We are interested in the sum of the two dice. Therefore: count the number of combinations for every possible sum find the chance of getting it.  

Exercise - Dice Odds Create the following data structure: Each combination of two dice is a tuple. All combinations are held in a dictionary, where the key is the sum of the combination. Each value in the dictionary is a list of tuples – that is, all combinations that sum to key Keys Values 2 [ (1 , 1) ] 3 [ (1 , 2) , (2, 1) ] 4 [ (1 , 3) , (2 , 2) , (3 , 1) ]

Exercise - Dice Odds rolls = {} Loop with d1 from 1 to 6 The general outline is: rolls = {} Loop with d1 from 1 to 6 Loop with d2 from 1 to 6 newTuple ← ( d1, d2 ) # create the tuple oldList ← dictionary entry for d1+d2 newList ← oldList + newTuple update dictionary entry Loop over all keys in the dictionary print key, length of the list Let’s Play!

Exercise - Dice Odds rolls = {} for d1 in range(1, 7): # first dice for d2 in range(1, 7): # second dice t = (d1, d2) key = d1+d2 val = rolls.get(key, []) # update list val.append(t) rolls[key] = val for key in rolls.keys(): # print result print key, len( rolls[key] )

Exercise - Dice Odds >>> 2 1 3 2 4 3 5 4 6 5 7 6 8 5 9 4 10 3 11 2 12 1