Part 2 will cover:
Python functions are defined using the def
keyword. By default, arguments are positional.
def doubling(x):
""" This is the function's docstrings. It provides
information about the function.
Given a number x.
Returns 2 * x.
"""
return 2 * x
def mult(x, y):
return x * y
print(doubling(5))
mult(3, 4) # positional arguments, x = 3, y = 4.
Input arguments can be specified by name. In this case, the order does not matter.
def printNums(x, y):
print("x's value is {} and y's value is {}".format(x,y))
# the following are all equivalent.
printNums(2, 3)
printNums(x=2, y=3)
printNums(y=3, x=2)
If named arguments and positional arguments are used together, named arguments must follow positional arguments.
def name(first, middle, last):
print(first, middle, last)
name("John", "F", "Kennedy")
name("George", last="Bush", middle="W")
Functions can take optional keyword arguments. These are given default values.
def hello(greeting="Hello,", name="Mike"):
print(greeting, name)
hello()
hello(greeting="Hi,") #name="Mike"
hello(name="John") #greeting="Hello,"
hello(name="Bob",greeting="Welcome,")
Python includes several built-in container types: lists and tuples. (Dictionaries are also very powerful and useful. To keep things simple, we won't cover them here.)
A list is the Python equivalent of an array, but is resizeable and can contain elements of different types:
xs = [3, 1, 2] # Create a list
print(xs, xs[2])
print(xs[-1]) # Negative indices count from the end of the list; prints "2"
xs[2] = 'foo' # Lists can contain elements of different types
print(xs)
xs.append('bar') # Add a new element to the end of the list
print(xs)
x = xs.pop() # Remove and return the last element of the list
print(x, xs)
You can iterate over a list like this:
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print(animal)
To sum over list, use sum instead of a for loop.
nums = [1,2,3,4]
print(sum(nums))
If you want access to the index of each element within the body of a loop, use the built-in enumerate
function.
animals = ['cat', 'dog', 'monkey']
for index, animal in enumerate(animals):
print('#{}: {}'.format(index + 1, animal))
List can contain different types of objects.
things = ["apple", 3, 2.5, [1,2,3]]
for obj in things:
print(obj)
"apple" in things
As usual, you can find all the gory details about lists in the documentation.
In addition to accessing list elements one at a time, Python provides concise syntax to access sublists; this is known as slicing. This is similar to string slicing we saw earlier.
nums = range(5) # range is a built-in function that creates an iterator, in this case,
# from 0 up to but not including 5.
nums = list(nums) # convert to a list. Later we'll use numpy to do this instead.
print(nums) # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4]) # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(nums[2:]) # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(nums[:2]) # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print(nums[:]) # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print(nums[:-1]) # Slice indices can be negative; prints ["0, 1, 2, 3]"
print(nums[:-2]) # Slice indices can be negative; prints ["0, 1, 2]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print(nums) # Prints "[0, 1, 8, 9, 4]"
nums = range(10)
nums = list(nums)
print(nums)
print(nums[1:8:2]) # from 1(including) to 8(not including) by 2.
print(nums[::2]) # from beginning to end(not including) by 2
When programming, frequently we want to transform one type of data into another. As a simple example, consider the following code that computes square numbers:
nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
squares.append(x ** 2)
print(squares)
You can make this code simpler using a list comprehension:
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print(squares)
List comprehensions can also contain conditions:
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print(even_squares)
A tuple is an (immutable) ordered list of values. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
A tuple is in many ways similar to a list. Here is a trivial example:
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
print(tup1)
print(tup1[0])
print(tup2[3])
print(tup3[-1])
tup1[0] = 'math' # error, can't change tuples!
It's easy to convert between lists and tuples.
a = [1,2,3,4,5]
atuple = tuple(a)
alist = list(atuple)
print(atuple)
print(alist)
Lists, tuples, dictionaries and strings are examples of iterables, objects that can be iterated over, as in a for loop.
Iterables allow us to "unpack" in a way that makes our code more readable.
alist = [3,5,2]
# instead of doing this
x = alist[0]
y = alist[1]
z = alist[2]
print(x,y,z)
# we can do this, unpacking.
a, b, c = alist #
print(a,b,c)
Here's another example. Suppose we have a list of (name,grade) tuples.
grades = [("Maria", 91), ("Mike", 88), ("Tom", 81)]
for element in grades:
print(element)
Unpacking allows us to unpack name and grade into distinct variables.
for name, score in grades:
print(name, score)
points = [(1,2), (3,4,5), (6,7)]
for pt in points: # iterate over list(points)
for coord in pt: # iterate over tuple(pt)
print(coord)
By returning a tuple and unpacking, we can create the illusion of a function returning multiple values.
def sumDiff(a,b):
s = a + b
d = a - b
return s, d # returning the tuple (s,d)
sumAB, diffAB = sumDiff(12,7) # unpacking the tuple
print(sumAB, diffAB)
Note: There are built-in functions that can calculate min and max, min() and max(). Don't use them! Use a for loop.
a = ["Tanya","Paxton","Jade"]
def average(lst):
s = 0.0
for item in lst:
# one line of code here.
# fill out return statement.
return
average2([("mike",100),("john",90),("sara",94)]) # returns 94.66666666666667
def average(lst):
average([("mike",100),("john",90),("sara",94)]) # returns 94.66666666666667
list1
and list2
. Every number in list2
that is not in list1
is added to list1
.¶