TA

COMP1029P: Python Bridging Course (Fall 2014) Tutorial

Week 2

In [2]:
# Function

def double(x):
    
    return 2 * x

double(4)
Out[2]:
8
In [3]:
# Comment style 1

def like(stuff):
    # This function takes a string called stuff as input
    # and returns a new string that has "I like " in
    # front of the input string

    return "I like " + stuff # Here is the return statement!

like('python!')
Out[3]:
'I like python!'
In [4]:
# Comment style 2

def like(stuff):
    """ This function takes a string called stuff as input and
    returns a new string that has 'I like ' in front of the
    input string """

    return "I like " + stuff # Here is the return statement!

like('python!')
Out[4]:
'I like python!'
In [5]:
# If and else simple way

def big(x):
    """ Takes a number x as input and returns a string
    indicating whether the number is big or small. """

    if x >= 100:
        return "That is a big number!"
    else:
        return "That is a small number."
    
big(10)
Out[5]:
'That is a small number.'
In [6]:
# If and else smart way

def big(x):
    """ Takes a number x as input and returns a string
    indicating whether the number is big or small. """

    if x >= 100:
        return "That is a big number!"
    return "That is a small number."

big(1000)
Out[6]:
'That is a big number!'
In [7]:
# Chickens and Aardvarks

def a_or_an(noun):
    """ Takes a word (string) as input and returns a new string
    that adds 'a' or 'an' as appropriate. """

    if noun[0] == 'a' or noun[0] == 'e' or noun[0] == 'i' or \
        noun[0] == 'o' or noun[0] == 'u':
        return "an " + noun
    else:
        return "a " + noun
    
print(a_or_an('apple'), a_or_an('banana'))
an apple a banana

In [8]:
# Silly function (elif usage, single line, param num)

def silly(x, y):
    """ This function takes two numbers as input and does
    something silly and useless. """

    if x < y:
        z = 2 * y
        return z
    elif x == y: return 3 * y
    else:
        return 0
In [9]:
# Variable scope, y lives inside the goofy

def goofy(x):
    y = 5
    x = x + y
    return x

print(goofy(3), y)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-36f86d3f9cb0> in <module>()
      6     return x
      7 
----> 8 print(goofy(3), y)

NameError: name 'y' is not defined
In [10]:
# Global variable, y lives in a file

y = 5

def goofy(x):
    x = x + y
    return x

print(goofy(3), y)
8 5

In [14]:
# Global var become local var inside a func

x = 10
def double(x):
    x = x * 2
    return x

print(double(x))
print(x)
20
10

In [15]:
# Some methods inside func may change muttable global var

def A(x):
    x.append(42)
    return x

l = [1, 2, 3]
print(l)
print(A(l))
print(l)
[1, 2, 3]
[1, 2, 3, 42]
[1, 2, 3, 42]

In [16]:
# another example

def B(x):
    x = [1, 2, 3]
    x.append(42)
    return x

l = [4, 5, 6]
print(l)
print(B(l))
print(l)
[4, 5, 6]
[1, 2, 3, 42]
[4, 5, 6]

In [17]:
# loopy function

def loopy():
    """ This function takes no input and simply returns the sum
    of the integers between 1 and 10. """

    sum = 0 # Initialize a counter to 0
    for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: # change to range(1,11) later
        sum = sum + i # update the sum, support +=, but not ++
    return sum # return the sum

loopy()
Out[17]:
55
In [18]:
# built in sum 
print(sum([1,2,3,4]))

# define sum

def oursum(mylist):
    """ Takes a list of numbers as input and returns the sum
    of the items in that list. """

    total = 0
    for number in mylist:
        total = total + number

    return total

print(oursum([2,3,4,5]))
10
14

In [23]:
# count good way

def count(L, X):
    """ Takes a list and an item as input and returns the number
    of times that the item is found in the list. """

    counter = 0
    for item in L:
        if item == X:
            counter = counter + 1

    return counter

count([2,3,4,5,4,6,4,3], 4)
Out[23]:
3
In [24]:
# count not so good way but sometimes necessary

def count(L, X):
    """ Takes a list and an item as input and returns the number
    of times that the item is found in the list. """

    counter = 0
    for i in range(0, len(L)):
        if L[i] == X:
            counter = counter + 1

    return counter

count([2,3,4,5,4,6,4,3], 4)
Out[24]:
3
In [25]:
# func call func

def sum(mylist):
    """ Takes a list of numbers as input and returns the sum
    of the items in that list. """

    total = 0
    for number in mylist:
        total = total + number

    return total

def shoptotal(shoplist):
    """ Takes a list of lists of numbers as input and returns
    the sum of all of those numbers. """

    total = 0

    for shop in shoplist:
        total += sum(shop)

    return total

shoptotal([[2,3], [3,4,5], [1,2]])
Out[25]:
20
In [26]:
# Nested loops

def shoptotal(shoplist):
    """ Takes a list of lists of numbers as input and returns
    the sum of all of those numbers. """

    total = 0

    for shop in shoplist:
        for number in shop:
            total += number

    return total

shoptotal([[2,3], [3,4,5], [1,2]])
Out[26]:
20
In [27]:
# While loops

def fpot(n):
    """ Takes a number n as input and returns the smallest power
    of 2 that is greater than or equal to n."""

    power = 1 # 1 is the smallest power of 2
    while power < n:
        power = power * 2

    return power

fpot(100)
Out[27]:
128
In [28]:
# For or While: for loop is preferable when we know in advance 
# how many times to loop whereas a while loop is ideal when we don't know in advance

def find(DNA, pattern):
    """ Takes a DNA string and a pattern string as input and
    returns the list of starting indices where the pattern
    string is found in the DNA string. """

    indexlist = []

    for i in range(0, len(DNA)):
        if DNA[i:i+len(pattern)] == pattern: #  Python is clever enough to realise that the ending index is not correct, and it will reduce the index to len(DNA) automatically
            indexlist.append(i)

    return indexlist

find('CTAGACTAGCC', 'TAG')
Out[28]:
[1, 6]
In [30]:
# a more complicate findcoding

def findcoding(DNA):
    """ Takes a DNA strings as input and finds the first coding
    region in that string."""

    for i in range(0,len(DNA)):
        # if the next 3 symbols are a start codon
        if DNA[i:i+3] == "ATG":
            # The starting position of the next codon
            j = i + 3

            while DNA[j:j+3] != "TAG" and \
                  DNA[j:j+3] != "TAA" and \
                  DNA[j:j+3] != "TGA":
                
           # while not DNA[j:j+3] in ["TAG", "TAA", "TGA"]:
                        
                # move three positions to the next codon
                j += 3

            # return the coding slice
            return DNA[i:j+3]
    
findcoding('GGATGAAATAGCC')
Out[30]:
'ATGAAATAG'

Week 3

In [1]:
# two dimensional array

creatures = [ [0, 0, 0, 0], [1, 1, 0, 1], [0, 0, 0, 1] ]

# get rows and cols by len(creatures) and len(creatures[0])

print(len(creatures), len(creatures[0]))
3 4

In [2]:
# exchange 0 and 1 inside an 2d array

def change(array):
    """ Takes a list representing a 2D array as input and
    returns a list representing the array in which 0's are
    changed to 1's and 1's are changed to 0's. """

    rows = len(array)
    columns = len(array[0])

    for row in range(rows):
        for column in range(columns):
            
            # using if-else way
            if array[row][column] == 0:
                array[row][column] = 1
            else:
                array[row][column] = 0
                
            # another simpler way
            #array[row][column] = 1 - array[row][column]

    return array # return statement can be omitted as 2d array passed inside will be changed globally

newcreatures = change(creatures)

print(newcreatures)
print(creatures)
[[1, 1, 1, 1], [0, 0, 1, 0], [1, 1, 1, 0]]
[[1, 1, 1, 1], [0, 0, 1, 0], [1, 1, 1, 0]]

In [3]:
# what if we don't want to change the original array passed inside
# need to manually create another array and set its value

def change(input):
    """ Takes a list representing a 2D array as input and
    returns a new list representing the array in which 0's
    are changed to 1's and 1's are changed to 0's. """

    output = [] # our output array is initially empty
    rows = len(input)
    columns = len(input[0])

    for row in range(rows):
        newrow = [] # for each row in the input array
                    # we create a new row in the output array
        for column in range(columns):
            # For each item in the current row
            # add the "opposite" item in the new row
            newrow.append(1 - input[row][column])

        output.append(newrow) # add this new row to output

    return output

newcreatures = change(creatures)
print(newcreatures)
print(creatures)
[[0, 0, 0, 0], [1, 1, 0, 1], [0, 0, 0, 1]]
[[1, 1, 1, 1], [0, 0, 1, 0], [1, 1, 1, 0]]

In [4]:
# do not index an empty list

def change(input):
    """ Takes a list representing a 2D array as input and
    returns a list representing the array in which 0's are
    changed to 1's and 1's are changed to 0's. However, the
    code shown below does not work! """

    output = [] # our output array is initially empty
    rows = len(input)
    columns = len(input[0])

    for row in range(rows):
        for column in range(columns):
            # For each item in the current row
            # add the "opposite" item in the new row
            output[row][column] = 1 - input[row][column]

    return output

newcreatures = change(creatures)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-521433d76736> in <module>()
     19     return output
     20 
---> 21 newcreatures = change(creatures)

<ipython-input-4-521433d76736> in change(input)
     15             # For each item in the current row
     16             # add the "opposite" item in the new row
---> 17             output[row][column] = 1 - input[row][column]
     18 
     19     return output

IndexError: list index out of range
In [5]:
# print a list

def printExample1():

    mylist = [1, 2, 3, 4, 5]

    for item in mylist:
        print(item)

    return

printExample1()
1
2
3
4
5

In [6]:
# print a list, without starting from a new line

def printExample2():

    mylist = [1, 2, 3, 4, 5]

    for item in mylist:
        print(item, end=" ")
       # print(str(item) + ":", end=" ") 

    return

printExample2()
1 2 3 4 5 
In [7]:
# simple animation 

import time
from IPython.display import clear_output

# Set the number of seconds that have passed to zero
sec = 0

while True: # Repeat forever
    # This method of clearing the screen is for the IDLE
    # environment
    print("\n" * 1)

    # Show the number of seconds that have passed
    print(sec)
    if sec % 10 == 0:
        clear_output() # clear_output for notebook

    # Wait for 1 second
    time.sleep(1)

    # Update the number of seconds that have passed
    sec += 1


31


32


33


34

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-7-76cae350882b> in <module>()
     18 
     19     # Wait for 1 second
---> 20     time.sleep(1)
     21 
     22     # Update the number of seconds that have passed

KeyboardInterrupt: 

Week 4

In [1]:
# basic dict

cantonese = {}
cantonese["thanks"] = "m goi"
cantonese["hello"] = "lei ho"

cantonese["hello"]
Out[1]:
'lei ho'
In [2]:
cantonese["goodbye"]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-357b508f9b83> in <module>()
----> 1 cantonese["goodbye"]

KeyError: 'goodbye'
In [3]:
cantonese
Out[3]:
{'hello': 'lei ho', 'thanks': 'm goi'}
In [4]:
# another way
cantonese = {'hello': 'lei ho', 'thanks': 'm goi'}
cantonese.keys()
Out[4]:
dict_keys(['hello', 'thanks'])
In [5]:
# ask if there is a value associated with a given key
print("thanks" in cantonese, "cake" in cantonese)
True False

In [6]:
from random import *

def play():
    """ This is a guessing game! Try to guess the computer's
    number which is an integer between 1 and 99. """

    print("Welcome!")

    # choice is from random package
    secret = choice(range(1, 100))
    numGuesses = 0
    userGuess = 0

    while userGuess != secret:

        userGuess = input("Enter your guess: ")
        userGuess = int(userGuess)
        numGuesses += 1

        if userGuess == secret:
            print("You got it in", numGuesses, "guesses!")
        elif userGuess > secret:
            print("Too high")
        else:
            print("Too low")

    print("Thanks for playing")
    
play()
Welcome!
Enter your guess: 50
Too low
Enter your guess: 75
Too low
Enter your guess: 87
Too low
Enter your guess: 94
Too low
Enter your guess: 98
Too low
Enter your guess: 99
You got it in 6 guesses!
Thanks for playing

In [7]:
# try-except-else for error handling
while True:
    try:
        ageString = input("What is your age? ")
        ageNum = int(ageString)
    except ValueError:
        print("You entered something that is not an integer.")
        print("Try again.")
    else:
        print("Thank you!")
        break
What is your age? i don't know
You entered something that is not an integer.
Try again.
What is your age? zero
You entered something that is not an integer.
Try again.
What is your age? 100
Thank you!

In [8]:
# more exception types
def find_ratio():
    while True:
        try:
            numerator = input("Enter a numerator: ")
            denominator = input("Enter a denominator: ")
            ratio = int(numerator)/int(denominator)
        except ValueError:
            print("You entered something other than a number")
            print("for the numerator or denominator. Try again.")
        except ZeroDivisionError:
            print("You entered zero for the denominator! Try again.")
        else:
            return ratio
    
find_ratio()
Enter a numerator: numerator
Enter a denominator: denominator
You entered something other than a number
for the numerator or denominator. Try again.
Enter a numerator: 1
Enter a denominator: denominator
You entered something other than a number
for the numerator or denominator. Try again.
Enter a numerator: 1
Enter a denominator: 0
You entered zero for the denominator! Try again.
Enter a numerator: 1
Enter a denominator: 2

Out[8]:
0.5
In [9]:
# write to and read from a file

def writeString():
    """ This function asks the user for a string and a file
    name and then makes a new file and stores that string in
    the file. """

    mystring = input("Enter a string that you want to save: ")
    filename = input("Enter a name of a file for writing: ")
    myfile = open(filename, "w") # open file for writing
    myfile.write(mystring) # write the string
    print('finish writing')
    myfile.close() # close the file

def readString():
    """ This function asks the user for a file name and then
    reads the contents of that file and returns it. """

    filename = input("Enter a name of a file for reading: ")
    myfile = open(filename, "r") # open file for reading
    mystring = myfile.read() # read contents into mystring
    myfile.close() # close file

    return mystring # return the string

writeString()
readString()
Enter a string that you want to save: Python Writing Test
Enter a name of a file for writing: writetest.txt
finish writing
Enter a name of a file for reading: writetest.txt

Out[9]:
'Python Writing Test'
In [10]:
# catch exception if filename is wrong for reading

def readString():
    """ This function asks the user for a file name and then
    reads the contents of that file and returns it. """

    try:
        filename = input("Enter a name of a file: ")
        myfile = open(filename, "r")
    except IOError:
        print("There is no file with that name.")
    else:
        mystring = myfile.read()
        myfile.close()
        return mystring
    
readString()
Enter a name of a file: randomfile
There is no file with that name.

In [11]:
# write multiple lines to a file

def writeMultipleStrings():
    """ This function asks the user for input strings and writes
    these strings to a file until an empty string is entered """

    filename = input("Enter a name of a file: ")
    myfile = open(filename, "w")
    mystring = input("Enter a string: ")

    while mystring != "":
        myfile.write(mystring + "\n")
        mystring = input("Enter a string: ")

    myfile.close()
    
writeMultipleStrings()
Enter a name of a file: writetest2.txt
Enter a string: first line
Enter a string: second line
Enter a string: third line
Enter a string: last line
Enter a string: 

In [12]:
# read lines from a file

def readMultipleStrings():
    """ This function asks the user for a file name and reads
    the string in that file into a list. """

    try:
        filename = input("Enter a name of a file: ")
        myfile = open(filename, "r")
    except IOError:
        print("There is no file with that name.")
    else:
        mylist = myfile.readlines()
        myfile.close()
        return mylist

readMultipleStrings()
Enter a name of a file: writetest2.txt

Out[12]:
['first line\n', 'second line\n', 'third line\n', 'last line\n']

Week 5

In [1]:
# scope concept revised

def firstfunc(x):
    print("The value of x in firstfunc(x) is: " + str(x))
    answer = secondfunc(x + 5) + x
    print("The value of x in firstfunc(x) is: " + str(x))
    return answer

def secondfunc(x):
    print("The value of x in secondfunc(x) is: " + str(x))
    x = 1
    print("The value of x in secondfunc(x) is: " + str(x))
    return x + 10

firstfunc(10)
The value of x in firstfunc(x) is: 10
The value of x in secondfunc(x) is: 15
The value of x in secondfunc(x) is: 1
The value of x in firstfunc(x) is: 10

Out[1]:
21
In [2]:
# simple recursion example

def recursivefunc(x):
    print("The value of x in recursivefunc(x) is: " + str(x))
    if x < 20:
        answer = recursivefunc(x + 5) + x
    else:
        answer = x
    print("The value of x in recursivefunc(x) is: " + str(x))
    return answer

recursivefunc(10)
The value of x in recursivefunc(x) is: 10
The value of x in recursivefunc(x) is: 15
The value of x in recursivefunc(x) is: 20
The value of x in recursivefunc(x) is: 20
The value of x in recursivefunc(x) is: 15
The value of x in recursivefunc(x) is: 10

Out[2]:
45
In [3]:
# classical example of factorial function

def factorial(n):
    """ Recursive function for computing factorial of n. """
    if n == 1:
        return 1
    else:
        result = n * factorial(n-1)
        return result
    
factorial(5)
Out[3]:
120
In [4]:
# load turtle graphics module

from turtle import *

# draw a square
def square(length):
    """ Draws a square with the given side length """

    for i in range(4):
        forward(length)
        right(90)

reset()
square(100)
In [5]:
reset()
pencolor("purple") # turtle changes to purple pen
speed(10)          # set speed

# Draw a spiral structure from the center of the window
# outwards using a for loop
for i in range(0, 400, 2):
    forward(i)
    right(89)

# Draw another spiral structure from the outside of the window
# towards the center of the window using another for loop
for i in range(401, 0, -2):
    forward(i)
    right(89)
In [6]:
# draw polygons using recursion

def poly(length, angle, sides):
    if sides == 0:
        return
    else:
        forward(length)
        right(angle)
        poly(length, angle, sides-1)
        
reset()
poly(100, 40, 5)
In [7]:
# class example

import datetime # Needed by get_age() when it calculates the age

class Person:
    def __init__(self, name, date_of_birth, money, description):
        '''
        The constructor of the Person class which initializes
        the attributes of the class
        '''

        # Initialize the attribute "name"
        self.name = name
        # Initialize the attribute "date_of_birth"
        self.date_of_birth = date_of_birth
        # Initialize the attribute "money"
        self.money = money
        # Initialize the attribute "description"
        self.description = description

    def give_money(self, amount, targetPerson):
        '''
        Give money from this person object to the target person
        '''

        # Decrease the attribute "money" of this object
        self.money = self.money - amount

        # Then increase the money of the target instance
        # with the same amount
        targetPerson.money = targetPerson.money + amount

    def get_age(self):
        '''
        Get the age of this person object
        '''

        # Get today's date
        today = datetime.date.today()

        # Split the day, month and year from the
        # date_of_birth attribute and store them
        # into d, m and y variables
        [d,m,y] = self.date_of_birth.split("/")

        # Return the age
        return today.year - int(y)
    
In [8]:
kitty = Person("Hello Kitty", "13/12/1984", 10000, \
"Very energetic and loves to play outdoors")

mary = Person("Mary White", "28/11/1960", 50000, \
"Very kind and loving, and loves to cook")

george = Person("George White", "15/10/1957", 100000, \
"Hardworking and dependable, but has a good sense of humor")

mimi = Person("Mimi White", "13/12/1984", 5000, \
"Kitty's twin sister. She wears a ribbon on her right ear")

print(george.get_age(), mary.money)
mary.give_money(500, kitty)
print(mary.money, kitty.money)


if (kitty.money > mary.money):
    print(kitty.name + " is richer.")
elif (mary.money > kitty.money):
    print(mary.name + " is richer.")
else:
    print("They have same amount of money")
    
if (kitty.get_age() > mary.get_age()):
    print(kitty.name + " is older.")
elif (mary.get_age() > kitty.get_age()):
    print(mary.name + " is older.")
else:
    print("They have same age.")
57 50000
49500 10500
Mary White is richer.
Mary White is older.

In [9]:
# review question
john = Person("John", "15/6/1984", 1000, "Rich man")
paul = Person("Paul", "4/8/1974", 100, "Poor man")
mary = Person("Mary", "19/3/1992", 2000, "Young lady")

if mary.get_age() < john.get_age():
    mary.give_money(1000, john)
    
if mary.money > john.money:
    mary.give_money(500, paul)
else:
    john.give_money(500, paul)
    
print(mary.money, paul.money, john.money)
1000 600 1500

Comments