CFSS Week 2 Terminology Recap

advertisement
CFSS Week 2 Terminology Recap
function
def my_function(a, b):!
! print “hello world”!
! return a+b
•
Coined by Leibniz (C17)
•
Euler (C18): “f(x)=x2"
•
functions in programming languages: black boxes
that:
•
•
take input
•
perform some action
•
return some output
“synonyms”: procedure, method, subroutine
call
•
To call a function is to invoke it
•
When you call a function, the sequence of
operations stored in its black box take place
•
“synonyms”: invoke
In[1]: ! my_function(3, 4)!
!
Out[1]: ! “Hello World”!
! ! ! 7
argument / parameter
def my_function(a, b):!
! print “hello world”!
! return a+b
•
In the function f(y) = x2:
•
The function is named “y”
•
The argument (Frege late C19) is named
“x” (also called an input parameter)
•
The result is to take a number and return
that number times itself
return value
def my_function(a, b):!
! print “hello world”!
! return a + b
variable / value
•
x = 3!
•
•
x = foo()!
•
•
“The variable named x takes on the
value 3”
“The variable x takes on the return
value of the function named foo()!
x == 3!
•
“Is the variable x equal to 3?”
operator
def my_function(a, b):!
! print “hello world”!
! return a + b
•
a.k.a. an infix function
•
•
as opposed to prefix:
•
+34
•
plus(3,4)
or postfix (sometimes “Reverse Polish
Notation” or RPN):
•
34+
type
•
In Python, a variable has one and only one type:
•
int: integer
•
float: digital approximation of a real number
•
str: a “string” or sequence of characters
•
list: an ordered sequence of things
•
tuple: an unmodifiable ordered sequence of things
•
dict: a mapping of things to other things
•
…and more
object
•
In Python, you can define your own types
using the keyword class
class MyClass:!
"""A simple example class"""!
i = 12345!
def f(self):!
return 'hello world'
keyword
•
A keyword is a special word that should
only be used in certain situations in Python,
and not used as the name of a variable.
•
Examples:
•
def
•
and
•
if
•
while
•
int
•
break
•
elif
•
print
•
str
•
in
•
else
•
return
•
float
•
for
•
not
•
continue
module / import
In [1]: import math!
!
In [2]: math.sin(math.pi/2)!
Out[2]: 1.0
•
A bunch of code somewhere else
•
Specifically:
In [3]: math!
Out[3]: <module 'math' from '/usr/local/Cellar/python/
2.7.9/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/lib-dynload/math.so'>
method (vs. function)
•
A method is a function which is
“owned and operated by” an object
In[1]: !my_string = “hello world”!
! ! ! print my_string.split()!
Out[1]:!['Hello', 'World']
•
As opposed to “global” functions:
In[1]: !my_string = “hello world”!
! ! ! print len(my_string)!
Out[1]:!11
expression
•
An expression is basically any minimally
syntactically valid chunk of code
•
x == 3!
•
x = 3!
•
x and y
… and more
!
•
•
•
•
•
•
•
algorithm
element
index
reference
key-value pair
implementation
hash
•
lookup
•
scope
•
exception
•
parse
•
syntax
•
condition
•
sequence
•
slice
Download