Announcements
description
Transcript of Announcements
![Page 1: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/1.jpg)
AnnouncementsSurvey link (can be found on piazza):
https://purdue.qualtrics.com/SE/?SID=SV_3f2sMjxvGiPOCJ6
Project 1 assigned next weekDetails discussed in recitation next week
Class / Recitation slides available on the syllabus pageCheck morning of for latest version
Python is like a foreign language
![Page 2: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/2.jpg)
Python Boot Camp!OutputVariablesAssignment Statements
Multiple AssignmentInputBasic Python Types Revisited
![Page 3: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/3.jpg)
Programs are Interactive
input
program
output
![Page 4: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/4.jpg)
OutputOutput Statements
• A print statement can print any number of expressions.
• Successive print statements will display on separate lines.
• printing a variable prints its value• A bare print will print a blank line.
• print()
![Page 5: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/5.jpg)
Clicker Question:Are these programs
equivalent?
print(Hello) print(“Hello”)21
A: yes
B: no
![Page 6: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/6.jpg)
What does this do in Python?
Hello = “Hello”
Now the two programs would be equivalentHello is a variable that stores the value: “Hello”
![Page 7: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/7.jpg)
Assignment StatementsSimple Assignment<variable> = <expr>
variable is an identifier, expr is an expressionThe expression on the RHS is evaluated to
produce a value which is then associated with the variable named on the LHS.
![Page 8: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/8.jpg)
VariablesJust as in algebra, variables are names used to
represent values (numbers, strings, etc)Variables are assigned values using the =
operator in pythonVariable names can be comprised of numbers
and alphabetic characters but must start with a character
a = 4myPicture = “barbara.jpg”
![Page 9: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/9.jpg)
What cannot be a variable• Some identifiers are part of Python itself. These
identifiers are known as reserved words. This means they are not available for you to use as a name for a variable, etc. in your program.
• and, def, for, is, raise, assert, elif, in, print, etc.• For a complete list, see table 2.1
![Page 10: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/10.jpg)
Clicker Question• Which variable name is not valid?1. a2. seven3. 4a4. _4
![Page 11: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/11.jpg)
Programming TipUse meaningful names!
This makes your program much easier to “read”Example:
Foo vs. X vs. R vs. Radius
![Page 12: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/12.jpg)
Differences from Mathematics
y = 3x = 9x = y + 6
Viewed as a system of equations
![Page 13: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/13.jpg)
.. But in Pythony = 3x = 9x = y + 6
Separate statements executed one after another (“control flow”)
Each statement assigns a value to a variable
![Page 14: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/14.jpg)
Differences from Mathematics
But what about …x = 5x = x + 6
![Page 15: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/15.jpg)
AssignmentExpression on the right hand side of the = is
evaluated first x + 6The result is “stored” in the variable on the left
hand side of the = x = 11
![Page 16: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/16.jpg)
Variable StorageVariables are stored in memory.Initially memory is blank
(sort of)
![Page 17: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/17.jpg)
Variable StorageAssigning a variable
associates a new ‘name’ with a new ‘value’ 4a
a = 4
![Page 18: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/18.jpg)
Variable StorageReassigning a variable
updates it’s value10a
a = 4a = 10
![Page 19: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/19.jpg)
Variable StorageSome special variable types
occupy more than one space in memory 10a
Sbally
a = 10b = “Sally”
![Page 20: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/20.jpg)
Assignment (writes)We can view an assignment as a “write” to
memoryThe variable represents a “container” in
memoryAssignment places, or “writes” the value into
that container
![Page 21: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/21.jpg)
Uses (Reads)A use of a variable retrieves the value from the
container10a
a + 610 + 6
![Page 22: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/22.jpg)
Problem Revisited
x = 5x = x + 6
5x
x = 5 + 6
The result is that thevalue 11 is stored into the variable x
![Page 23: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/23.jpg)
Assigning InputThe purpose of an input statement is to get
input from the user and store it into a variable.<variable> = eval(input(<prompt>))
![Page 24: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/24.jpg)
Assigning InputFirst the prompt is printedThe input part waits for the user to enter a
value and press <enter>The expression that was entered is
evaluated to turn it from a string of characters into a Python value (a number).
The value is assigned to the variable.
![Page 25: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/25.jpg)
Simultaneous Assignmentsum, diff = x+y, x-yHow could you use this to swap the values for x
and y?• Why does this not work?
x = yy = x
We could use a temporary variable…
![Page 26: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/26.jpg)
Temporary variable• Swapping x and y with a temporary>>>z = x
>>>x = y
>>>y = z
This is inefficient We need an extra “box” of memory to do this
![Page 27: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/27.jpg)
Simultaneous AssignmentWe can swap the values of two variables quite
easily in Python!x, y = y, x>>> x = 3>>> y = 4>>> print x, y3 4>>> x, y = y, x>>> print x, y4 3
![Page 28: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/28.jpg)
Simultaneous AssignmentWe can use this same idea to input multiple
variables from a single input statement!Use commas to separate the inputs
def spamneggs(): spam, eggs = eval(input("Enter # spam then # of eggs:")) print ("You ordered", eggs, "eggs and", spam, "slices of spam. Yum!“)
>>> spamneggs()Enter # spam then # of eggs: 3, 2You ordered 2 eggs and 3 slices of spam. Yum!>>>
![Page 29: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/29.jpg)
Python TypesLast time we introduced the notion of “types”
Ints, Floats, Strings, Characters, and others
Two representations of numbers1 vs 1.0
![Page 30: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/30.jpg)
Numeric Data TypesInside the computer, whole numbers and
decimal fractions are represented (encoded) quite differently!
We say that decimal fractions and whole numbers are two different data types.
The data type of an object determines what values it can have and what operations can be performed on it.
![Page 31: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/31.jpg)
Numeric Data TypesWhole numbers are represented using the
integer (int for short) data type.These values can be positive or negative whole
numbers.Encoded in binary (last week lecture slides and
recitation slides)
![Page 32: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/32.jpg)
Numeric Data TypesNumbers that can have fractional parts are
represented as floating point (or float) values.How might we encode these?What about two numbers?
How can we tell which is which?A numeric literal without a decimal point produces
an int valueA literal that has a decimal point is represented by
a float (even if the fractional part is 0)
![Page 33: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/33.jpg)
Numeric Data TypesPython has a special function to tell us
the data type of any value.>>> type(3)<class 'int'>>>> type(3.1)<class 'float'>>>> type(3.0)<class 'float'>>>> myInt = 32>>> type(myInt)<class 'int'>>>>
![Page 34: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/34.jpg)
Numeric Data TypesWhy do we need two number types?
• Values that represent counts can’t be fractional (you can’t have 3 ½ quarters)
• Most mathematical algorithms are very efficient with integers
• The float type stores only an approximation to the real number being represented!
• Since floats aren’t exact, use an int whenever possible!
![Page 35: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/35.jpg)
Numeric Data TypesOperations on ints produce ints,
operations on floats produce floats (except for /).
>>> 10.0/3.03.3333333333333335>>> 10/33.3333333333333335>>> 10 // 33>>> 10.0 // 3.03.0
>>> 3.0+4.07.0>>> 3+47>>> 3.0*4.012.0>>> 3*412
![Page 36: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/36.jpg)
Numeric Data TypesInteger division produces a whole number.That’s why 10//3 = 3!Think of it as ‘gozinta’, where 10//3 = 3 since 3
goes into 10 3 times (with a remainder of 1)10%3 = 1 is the remainder of the integer
division of 10 by 3.a = (a/b)(b) + (a%b)
![Page 37: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/37.jpg)
HomeworkFinish Chapter 2Read Chapter 3.1Read variables and strings from the python wiki
bookhttp://en.wikibooks.org/wiki/Python_Programming/
Variables_and_StringsWork through the python tutorial
http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3
![Page 38: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/38.jpg)
AnnouncementsPre Lab 3 is now availableIn Lab 3 you will be leveraging the code you
wrote in Lab 2 (it will be provided to you)Starting next week your use of iClickers will be
graded
![Page 39: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/39.jpg)
Python Boot Camp!A few brief string operations
You will need these for labIntroduction to control flowIntuition behind functionsArguments / Returns
![Page 40: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/40.jpg)
String Arithmetic?In the first week we saw
print(“Hello” + “World”)
The + operator “adds” two strings togetherprint(“HelloWorld”)
What if we wanted a space?print(“Hello “ + ”World”)print(“Hello” + “ World”)print(“Hello” + “ “ + “World”)
![Page 41: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/41.jpg)
String Arithmetic?We have an intuition of what + does, can we
reason about * (multiplication)?print(“Hello” * “World”) ???
But what about? print(3 * “Hello”) We know that 3 * 5 is short hand for: 5 + 5 + 5 … Logically 3 * “Hello” would be short hand for: “Hello” + “Hello” + “Hello”
![Page 42: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/42.jpg)
String Arithmetic?We know what “Hello” + “Hello” + “Hello” does!
It gives us: “HelloHelloHello”
![Page 43: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/43.jpg)
Functions
input
function
output
Arguments
Return
![Page 44: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/44.jpg)
Control FlowThe term control flow is typically used to
describe the order of execution in codeThis can be represented as a flow chart
The End you will see in the flowcharts is not an actual programming construct. It merely represents where the chart ends.
In simple programs code flows from the first line to the last line, one line at a time
![Page 45: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/45.jpg)
a = 15
b = -7
print(a)
print(b)
print(a + b)
End
![Page 46: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/46.jpg)
Intuition behind functionsWe want a mechanisms to allow for code reuse
Consider if we have a program A and we want to write a new program that does what A does only twice
a = 15
b = -7
print(a+b)
![Page 47: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/47.jpg)
Program A’
a = 15
b = -7
print(a+b)
a = 15
b = -7
print(a+b)
![Page 48: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/48.jpg)
We can use a function!def A(): a = 15
b = -7
print(a+b)
A()
A()
Abstractly a function provides a name to a piece of code
![Page 49: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/49.jpg)
Week 1 Algorithm Example
1. Remove book from bag2. Place book on desk3. Open book to first page4. Until end of book, Read()
Let us now look at step 4 as a function call
![Page 50: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/50.jpg)
FunctionsRead():1. Place eyes on top left corner of page2. For each word on page
3. ReadWord()
4. If end of page is reached, change current page to next page
![Page 51: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/51.jpg)
Defining A FunctionFunction definitions start with defThis is followed by the function name, and any
arguments if applicableThe : identifies the start of a block of codeThe body of the function is anything indented
beneath this line
def myFunction(a,b):print(a)print(b)
![Page 52: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/52.jpg)
ArgumentsFunctions have an important feature
They can receive and return valuesValues are ‘passed to’ a function inside of ( ) when
called and returned using a special instruction
def myFunction( input ):return output
![Page 53: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/53.jpg)
FunctionsFunctions come up in two ways in programsFunction Definition
Where the function is createdStarts with def in python
Function CallWhere the function is usedresult = myFunction(argument1,argument2)
![Page 54: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/54.jpg)
Function ReturnIf a function needs to return a value back to the
place in the code where it is called it uses a return statement
def myFunction(a,b):print(a)print(b)return a+b
![Page 55: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/55.jpg)
Clicker Question:Are these programs
equivalent?def myFun(a): print(a) return aprint(myFun(4))
def myFun(a): print(a)print (myFun(4))
21
A: yes
B: no
![Page 56: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/56.jpg)
Function CallOnce the function is defined it can be called as
many times as one likes
If the function has a return it can be stored into a variable at the call
myFunction(6,7)myFunction(4,10)
a = myFunction(6,7)
![Page 57: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/57.jpg)
Example Revisited(but slightly modified)
def myFun(a): print(a) return a+1myFun(myFun(4))
![Page 58: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/58.jpg)
Example Revisited(but slightly modified…
again)def myFun(a): print(a) return a+1b =myFun(4)myFun(b)
![Page 59: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/59.jpg)
Clicker Question:Are these programs
equivalent?a = 3def myFun(a): print(a)print(a)
a = 3def myFun(a): print(a) print(a)
21
A: yes
B: no
![Page 60: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/60.jpg)
Clicker Question:Are these programs
equivalent?a = 3def myFun(b): print(b)print(a)myFun(3)
a = 3def myFun(b): print(b) print(b)myFun(3)
21
A: yes B: no
![Page 61: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/61.jpg)
IndentationIndentation is very important in pythonIn functions it defines where the function endsPython will give errors if the indentation is
incorrect
![Page 62: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/62.jpg)
Clicker Question:Are these programs
equivalent?a = 3def myFun(a): print (a)myFun(4)
a = 3print (a)
21
A: yes
B: no
![Page 63: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/63.jpg)
Naming and BindingArguments are bound at the “call site”
Call site: a point in the program where the function is called
The binding is active up until the return of the function
![Page 64: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/64.jpg)
Naming and Binding
a = 3def myFun(a): print (a)myFun(4)print(a)
![Page 65: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/65.jpg)
Local VariablesWe saw that arguments can be rebound for the
duration of the callWhat about variables that we define in the
function body?
![Page 66: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/66.jpg)
Local VariablesAbstractly there are two types of local variables
1) those which introduce a new variable2) those which redefine an existing variable which
was initially defined outside the function definitionLocal variables, just like arguments, have a
lifetime associated with the function call
![Page 67: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/67.jpg)
Local Variablesa = 3y = 10def myFun(a): print (a) y = 1myFun(4)print(a)print(y)
![Page 68: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/68.jpg)
Clicker Question: does this program print 3 or 4?
x = 3def myFun(): print (x)x = 4myFun()
A: 3
B: 4
![Page 69: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/69.jpg)
Variables and FunctionsVariables used in functions but defined outside
of the function can be changedMultiple calls to a function may yield different
results if the program “rebinds” such variables
![Page 70: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/70.jpg)
Variables and Functionsx = 3def myFun(): print (x)x = 4myFun()x =5myFun()
![Page 71: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/71.jpg)
Lets ReviewFunctions take input and produce outputOutput is provided by the “return” statement
Otherwise the function does not provide output
At the call site of the function the arguments get bound The arguments can rebind variables that have already
been defined for the duration of the call
You can use variables defined outside the function but you must be careful!
![Page 72: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/72.jpg)
Function ArgumentsFunctions typically assume something important
about the arguments
Will this work no matter what we provide as arguments?
def sumOfTwo(a,b):return a+b
![Page 73: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/73.jpg)
Function ArgumentsConsider the following three cases:
One of these cases will throw an error. This behavior is defined by the code inside the function
res = sumOfTwo(1,2)res = sumOfTwo(“Hello “, “World”)res = sumOfTwo(“Hello”, 1)
![Page 74: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/74.jpg)
Function ArgumentsThere are two ways to handle this difficulty
1. Tell everyone what the function expects2. Include checks inside the function to ensure the
arguments are what is expected
A combination of both techniques should be used
![Page 75: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/75.jpg)
Function DocumentationThis solution uses comments and if-statements.We will revisit this in later slides
#This function expects two integers and returns -1 def sumOfTwo(a,b):
if type(a) == int and type(b) == int : return a+b
return -1
![Page 76: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/76.jpg)
What all can I do within the body of a function?
Define local variablesCall other functionsDefine new functions
![Page 77: Announcements](https://reader036.fdocuments.in/reader036/viewer/2022062816/568156c4550346895dc45595/html5/thumbnails/77.jpg)
HomeworkRead Chapter 4 (prior to recitation)Start reading chapter 6 (functions)