Adapted by Volodymyr Kuleshov and Isaac Caswell from the CS231n
Python tutorial by Justin Johnson (http://cs231n.github.io/python-numpy-tutorial/).
Update to Python 3 by Long Nguyen
Python Crash Course Tutorial for anyone with some experience in programming.
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, dictionaries, sets, and tuples.
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))
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)
A dictionary is stores (key, value) pairs. Lists are ordered sets of objects; dictionaries are unordered sets. A dictionary's value is accessed by its key rather its position. Dictionaries use curly brackets {}. You can use it like this:
d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data, use {}
print(d['cat']) # Get an entry from a dictionary; prints "cute"
print('cat' in d) # Check if a dictionary has a given key; prints "True"
keys = d.keys() # returns keys of the dictionary.
keys = list(keys)
print(keys)
values = d.values() # returns values of the dictionary.
values = list(values)
print(values)
d['fish'] = 'wet' # Set a new entry in a dictionary
print(d['fish']) # Prints "wet"
print(d)
del(d['fish']) # Remove an element from a dictionary
You can find all you need to know about dictionaries in the documentation.
It is easy to iterate over the keys in a dictionary:
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
legs = d[animal]
print('A {} has {} legs'.format(animal, legs))
If you want access to keys and their corresponding values, use the items method:
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.items():
print('A {} has {} legs'.format(animal, legs))
d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data, use {}
print(d['cat']) # Get an entry from a dictionary; prints "cute"
print('cat' in d) # Check if a dictionary has a given key; prints "True"
x="hello"
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)