Finals Week Office Hours

Hi everyone,

I need to adjust my office hours slightly this week due to final exams.  If you’d like to swing by here are my official office hours for finals week:

  • Tuesday 5/12 from 11-12:30pm in WWH 420
  • Wednesday 5/13 from 11-12:30pm in WWH 420

Final Exam Review Guide

I wanted to write up a quick outline of what will be covered on the Final Exam for Introduction to Computer Programming. We will be going over this in detail during class this week as well.

The solutions to the programming problems are available at the end of this post.

  1. Programming Mechanics
    1. Functions (what are they, using them, arguments, return values, etc)
    2. Variables (what are they, creating them, using them, naming rules, etc)
    3. Reading input from the keyboard with the input() function
  2. Math Expressions
    1. Writing math expressions
    2. Evaluating math expressions
    3. Storing & printing the results of math expressions
  3. Data Types
    1. Strings
    2. Numeric data types
      1. Integers (int)
      2. Floating point numbers (float)
    3. The Boolean data type
    4. The List data type
    5. Mixed type expressions
    6. Type conversion (string to int, int to strings, etc)
  4. Basic String Manipulation
    1. Combining two strings (concatenation)
    2. Multiplying a string (x = ‘hi’ * 5)
    3. Case manipulation (x = str.lower(‘CRAIG’) # converts the string literal ‘CRAIG’ to ‘craig’)
    4. Calculating string length using the len() function
  5. Selection Statements
    1. The structure of an IF statement (IF keyword, condition, colon, indentation)
    2. Writing a condition for an IF statement
    3. Boolean operators (<, >, ==, !=, >=, <=)
    4. Comparing numeric values using Boolean expressions
    5. Comparing string values using Boolean expressions
    6. Using the IF-ELSE statement
    7. Nesting decision structures (IF statements inside other IF statements)
    8. The IF-ELIF-ELSE statement
    9. Logical operators (and, or, not)
  6. Generating random numbers using the random.randint() function
  7. Condition Controlled Loops (while loops)
    1. mechanics & how they work
    2. setting up conditions for a while loop
    3. infinite loops and how to avoid them
    4. sentinels (defining a value that the user enters that causes the loop to end)
    5. input validation loops (asking the user to continually enter a value until that value matches some condition)
  8. Accumulator variables
    1. setting up and using accumulator variables
    2. self referential assignment statements (i.e. counter = counter + 1)
    3. augmented assignment operators (i.e. counter += 1)
  9. Count Controlled Loops (for loops)
    1. mechanics and how they work
    2. iterating over a list (i.e. for x in [1,2,3,4,5]:)
    3. using the target variable in a for loop
    4. nested loops (i.e. loops inside of other loops)
  10. Functions
    1. mechanics and how functions work
    2. the “black box” model
    3. function definitions
    4. arguments
    5. return values
    6. calling a function
    7. local variables (variables that are defined inside a function and can only be accessed inside that function)
    8. passing arguments to your own functions
    9. writing a value returning function (i.e. using the ‘return’ keyword to send a result from your function to the part of your program that called that function)
    10. Input, Processing & Output notation
  11. Modules
    1. Creating a module
    2. Defining functions in a module
    3. Calling functions in a module
  12. Exceptions
    1. Preventing exceptions using selection statements (i.e. using an “if” statement to prevent an error from occuring)
    2. Using the try / except / else suite to test problematic code for an error and “catch” it before it has a chance to crash your program.
  13. Lists
    1. Simple Variables vs. Lists (simple variables can only hold one piece of data, but lists can hold multiple values) – you can think of a list like a “book” and a variable like a “sheet of paper”
    2. Defining lists in Python (i.e. mylist = [1,2,3])
    3. Concatenating lists with the “+” operator
    4. Repeating lists with the “*” operator
    5. Referencing list items using index notation (i.e. mylist[0])
    6. Iterating through a list using a “while” loop
    7. Iterating through a list using a “for” loop
    8. Using the len() function to determine the # of items in a list
    9. Updating the value of an item in a list using bracket notation
    10. Creating empty lists
    11. Finding an item in a list using the “in” operator
    12. Adding items to a list using the append method
    13. Sorting items in a list using the sort method
    14. Reversing items in a list using the reverse method
    15. Finding the position of an item in a list using the index method
    16. Inserting an item in a list at a specific index using the insert method
    17. Finding the largest and smallest values in a list using the min and max methods
    18. Totaling the values of all elements in a list using an accumulator variable
    19. Removing an item from a list using the remove method
    20. Storing lists in files
    21. Reading lists from files using the readlines method
  14. Strings Manipulation
    1. Iterating through all characters in a string using a for loop
    2. Indexing a specific character in a string using bracket notation
    3. Iterating through all characters in a string using a while loop
    4. String immutability (you can’t change a string using bracket notation like you would change a list element)
    5. Testing a string for substrings using the “in” operator
    6. Detecting character types in a string using the built-in string testing methods (isdigit, isalpha, isalnum, islower, isupper, isspace)
    7. Splitting a string into a list using the “split” method
  15. File Input & Output
    1. Opening a file for writing
    2. Opening a file for reading
    3. Writing data to a file
    4. Delimiters (separating data in a file)
    5. Reading data from a file using the read() method
    6. Reading data from a file into a list
    7. Processing data stored in a file
  16. Dictionaries
    1. What is a Dictionary?
    2. Basic usage of Dictionaries
    3. Differences between Lists and Dictionaries
    4. Knowing when to use which data type (lists for numerically indexed data, dictionaries for string-indexed data)

The final exam will consist of approximately 9 questions divided into two types – Trace the Output and Programming Challenges.

Also, as promised, the Final Exam will come with a comprehensive command listing of every function, method and language structure that we covered this semester. You can download a copy of this listing here: Python_Command_Index

Sample Problems

Evaluate the following expressions. Note the data type that gets generated by each expression as well.  You can check the solutions to these problems by copying and pasting them into IDLE to see how Python reacts.

Expression
‘a’ + ‘b’
‘a’ * 5
5 * ‘a’ + 5 * ‘b’
[1] + [2]
['a', 'b'] + ['c', 'd']
[2] * 4 + [4] * 2
[1,2,3] + 3 * [4]
99 % 3
5 + (10 % 3)
100 > 5
100 > 5 and 100 < 99
1 > 2 or 2 > 1
not (1 > 2) and not (3 < 3)
(True or False) and (False and True)
x = ‘giraffe’
x[2] # evaluate the result of this line of code
x = ‘G+I+R+A+F+F+E’
x.split(“+”) # evaluate the result of this line of code
‘cat’ + ‘dog’
‘cat’ > ‘dog’
(99 * 2) < 200 or (100 > 50 * 2)
True or False or False or True

Trace the Output

Trace the output of the following programs (each shaded box below is a separate Python program.)

count = 0

while count < 10:
    print ('Hello')
    count += 1
x = 10
y = 0

while x > y:
    print (x, y)
    x = x - 1
    y = y + 1
keepgoing = True
x = 100

while keepgoing:
    print (x)
    x = x - 10
    if x < 50:
        keepgoing = False
x = 45

while x < 50:
    print (x)
for x in [1,2,3,4,5]:
    print (x)
for p in range(1,10):
    print (p)
for z in range(-500,500,100):
    print (z)
x = 10
y = 5

for i in range(x-y*2):
    print ("%", i)
c = 0

for x in range(10):
    for y in range(5):
        c += 1

print (c)
x = [1,2,3]

counter = 0
while counter < len(x):

    print (x[counter] * '%')

    for y in x:
        print (y * '*')

    counter += 1
for x in 'lamp':
    print (str.upper(x))
x = 'one'
y = 'two'

counter = 0
while counter < len(x):
    print (x[counter], y[counter])
    counter+=1
x = "apple,pear,peach"
y = x.split(",")

for z in y:
    print (z)
x = 'apple,pear,peach,grapefruit'
y = x.split(',')

for z in y:
    if z < 'm':
        print (str.lower(z))
    else:
        print (str.upper(z))
def myfun(a):
    print (a * '#')

x = "1-2-3-4-5"
y = x.split("-")

for z in y:
    myfun(int(z))
# Trace the output of the following program.  Then rewrite the program using a “while” 
# loop instead of a “for” loop.  You don’t need to re-write the list – just rewrite 
# the looping portion of the program.

pokemon = ["squirtle", "pikachu", "charmander",    
           "bulbasaur", "meowth"]

for i in range(0, len(pokemon), 2):
    print (i, pokemon[i])


# rewrite
counter = 0

while counter < len(pokemon):
    print (counter, pokemon[counter])
    counter += 2
foo = { "apple":1.99, "pear":2.99, "peach":3.99 }
bar = [ "apple", "starfruit", "orange", "pear", "PEACH", "banana" ]

for i in range(len(bar)):
    print (i+1, bar[i], ": ", end="")
    if bar[i] in foo:
        print (foo[ bar[i] ])
    else:
        print ("?")
pokemon = { "pikachu":["electricity",100], "charmander":["fire",90], "squirtle":["water",110] }

for key in pokemon.keys():
    print (key)
    for i in range(len(pokemon[key])):
        print ("*", pokemon[key][i])
# Given the data file below (datafile.txt), what will be stored in the file at the 
# end of this program?  Note that every line has a line break at the end of it.
# 
# You can create this file by opening up a text editor on your computer (TextEdit on a
# Mac or Notepad on a PC) and creating a file called "datafile1.txt" in the same
# folder as your Python program if you want to test this out.
# Alternately, see the program after this one - it will create the necessary file for
# you using Python

datafile1.txt
apple
pear
peach
grapefruit

________________________________________
file_object = open("datafile1.txt")
data = file_object.read()
file_object.close()

file_object2 = open("datafile1.txt", "a")
data = data.split("\n")

c = 0
for item in data:
    if c > 0:
        file_object2.write("," + item)
    else:
        file_object2.write(item)
    c += 1
    
file_object2.write("\n")
file_object2.close()
# this program can be used to create 'datafile1.txt' for the previous
# trace the output question
fo = open("datafile1.txt", "w")
fo.write("apple\n")
fo.write("pear\n")
fo.write("peach\n")
fo.write("grapefruit\n")
fo.close()
votes = {
          "NJ":[1478749,2126610,45774],
          "NM":[35788,415335,32634],
          "NY":[2490496,4485877,105163]
        }

r = 0
d = 0
o = 0

for key in votes.keys():
    r += votes[key][0]
    d += votes[key][1]
    o += votes[key][2]

print ("Republican:", r)
print ("Democrat:", d)
print ("Other:", o)

Programming Challenges

  1. Write a program that prompts the user to enter in 5 colors. Print out the colors entered in alphabetical orders. Then print out the colors entered in reverse alphabetical order.
  2. Write a program that prompts the user to enter in an unlimited number of colors. Test to make sure that the user did not already enter a color before storing it (i.e. “We’re sorry, but you’ve already added the color ‘yellow’”). When the user types the string “exit” you should stop collecting colors. The print out the colors entered in alphabetical order.
  3. Rewrite the following program using a “for” loop:
    x = [1,2,3,4,5]
    
    counter = 0
    while counter < len(x):
        print (x[counter])
        counter += 1
  4. Rewrite the following program using a “while” loop:
    x = ['a','b','c','d']
    
    for y in x:
        print (y)
  5. Rewrite the following program using a “while” loop:
    x = 'python'
    
    for y in x:
        print (y)
  6. Write a program that lets a user enter in 5 price values. Ensure that the prices entered are greater than 0 (but you can assume they will be numeric and not alphanumeric). Once you have collected all the prices you should print out the following:
    1. The average price
    2. The highest price
    3. The lowest price
    4. If the user spent more than 100, you can give them the lowest priced item for free. In this case you should congratulate them, print out their total bill and average price without the lowest priced item factored in.
  7. Write a function that accepts a single string as an argument. Your function should then determine if the string contains all uppercase characters. If so, return True, and if not return False.
  8. Write a function that accepts a single string as an argument. Convert the string to all lowercase and return the new string.
  9. Write a program that continually prompts the user to enter in a number. Evaluate whether the value entered is a valid number (float or int). If it is, print out the number. If it isn’t, ask the user to try again.  Your program should not crash if the user enters an invalid number (i.e. “twenty” is invalid since it does not contain all numeric digits)
  10. Write a function that generates a random letter from the string ‘BINGO’. Here is a sample running of this program:
    x = generate_bingo_letter()
    print (x)
    
    >> N
    
    x = generate_bingo_letter()
    print (x)
    
    >> O
  11. Take your “bingo” function and write a program that continually generates 5 random letters from the string ‘BINGO’ and print them out along with the attempt number. If all 5 letters spell the word “BINGO” you can end the program and print out a congratulatory message. Here’s a sample running of the program:
    0 GBOBI
    1 OOOIG
    2 NIIIN
    3 BIGNN
    4 OGOIN
    5 BNOGB
    
    .......
    
    3856 BGIIO
    3857 NBONN
    3858 IBNGI
    3859 GNOBN
    3860 BINGO
    Finally, after 3860 tries!
  12. You have been asked to write a “slot machine” program which should randomly arrange three graphical symbols on the screen.  All possible symbols are defined in the following list:
    symbols = ['cherry', 'pear', 'star', 'seven', 'watermelon']

    Write a program that randomly selects three of these symbols and prints them out to the user.  You cannot repeat a symbol (i.e. you cannot select ‘cherry’ twice).  Here’s  are a few sample runnings of the program:

    >> cherry star seven
    
    >> seven watermelon star
    
    >> pear star cherry
  13. Write a program that generates a random series of lottery numbers for the user.  Your program should generate 5 unique random numbers between 1 and 100.  Numbers should not be repeated within a series and should be printed in ascending order.  Here are three sample runnings of the program:
    Your lottery #'s are:
    [19, 61, 62, 78, 99]
    >>> ================================ RESTART 
    
    Your lottery #'s are:
    [1, 41, 64, 66, 78]
    >>> ================================ RESTART 
    
    Your lottery #'s are:
    [19, 20, 28, 41, 97]
  14. Body Mass Index (BMI) is a measurement that is used to classify the weight of an individual as underweight, normal weight, overweight or obese.  The formula for calculating BMI is as follows:BMI = (Weight in pounds * 703) / (height in inches)2.  BMI values can be interpreted as follows:
    • Below 18.5 – underweight
    • 18.5 – 24.9 – normal
    • 25 – 29.9 – overweight
    • 30+ – obese

     

    Write a function called “calc_bmi” that accepts two arguments – a weight value in pounds (float) and a height value in inches (float).  You can assume the arguments supplied are valid floats.  Using these arguments calculate BMI using the formula above, and return  a the BMI (float) and a description (string).  Use IPO notation to document your function.

  15. Write a program that asks the user for their weight in pounds (one value) and their height in feet and inches (two values).  You cannot assume anything about these values, so validate the input as necessary.  Next, use the function you wrote for question #14 to calculate the user’s BMI and print it out for them as follows.  Your program does not need to repeat.  Note that 12 inches = 1 foot. (10 points)
    Enter weight in pounds:  none of your business
    Sorry, that’s not a valid weight.  Try again
    Enter weight in pounds:  150
    Enter height in feet: 6
    Enter height in inches:  none
    Sorry, that’s not a valid height.  Try again.
    Enter a height in inches:  0
    
    A 6’0 person weighting 150lbs has a BMI of 20.3414352.  This is considered normal weight.
    
  16. Write a function that rolls a 10 sided die. Your function should take no arguments and should return the result of the die roll.  Then write a program that lets the user enter two integers (validate the values – the user could enter any data type here). Next, continually uses your function to roll two dice until both die roll the numbers that the user entered at the same time.  Note that the numbers may appear in any order (i.e. if the user enters 6 and 5 a valid end state of your program would be the rolls (6,5) or (5,6).  Keep track of how many rolls it took to reach this state. Here is a sample running of this program:
    Enter a number between 1 and 10:  6
    Enter a number between 1 and 10:  5
    
    1. 5 and 1
    2. 6 and 3
    3. 3 and 10
    4. 5 and 6
    Your numbers were found on roll # 4
    
  17. Write a function called “line” that accepts a string as an argument. Your function should generate a string pattern based on the argument provided. Here are the rules:
    • If the string provided is a number, and that number is even, generate a pattern of hash signs that matches the number provided. For example, calling line(“4”) will generate the following:
      ####
      

      and calling line(“10”) will generate the following:

      ##########
      
    • If the string provided is a number, and that number is odd, generate a pattern of stars using the number provided. For example, calling line(“3”) will generate the following:
      ***
      

      and calling line(“9”) will generate the following:

      *********
      

      If the string provided is not a number you can simply generate and return empty string.

    Once you have generated your pattern you should return the result when completed. You cannot assume anything about the supplied string, and your function should avoid raising any exceptions that will cause your program to crash. If an exception is raised you can return an empty string.

  18. Write a FUNCTION called “valid_n_number” that determines if a NYU Student ID is valid. For the purpose of this question a valid NYU Student ID is defined as follows:
    • exactly 9 characters long
    • begins with the uppercase character ‘N’
    • all characters beside the beginning ‘N’ character must be numbers

    Your function should accept a test “N number” as an ARGUMENT (String) and RETURN a status value (Boolean). Here’s a sample program that uses your function:

    test1 = valid_n_number("N123")
    test2 = valid_n_number("N1234567890")
    test3 = valid_n_number("P12345678")
    test4 = valid_n_number("NXYZ!5678")
    test5 = valid_n_number("N12345678")
    
    print (test1, test2, test3, test4, test5) 
    

    And here’s the expected output:

    False False False False True
    
  19. Write a program that asks the user to enter in 5 NYU Student ID numbers. Ensure that the ID numbers that the user enters are valid (use your function from question #3) and that they do not enter duplicate IDs. Let the user know if they make a mistake. When you have collected 5 IDs print them out in ascending order. Here’s a sample running of this program.
    Enter a N Number: N12345678
    Enter a N Number: N123
    Invalid N Number
    Enter a N Number: P12345678
    Invalid N Number
    Enter a N Number: N12345678
    Duplicate N Number
    Enter a N Number: N00000003
    Enter a N Number: N00000001
    Enter a N Number: N00000002
    Enter a N Number: N00000000
    
    Your N-Numbers:
    
    N00000000
    N00000001
    N00000002
    N00000003
    N12345678
    

 

Programming Problems – Solutions

Problem #1

# create an empty list
colors = []

# prompt the user for 5 colors
counter = 0
while counter < 5:

    # get a color from the user
    color = input("Give me a color: ")

    # put the color in the list
    colors.append(color)

    counter += 1

# sort the list
colors.sort()

print (colors)

# reverse the list
colors.reverse()

print (colors)

Problem #2

colors = []

# ask the user for an unlimited number of colors
keepgoing = True

while keepgoing == True:

    # ask the user for a color
    color = input('Give me a color: ')

    # does the user want to exit?
    if color == 'exit':
        keepgoing = False

    elif color in colors:
        print ("Sorry, that's already in the list.")

    else:
        colors.append(color)

# sort the list
colors.sort()

print (colors)

Problem #3

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

for y in x:
    print (y)

Problem #4

x = ['a','b','c','d']

counter = 0

while counter < len(x):
    print (x[counter])
    counter += 1

Problem #5

x = 'python'

counter = 0

while counter < len(x):

    print (x[counter])

    counter += 1

Problem #6

prices = []

# grab 5 values from the user ensuring that they are
# all greater than 0
keepgoing = True

# counter variable
total = 0

while keepgoing == True:

    # grab a price
    price = float(input("Give me a price: "))

    # is the price greater than zero?
    if price <= 0:
         print ("Sorry, too low.")
    else:
         prices.append(price)
         total += price

    # see if we can get out of the loop
    if len(prices) == 5:
         keepgoing = False 

print ("Average: ", total / 5)
print ("Highest: ", max(prices))
print ("Minimum: ", min(prices)) 

# special deal
if total > 100:

    newtotal = total - min(prices)

    print ("Congrats!")
    print ("New total: ", newtotal)

    print ("New average: ", newtotal / 4)

Problem #7

def uppercase_test(s):

    # look at each character in s and determine
    # if it is uppercase or lowercase
    for char in s:

        if char.isupper() == False:
            return False

    return True

x = uppercase_test("CRAIG")

print (x)

Problem #8

def lowercase_converter(s):

    s = str.lower(s)
    return s

Problem #9

keepgoing = True

while keepgoing:

    # test to see if the number is truly a number
    try:
        num = float(input("Give me a number: "))

    # if there was an error, do this:
    except:
        print ("Sorry, bad number!  Try again.")

    else:
        keepgoing = False

print (num)

Problems #10 & #11

import random

def generate_bingo_letter():

    # what is the string we are using as a source?
    source = 'BINGO'

    # generate a random integer between 0 and 4
    num = random.randint(0,4)

    # grab out the substring
    letter = source[num]

    # send it back
    return (letter)

keepgoing = True

counter = 1

while keepgoing == True:

    # generate 5 random characters
    c1 = generate_bingo_letter()
    c2 = generate_bingo_letter()
    c3 = generate_bingo_letter()
    c4 = generate_bingo_letter()
    c5 = generate_bingo_letter()

    # build a word out of this letter
    word = c1 + c2 + c3 + c4 + c5

    # does this spell bingo?
    if word == 'BINGO':
        print ("got it after", counter, "tries!")
        keepgoing = False
    else:
        counter += 1

Problem #12

import random

symbols = ['cherry', 'pear', 'star', 'seven', 'watermelon']

# select 3 symbols at random 
for x in range (3):

     # get a random number between 0 and the length of our list
     i = random.randint(0, len(symbols) - 1) 

     # print out the symbol at this position
     print (symbols[i], ' ', end = '')

     # remove the symbol from the list 
     symbols.remove(symbols[i])

Problem #13

import random

lottery = []

for x in range(5):

    while True:

        num = random.randint(1,100)
        if num not in lottery:
            lottery.append(num)
            break


lottery.sort()

print (lottery)

Problems #14 and #15

def calc_bmi(weight, height):

    bmi = (weight*703)/(height**2)

    if bmi < 18.5:
        return bmi, "Underweight"
    elif bmi < 25:
        return bmi, "Normal weight"
    elif bmi < 30:
        return bmi, "Overweight"
    else:
        return bmi, "Obese"



# get weight
while True:
    try:
        weight = float(input("Enter weight in pounds: "))
    except:
        print ("Not a valid weight, try again")
    else:
        if weight < 0:
            print ("Negative weight values are not allowed")
        else:
            break

# get height in feet and inches
while True:
    try:
        height_feet = int(input("Enter height in feet: "))
        height_inches = int(input("Enter height in inches: "))
    except:
        print ("Not a valid height, try again")
    else:
        if height_feet <= 0 or height_inches < 0 or height_inches >= 12:
            print ("Invalid entry, try again")
        else:
            break

# calc bmi
bmi, description = calc_bmi(weight, height_feet*12+height_inches)

print ("A", height_feet, "'", height_inches, "person weighing", weight, "lbs has a BMI of", bmi, ". This is considered", description)

Problem #16

import random

def roll_10_sided_die():
    d1 = random.randint(1,10)
    d2 = random.randint(1,10)
    return d1, d2

while True:
    try:
        user_d1 = int(input("Enter a number between 1 and 10: "))
        user_d2 = int(input("Enter a number between 1 and 10: "))
    except:
        print ("Invalid number, try again")
    else:
        if user_d1 < 1 or user_d1 > 10 or user_d2 < 1 or user_d2 > 10:
            print ("Numbers must be between 1 and 10, try again")
        else:
            break

counter = 0
while True:

    counter += 1

    d1, d2 = roll_10_sided_die()

    print (counter, ". ", d1, " and ", d2, sep="")

    if (d1 == user_d1 and d2 == user_d2) or (d1 == user_d2 and d2 == user_d1):
        print ("Your numbers were found on roll #", counter)
        break

Problem #17

def line(x):

    try:
        if x.isdigit():
            x = int(x)
            if x % 2 == 0:
                return x*"#"
            else:
                return x*"*"
        else:
            return ""
    except:
        return ""


print (line("4"))
print (line("5"))
print (line("apple"))
print (line(100))

Problems #18

def valid_n_number(number):

    if len(number) != 9:
        return False
    else:
        if number[0] != "N":
            return False
        else:
            testdigits = number[1:]
            if testdigits.isdigit():
                return True
            else:
                return False


print (valid_n_number("N123") )
print (valid_n_number("N1234567890") )
print (valid_n_number("P12345678") )
print (valid_n_number("NXYZ!5678") )
print (valid_n_number("N12345678") )

Problem #19

def valid_n_number(number):

    if len(number) != 9:
        return False
    else:
        if number[0] != "N":
            return False
        else:
            testdigits = number[1:]
            if testdigits.isdigit():
                return True
            else:
                return False


numbers = []

while len(numbers) < 5:

    test = input("Enter an N#: ")

    if valid_n_number(test) == False:
        print ("Invalid N Number")
    else:
        if test in numbers:
            print ("Duplicate N Number")
        else:
            numbers.append(test)

numbers.sort()

print (numbers)

Day 27 Wrap Up

  1. A Dictionary in Python is a sequence object like a list but, unlike a list, a Dictionary doesn’t use integer based index values. Instead, Dictionaries use immutable objects (like Strings) to index their content. In other languanges Dictionaries are sometimes referred to as “Associative Arrays” or “Hashmaps”
  2. You can create a Dictionary using the curly braces – “{” and “}”, like this:
    my_dictionary = { }
    
  3. In order to add an item to a Dictionary you need to specify a “key” – this is usually in the form of a String. You can then associate some data with that key. For example I could associate the number 3.2 with the key “python” by doing this:
    my_dictionary["python"] = 3.2
    

    This will place the number 3.2 into the Dictionary at the position marked by the String “python”

  4. You can access all items in a Dictionary by printing it out, like this:
    print (my_dictionary)
    

    However, you often just want to access one item – this works the same as with an array, but you will use a key instead of an integer index:

    print ( my_dictionary["python"] )
    
  5. You can initialize a Dictionary with a known set of key / value pairs by using the following syntax:
    my_dictionary = { "python":3.2, "java",1.8 }
    

    This will create a Dictionary with the keys “python” and “java”

  6. Note that you cannot access elements in a Dictionary that have not been defined. This would raise an exception if “php” was not a key in the Dictionary:
    print ( my_dictionary["php"] )
    

    You can use the “in” operator to test to see if a key is in a dictionary like this:

    if ( "java" in my_dictionary" ):
    

    Note that this will check for the presence of a key in a dictionary, not for the data that the key is storing!

  7. You can use the del command to delete a key in a Dictionary, like this:
    del my_dictionary["java"]
    

    Make sure that you know that the key in question has been defined in the Dictionary before you run this command!

  8. You can clear all keys in a Dictionary by doing the following:
    my_dictionary.clear()
    
  9. To iterate over every time in a Dictionary you need to be able to visit every key value (as opposed to simply knowing the size of a list and iterating over the integer range of the list)
    You can extract all the keys from a Dictionary by doing the following:

    for key in my_dictionary.keys():
       # statements
    

    The target variable “key” will assume each key value in your Dictionary as the loop progresses.
    You can print out all items with their keys by doing the following:

    for key in my_dictionary.keys():
    	print (key, " == ", my_dictionary[key])
    

    You can also iterative over a Dictionary by using the following technique to extract both the key and the value at the same time:

    for key, value in my_dictionary.items():
    	print (key, value)
    

    And if you want to just extract the items (without the keys) you can do the following:

    for value in my_dictionary.values():
    	print (value)
    
  10. Next we wrote a program that asks the user to enter in a series of student names and test scores and used a Dictionary to store a new record in the dictionary based on the student name (i.e. the name becomes the key).
    # accum dictionary
    roster = {}
    
    # get student names and scores
    while True:
    
        # get a name
        name = input("name: ")
    
        # sentinel
        if name == "end":
            break
    
        # grab a score
        score = float(input("score: "))
    
        # do we know this student?
        if name in roster:
    
            # add to the students score
            roster[name] += score
        else:
    
            # put into the dictionary
            roster[ name ] = score
    
    
        print (roster)
    
    
      
    
  11. We also allowed the user to enter in multiple scores for each student – if the student already has a score we added it to a list of scores for that particular student.
    # accum dictionary
    roster = {}
    
    # get student names and scores
    while True:
    
        # get a name
        name = input("name: ")
    
        # sentinel
        if name == "end":
            break
    
        # grab a score
        score = float(input("score: "))
    
        # do we know this student?
        if name in roster:
    
            # add to the students score
            roster[name].append(score)
    
        else:
    
            # put into the dictionary
            roster[ name ] = [ score ]
    
    
        print (roster)
    
    
    # examine all scores and report to the user
    for key in roster.keys():
    
        print ("Score for", key)
        print ("* Total tests:", len( roster[key] ) )
        print ("* Average score:", sum(roster[key])/len(roster[key]))
    
  12. Next we counted the frequency of all words in the Declaration of Independence! Here’s the data file we used (declaration.txt):
    # open the file for reading
    fo = open("declaration.txt", "r")
    data = fo.read()
    fo.close()
    
    # split on the line break
    splitlines = data.split("\n")
    
    # create the dictionary
    frequency = {}
    
    # iterate over the lines
    for line in splitlines:
    
        line = str.lower(line)
        splitwords = line.split(" ")
    
        # iterate over the words
        for word in splitwords:
    
            # create a new empty word
            newword = ""
    
            # examine each character in the word and remove
            # all non-alpha characters
            for character in word:
                if character.isalpha():
                    newword += character
    
            # make sure new word actually has some letters in it
            # then add it to our dictionary
            if len(newword) > 0:
    
                if newword not in frequency:
    
                    frequency[ newword ] = 1
    
                else:
    
                    frequency[ newword ] += 1
    
    
    for key in sorted(frequency.keys()):
        print (key, ":", frequency[key])
    
    
    # ask the user to query the database
    while True:
    
        # get a search term
        search = input("Enter a search term:")
    
        # have we seen this term?
        if search in frequency:
    
            print ("Your term was found", frequency[search], "times")
    
        else:
            print ("Your term was not found")
    
    
    
  13. Note that you cannot access elements in a Dictionary that have not been defined. This would raise an exception if “java” was not a key in the Dictionary:
    print ( my_dictionary["java"] )
    

    You can use the “in” operator to test to see if a key is in a dictionary like this:

    if ( "java" in my_dictionary" ):
    

    Note that this will check for the presence of a key in a dictionary, not for the data that the key is storing!

  14. Next we wrote a program that is based on a data set that contains three fields – a location, a bug species and a count of the # of bugs observed (bugs.txt).  The file contains an arbitrary number of records in any order. Calculate the following:
    Number of bugs by species
    Number of bugs by location
    Number of bugs by species & location. Here’s the solution:

    fo = open("bugs.txt", "r")
    data = fo.read();
    fo.close()
    
    # create a dictionary to keep track of bugs by borough
    bugs_b = { }
    bugs_s = { }
    bugs_c = { }
    
    # iterate over by line
    splitlines = data.split("\n")
    
    for line in splitlines:
    
        temp = line.split(",")
    
        # ADD BY species and borough
        key = temp[0] + temp[1]
    
        if key not in bugs_c:
            bugs_c[key] = int(temp[2])
        else:
            bugs_c[key] += int(temp[2])
    
        # add by species
        if temp[1] not in bugs_s:
            bugs_s[temp[1]] = int(temp[2])
        else:
            bugs_s[temp[1]] += int(temp[2])
    
    
        # add by borough
        if temp[0] not in bugs_b:
    
            bugs_b[temp[0]] = int(temp[2])
    
        else:
    
            bugs_b[temp[0]] = bugs_b[temp[0]] + int(temp[2])
    
    print (bugs_b)
    print (bugs_s)
    print (bugs_c)
    
      
    

Day 26 Wrap Up

Today we began our discussion on File Input and Output using Python. Here’s an overview of what we covered:

  1. All of the programs that you have been writing so far have reset themselves each time they run. This is because the data associated with the program (i.e. your variables) are stored in your computer’s memory (RAM)
  2. RAM is a volatile place – it serves as a computer’s short term memory. RAM that is being used by a program is cleared when the program stops running. If programs are to retain data between executions then we must find a more permanent way to store and save information for future use.
  3. A computer almost always has at least one type of long-term storage device at its disposal (usually some kind of hard drive) — we can use the long term storage capabilities of a computer to store data in the form of a file.
  4. Once we save a file it will remain on the long term storage device after the program is finished running, and can be accessed and retrieved later on. This is a pretty common technique and is used by almost all programs that need to keep track of some kind of information between executions.
  5. There are two types of files we can work with – “text files” and “binary files.”
  6. Text files are files that contain data that is encoded as text (i.e. ASCII or Unicode characters). Data stored in a text file is visible and can be read by a program that is designed to view / edit textual data (i.e. a word processing program). We will be working exclusively with Text Files this semester.
  7. Binary files contain data that is not encoded as a text format – they are intended to be read by other programs and not by humans directly. Binary files appear as “gibberish” when viewed via a word processing program. An example of a binary file would be a movie or image file – if you were to open it up in a word processing program you would see nothing but intelligible characters.
  8. Every file must have its own filename, which is nothing more than a label that allow us to uniquely identify groupings of data that exist on a long term storage device. On most operating systems a filename comes with a “file extension” which allows you to quickly tell what kind of data is stored inside that file. Extensions come after the last period in a file (i.e. “mydocument.doc” – “doc” is the file extension). File extensions tell the operating system what kind of data is stored in the file and allows the OS to select an appropriate program to open a particular file.
  9. To begin working with files in Python you need to first create a “File Object.” A File Object is a special variable that serves as a connection between your program and the operating system. File Objects tell the OS the name of a file that you wish to work with along with what you want to do to that file (write, read, append, etc). Here’s how to open up a file object in Python that tells the operating system that you want to write to file called “myfile.txt”:
    file_object = open("myfile.txt", "w")
  10. The open() function takes two arguments – the name of a file and a “mode” operation. Both of these arguments are strings. Python supports three file modes:
    1. “w” – Write Mode. Open up a file so you can write information to that file. If the file does not exist it will be created. If the file does exist it will be overwritten.
    2. “a” – Append Mode. Open up a file so you can write information to that file. If the file does not exist it will be created. If the file does exist we will simply “append” any new data to the end of the file.
    3. “r” – Read Mode. Opens upa file so that you can read its contents.
  11. You can write data to a file that has been opened for writing by using the write() function. Here’s an example:
    # open up a file object for writing
    file_object = open('myfile.txt', ‘w’)
    
    # store the string 'craig' in the file
    file_object.write('craig')
    
    # store the string 'hello' in the file
    file_object.write('hello')
    
    # close the file when you’re done
    file_object.close()
  12. If you were to look at the file that gets created by the source code above you would see that both strings (‘craig’ and ‘hello’) are concatenated on the same line. The write() function simply takes its data and stores it in the file – it does not provide any formatting or extra characters to help delimit your data.
  13. You want to try and avoid writing files that concatenate all of your data into one long line of unintelligible text. This is bad practice since it can be very difficult – or impossible – to extract out your data later on. One way to separate data in a text file is by splitting out your data into multiple lines using the “\n” escape character. This allows you to store a single value on each line of your text file, which will make things a lot easier for you later on when you need to read your file and perform some kind of operation on the data contained within. Here’s an example program that we wrote in class that stores a username and password in a file – each item will be stored on its own line.
    # ask the user for their username
    # and password
    username = input("What's your username: ")
    password = input("What's your password: ")
    
    # store the username and password
    file_object = open("security.txt", "w")
    
    # send the information into the file
    file_object.write(username + "\n")
    file_object.write(password + "\n")
    
    # print a confirmation
    print ("Your account has been set up")
    
    # close the file
    file_object.close()
  14. You can read data contained inside a file once you have created a file object and have opened a file for reading using the read() function. Here’s an example:
    myvar = open("test.txt", "r”)
    alldata = myvar.read()
    print (alldata)
    myvar.close()
  15. The read() function extracts all data from a file as a string and returns a string. This string must be further processed before it can actually be used, usually through using the string “split” method.
  16. Formatting data that you store in an external file is up to you, and you can choose any layout convention you’d like.  However, many programmers design their files such that data is laid out across multiple lines (using a “\n” character) in order to separate important pieces of data.
  17. The readline() function can be used to read a single line of data (until the next “\n” character). The function returns a string that contains the requested information (including the “\n” character). Your file object keeps track of its position inside a file using a “read position” indicator – it will remember the last line you read using the readline() function and will let you read lines sequentially if you continually call the readline() function. Here’s an example:
    line1 = file_object.readline()
  18. You can also use the readlines() method to read all lines from a file object directly into a Python list.  Here’s an example:
    filelist = file_object.readlines()
    print (filelist)
    
    >> [ 'I'm on line 1!\n', 'I'm on line 2!\n' ]
  19. You can use the special “rstrip” function that is built into the String data type to remove certain characters. “rstrip” takes one argument – a string – and returns a string version of itself with all instances of the supplied character pattern removed. You can use the “rstrip()” function to extract all instances of the “\n” character by doing the following:
    for x in range(len(filelist)):
         filelist[x] = filelist[x].rstrip("\n")
  20. Here’s an example that puts all of this together — in this program we open up the “security.txt” file that we created earlier and extract the username and password stored inside. Then we ask the user for a username & password combo and test to see if the two are the same.
    # open up a connection to our file
    file_object = open("security.txt", "r")
    
    # grab the username & password as a list
    filelist = file_object.readlines()
    
    # remove the line breaks
    for x in range(len(filelist)):
         filelist[x] = filelist[x].rstrip("\n")
    
    # store the username and password in new variables to make
    # our program more "readable"
    username_from_file = filelist[0]
    password_from_file = filelist[1]
    
    # close the file
    file_object.close()
    
    # now that we have the username and password
    # ask the current user for their info
    # if it matches what's in the file we cna let them in
    # otherwise they can't log in
    username = input("username: ")
    password = input("password: ")
    
    # check to see if they match
    if username == username_from_file and password == password_from_file:
        print ("you're in!")
    else:
        print ("sorry, wrong info")
  21. When you are reading data from a file you will need to convert strings to ints or floats if you want to perform calculations on them. This is identical to what we have to do when using the input() function to ask the user to enter a string. Note that the int() and float() functions automatically remove line break characters for you. Here’s an example that opens up a text file called “testscores.txt” — this file contains the following information:
    craig
    100
    82
    74

    And here is how we can work with the file by treating the score values as integers:

    # open up the file
    file_object = open("testscores.txt", "r")
    
    # read in each line manually (without putting data into a
    # list first)
    studentname = file_object.readline()
    studentname = studentname.rstrip("\n")
    score1 = int(file_object.readline())
    score2 = int(file_object.readline())
    score3 = int(file_object.readline())
    
    # print out data
    print (studentname)
    
    average = (score1 + score2 + score3) / 3
    
    print ("average: ", average)
  22. You can also use repetition structures to write large amounts of data into a file. Here’s an example that writes out a series of price values that are entered by the user:
    # open up our file
    file_object = open("prices.txt", "a")
    
    keepgoing = True
    
    while keepgoing == True:
    
        # ask the user for a price
        price = float(input("give me a price: "))
    
        if price > 0:
    
            # store the price
            file_object.write(str(price) + "\n")
    
        else:
    
            # end the loop
            keepgoing = False
    
    # close the file
    file_object.close()
  23. Most times you won’t know how many lines of data exist in a text file. One strategy for dealing with these kinds of files is to use a while loop in combination with the readline() function. The readline() function will return an empty string (‘’) when it has reached the end of the file. Here’s an example that prints out the average price of the items entered by the previous program:
    # accum variables
    total = 0
    num_prices = 0
    
    # open up our file
    file_object = open("prices.txt", "r")
    
    # continually real from the file as long as there
    # is data to be read
    keepgoing = True
    
    while keepgoing == True:
    
        # read the next line from the file
        nextline = file_object.readline()
    
        # test to see if we have reached the end of the file
        if nextline == '':
            keepgoing = False
        else:
            nextline = nextline.rstrip("\n")
            nextline = float(nextline)
    
            # accum variables
            total = total + nextline
            num_prices += 1
    
            print (nextline)
    
    # print out summary
    print ("average: ", total / num_prices)
    
    # close the file
    file_object.close()
  24. Here are some additional practice problems that we went over in class. Problem #1 – Write a “matchmaking” program that asks the user to enter in their favorite color and their favorite food. Store the result in a text file.
    # open up the file
    file_object = open("match.txt", "w")
    
    # ask the user their fav color
    color = input("Fav color: ")
    
    # ask the user their fav food
    food = input("Fav food: ")
    
    # write the info to the file
    file_object.write(color + "\n")
    file_object.write(food + "\n")
    
    # close the file
    file_object.close()
  25. Problem #2 – Interface with your matchmaking text file and ask a second user for a favorite color and favorite food. Compare the results – if they get 0/2 questions correct, they are not a match! ½, they might be a match. 2/2, they are definitely a match!
    # get the info from the file that we will
    # be using for comparison purposes
    file_object = open("match.txt", "r")
    
    # get the fav food and fav color
    color = file_object.readline()
    food = file_object.readline()
    
    # get rid of the line breaks
    color = color.rstrip("\n")
    food = food.rstrip("\n")
    
    # close the file
    file_object.close()
    
    # ask the current user what their prefs are
    usercolor = input("What's your fav color? ")
    userfood = input("What your fav food? ")
    
    # compare
    points = 0
    
    if usercolor == color:
        points +=1
    if userfood == food:
        points +=1
    
    print (points/2, "percent match!")
  26. Problem #3 – Open up a text file named “drawing.txt” for read access. Using the turtle graphics library, extract the coordinates contained in the file and draw the resulting picture. Use the turtle.goto() function to move the turtle from point to point.
    import turtle
    
    turtle.setup(500,500,0,0)
    
    # open up the file
    file_object = open("drawing.txt", "r")
    
    # go through each line
    keepgoing = True
    
    while keepgoing == True:
    
        # read the x and y coordinate
        x = file_object.readline()
    
        # are we at the end?
        if x == '':
            keepgoing = False
        else:
            # read the y coordinate
            y = file_object.readline()
    
            # convert to floats
            x = float(x)
            y = float(y)
    
            # draw this to the screen
            turtle.goto(x,y)
    
    file_object.close()

    “drawing.txt”

    -120.0
    -95.0
    -76.0
    22.0
    -13.0
    185.0
    66.0
    24.0
    114.0
    -103.0
    24.0
    -103.0
    23.0
    -187.0
    -38.0
    -186.0
    -39.0
    -101.0
    -120.0
    -96.0

Day 25 Wrap Up

Today we covered the following topics:

  1. We discussed a new technique that allows us to “split” a string up into smaller parts so that we can “unpack” it into a list.  For example, consider the following string:
    x = “Craig,Professor,NYU”
  2. This string contains three pieces of data – a name, a title and an employer. The data has been “packaged” into a single string in a meaningful way (i.e. we know that the comma character separates the different data points)
  3. We can use a technique called “splitting” to “unpack” a string and extract its individual values. The trick is to isolate a “separator” character that can be used to delimit the data you want to extract. Here’s an example:
    mystring = “Craig,Professor,NYU”
    splitstring = mystring.split(“,”)
    print (splitstring)
    
    >> [‘Craig’, ‘Professor’, ‘NYU’]
  4. General Algorithm:
    1. Obtain a string that contains data organized in a certain way
    2. Split the string into a list
    3. Process the list using list mechanics
  5. We then wrote a program that processed a string that contained grade information and generated the average score for the student, the highest and lowest scores, and the average with the lowest score “dropped.”
    scores = "95,100,67,33,88"
    
    # convert scores into a list
    mylist = scores.split(",")
    
    # convert all values int he list to ints
    for x in range(len(mylist)):
        mylist[x] = int(mylist[x])
    
    total = 0
    
    # get info from list
    for x in mylist:
        total += x
    
    print ("Average:", total / len(mylist))
    print ("Best:", max(mylist))
    print ("Word:", min(mylist))
    
    # dropping the lowest
    newtotal = total - min(mylist)
    newaverage = newtotal / (len(mylist) - 1)
    
    print ("Average with dropped:", newaverage)
  6. Next we moved on to talk about how we can obtain data from outside of our Python programs. When writing a large program you generally won’t be “hard coding” data into your programs – most times you will be processing information that comes to you in the form of user input or via an external data source.
  7. One way to easily get information into Python from the “outside world” is to initiate a connection to the Web and obtain data via a URL.  Here is the general algorithm for doing this:
    1. Initiate a web connection to an external data source
    2. Obtain the data that exists at that location as a String
    3. “Clean up” the string into a meaningful form that your program can understand. This usually involves making sure that the character set used in the external file is one that your computer can handle.
    4. Parse your string into a list
    5. Process this list and generate output
  8. You can initiate web connections using the “urllib” module. Example:
    import urllib.request
    
    # where should we obtain data from?
    url = “http://www.nyu.edu”
    
    # initiate request to URL
    response = urllib.request.urlopen(url)
    
    # read data from URL as a string
    data = response.read().decode(‘utf-8’)
    print (data)
  9. Some notes of caution:
    1. The web is a volatile place – servers can go up and down at a moment’s notice, and your connection to the network may not always be available.
    2. You need to be prepared to deal with connection errors in your programs.
    3. You can use the try / except / else suite to help “catch” errors before they crash your program. For example:
      try:
           # initiate request to URL
           response = urllib.request.urlopen(url)
      except:
           print (“Looks like the request failed!”)
      else:
           # read data from URL as a string
           data = response.read().decode(‘utf-8’);
  10. Next we wrote an interactive “voting” application that lets users submit votes via SMS text messages.  For this project built a system that will did the following:
    1. Allow users to “vote” for their favorite Python language construct (“if”, “while”, “for”, “try”, etc)
    2. Voting will happen via SMS message
    3. Votes will be tabulated in real-time and displayed to the audience
  11. SMS Voting can be “outsourced” to a company that can handle text messages and deliver them to you as string data.  We used a free service called “TextMarks” to do this.  We set up a new keyword with TextMarks (the keyword “Python”) and users can text their vote to by doing the following:
    1. Text to:  4144
    2. Message:  Python *your vote*
    3. … where “*your vote*” is the piece of text you want to send to the voting application.
  12. Here is how we setup our keyword on their site – the important piece of this is where we want to redirect the data once it has been texted into the system.TextMarks Keyword SetupTextMarks Keyword Setup
  13. Next we wrote a small PHP program that was designed to “listen” for communication from TextMarks and store the result in a data file.
  14. Finally we have to process the data using Python.  Here is a how we did this in class:
    import urllib.request
    
    # where should we obtain data from?
    url = "http://www.yourwebsite.com/textmarks.txt" # same URL you used when setting up your keyword
    
    # initiate request to URL
    response = urllib.request.urlopen(url)
    
    # read data from URL as a string
    data = response.read().decode('utf-8')
    data = str.lower(data)
    
    # split the data on the line break
    splitdata = data.split("\n")
    
    # go through each message and pull out just the message part
    for message in splitdata:
    
        # split it again so we can get the phone number
        # and the message
        split2 = message.split(",")
    
        print (split2[1])

And here are some programs that we wrote in class that dealt with Strings, Lists and the Web:

# parsing test scores from a String

# define scores
scores = "95,100,67,33,88"

# split them up into a list
scores_list = scores.split(",")

# now we have to convert them to integers
for i in range(len(scores_list)):
    scores_list[i] = int(scores_list[i])

# now we can analyze the scores
print ("Highest score:", max(scores_list))
print ("Lowest score:", min(scores_list))
print ("Average score:", sum(scores_list) / len(scores_list))
print ("Average score with lowest dropped:", ( sum(scores_list)-min(scores_list) ) / ( len(scores_list)-1))
# ask the user to enter in numbers separated by commas

# get input
raw_input = input("Enter in a comma separated list of numbers between 0 and 100: ")

# split into a list
number_list = raw_input.split(",")

# convert to integers
for i in range(len(number_list)):
    number_list[i] = int(number_list[i])

# now analyze the numbers!

# ascending order
print ("Acending order")
for i in range(len(number_list)):
    print (number_list[i])

# decending order
print ("Decending order")
for i in range(len(number_list)-1, -1, -1):
    print (number_list[i])

# sum of the numbers
print ("Sum:", sum(number_list))

# frequency count
frequency = [0] * 101

# iterate over all numbers in the list
for number in number_list:

    # make a note that we have seen this number in our list
    frequency[ number ] += 1

# now print out the frequency list
for i in range(len(frequency)):

    # have we seen this number?
    if frequency[i] > 0:

        print (i, "has been seen", frequency[i], "times")
# morse code converter

# define the code
code = "0,-----,1,.----,2,..---,3,…--,4,....-,5,.....,6,-....,7,--...,8,---..,9,----.,A,.-,B,-...,C,-.-.,D,-..,E,.,F,..-.,G,--.,H,....,I,..,J,.---,K,-.-,L,.-..,M,--,N,-.,O,---,P,.--.,Q,--.-,R,.-.,S,...,T,-,U,..-,V,...-,W,.--,X,-..-,Y,-.--,Z,--.."

# split it apart
splitcode = code.split(",")

# ask the user for a word
word = str.upper(input("word:"))

# analyze the word character by character
for c in word:

    if c in splitcode:

        location = splitcode.index(c)
        print (c, splitcode[location+1])
# getting data from the web - hello, world!

# import the urllib library
import urllib.request

# where should we obtain data from?
url = "http://www.cnn.com"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

print (data)
# most popular boy and girl baby names

# import the urllib library
import urllib.request

# first get the boy names
url = "http://i5.nyu.edu/~cmk380/pythondata/boys_2010.txt"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

# split the data into a list
boynames = data.split("\n")

# now get the girl names
url = "http://i5.nyu.edu/~cmk380/pythondata/girls_2010.txt"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

# split the data into a list
girlnames = data.split("\n")

# now ask the user to enter in a name
testname = str.upper(input("Enter a name: "))

# see if we know about this name
if testname in boynames:

    # grab its location
    location = boynames.index(testname)

    print (testname, "is #", location+1, "on the list of most popular boy name!")

if testname in girlnames:

    # grab its location
    location = girlnames.index(testname)

    print (testname, "is #", location+1, "on the list of most popular girl name!")
# world series winners

# import the urllib library
import urllib.request

# get winning team data
url = "http://i6.cims.nyu.edu/~kapp/python/WorldSeriesWinners.txt"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

# split the data into a list
rawdata = data.split("\n")

# now we need to analyze the list to extract the winners

# we will need two arrays to keep track of winning teams and how many times they have won
winning_team  = []
winning_count = []

for team in rawdata:

    # do we know about this team yet?
    if team not in winning_team:

        # add it to the lists!
        winning_team.append(team)
        winning_count.append(0)

    # otherwise we do know about this team
    else:

        location = winning_team.index(team)

        # update value
        winning_count[location] += 1

# find the winner!
most_wins = max(winning_count)

best_team = winning_count.index(most_wins)

print ("The team with the most wins is", winning_team[ best_team ], "with", most_wins, "wins!")

 

Day 24 Wrap Up

Today we continued our discussion on lists.  Here’s an overview of what we covered:

  1. We started off by writing a simple program that lets a store manager calculate weekly salary figures for his or her employees.  Here’s the code we wrote:
    # total # of employees
    NUM_EMPLOYEES = 8
    
    # list to hold the employees
    hours = [0] * 8
    
    # get employee info for these employees
    for x in range(NUM_EMPLOYEES):
    
        # get hours worked
        print ("Employee #", (x+1), end='')
    
        temp = float(input(" hours worked: "))
    
        # put the hours into the list
        hours[ x ] = temp
    
    # ask for hourly rate
    rate = float(input("Hourly rate: "))
    
    # go through the list again and compute salary
    for x in range(NUM_EMPLOYEES):
    
        # compute salary
        salary = rate * hours[x]
    
        print ("Employee #", (x+1), "earned", salary)
  2. Next we wrote a larger program that acted as an “inventory management” system for a small grocery store.  We started off by creating a simple program that defined our inventory using three lists.  The position of an item in the ‘products’ list can be used to “look up” the price of the product and how many of those products we have available for sale.  Our first attempt at the program included a simple menu system that gave the user two choices – print out the available inventory for the store or quit the program.  Here’s the code:
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory or (Q)uit: "))
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
  3. Next we extended the program so that we could buy items. When we purchase an item we need to verify that (a) the item exists and (b) that we have enough of those items available for sale. Here’s the code:
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory, (B)uy or (Q)uit: "))
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
    
        # buy mode
        if mode == 'b':
    
            desired_item = str.lower(input("What product? "))
    
            # do we carry this item?
            if desired_item not in products:
    
                print ("Sorry, we don't carry this item.")
    
            # yes, we do carry this!
            else:
    
                # how many do they want to buy
                amount = int(input("How many? "))
    
                # figure out the location of this product in our list
                location = products.index(desired_item)
    
                # make sure we have enough of this stuff to sell
                if inventory[ location ] >= amount:
    
                    # we can make a sale!
                    print ("This will cost you", amount * prices[location])
    
                    # decrease available inventory so we don't sell more of this later on
                    inventory[ location ] -= amount
    
                else:
    
                    print ("Sorry, we don't have enough of those to sell you")
  4. Then we added in the ability to add new products into the system. This feature also makes sure that we aren’t adding duplicate products. Here’s the code:
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory, (B)uy, (A)dd or (Q)uit: "))
    
        # add mode
        if mode == 'a':
    
            # ask the user for a new product name
            newproduct = str.lower(input("New Product Name: "))
    
            # make sure we don't sell this already
            if newproduct in products:
                print ("Sorry, we already sell this")
    
            else:
    
                newprice = float(input("Price: "))
                newamount = int(input("Amount of items: "))
    
                # add the items to our database
                products = products + [newproduct]
                prices.append(newprice)
                inventory.append(newamount)
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
    
        # buy mode
        if mode == 'b':
    
            desired_item = str.lower(input("What product? "))
    
            # do we carry this item?
            if desired_item not in products:
    
                print ("Sorry, we don't carry this item.")
    
            # yes, we do carry this!
            else:
    
                # how many do they want to buy
                amount = int(input("How many? "))
    
                # figure out the location of this product in our list
                location = products.index(desired_item)
    
                # make sure we have enough of this stuff to sell
                if inventory[ location ] >= amount:
    
                    # we can make a sale!
                    print ("This will cost you", amount * prices[location])
    
                    # decrease available inventory so we don't sell more of this later on
                    inventory[ location ] -= amount
    
                else:
    
                    print ("Sorry, we don't have enough of those to sell you")
  5. Finally we added in the ability to remove products from the database.
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory, (B)uy, (A)dd, (R)emove or (Q)uit: "))
    
        # remove mode
        if mode == 'r':
    
            # ask the user for the product to remove
            removeproduct = str.lower(input("What product to remove: "))
    
            # make sure we own this product
            if removeproduct not in products:
    
                print ("Sorry, we don't carry that.")
    
            else:
    
                # figure out the location of this product
                location = products.index(removeproduct)
    
                # delete all of these slots from all lists
                del products[ location ]
                del prices[ location ]
                del inventory[ location ]
    
        # add mode
        if mode == 'a':
    
            # ask the user for a new product name
            newproduct = str.lower(input("New Product Name: "))
    
            # make sure we don't sell this already
            if newproduct in products:
                print ("Sorry, we already sell this")
    
            else:
    
                newprice = float(input("Price: "))
                newamount = int(input("Amount of items: "))
    
                # add the items to our database
                products = products + [newproduct]
                prices.append(newprice)
                inventory.append(newamount)
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
    
        # buy mode
        if mode == 'b':
    
            desired_item = str.lower(input("What product? "))
    
            # do we carry this item?
            if desired_item not in products:
    
                print ("Sorry, we don't carry this item.")
    
            # yes, we do carry this!
            else:
    
                # how many do they want to buy
                amount = int(input("How many? "))
    
                # figure out the location of this product in our list
                location = products.index(desired_item)
    
                # make sure we have enough of this stuff to sell
                if inventory[ location ] >= amount:
    
                    # we can make a sale!
                    print ("This will cost you", amount * prices[location])
    
                    # decrease available inventory so we don't sell more of this later on
                    inventory[ location ] -= amount
    
                else:
    
                    print ("Sorry, we don't have enough of those to sell you")
  6. Next we wrote a program that generate any number of bouncing graphical boxes on the screen. In this program we use lists to keep track of the object’s position, speed and color. The “trick” to the code below is in the “gameloop” function. We used a method called “turtle.ontimer()” to repeatedly call the same function over and over again (in this case every 30 milliseconds) – we run the function, and then when it completes we call the “turtle.ontimer()” method to schedule another calling of the function in 30ms.
    import turtle
    import random
    
    # ask the user for a number of objects
    numobjects = int(turtle.textinput("Number of objects", "Number of objects"))
    
    # create lists that store the x & y position for each box
    objects_x = []
    objects_y = []
    
    # create lists that store the x & y speed for each box
    objects_sx = []
    objects_sy = []
    
    # create a list that stores the color for each box
    objects_c  = []
    
    # load our lists with random data
    for x in range(numobjects):
    
        # randomize the x & y positions for each box
        objects_x.append(random.randint(-200, 200))
        objects_y.append(random.randint(-200, 200))
    
        # randomize the speeds for each box
        objects_sx.append(random.randint(-1, 1))
        objects_sy.append(random.randint(-1, 1))
    
        # randomize the color for each box - note that we are
        # using a "list inside a list" here - we call these "multidimensional
        # lists" in Python and "multidimensional arrays" in other languages
        objects_c.append([random.random(), random.random(), random.random()])
    
    # our "gameloop" function runs every 30ms and continually redraws the screen
    def gameloop():
    
        # clear the canvas and hide the turtle
        turtle.clear()
        turtle.hideturtle()
    
        # iterate through all of our objects
        # "x" will assume the index value for each item in the objects_x
        # list, which can be used as the index value for all of the other lists
        for counter in range(len(objects_x)):
    
            # pick up the pen
            turtle.penup()
    
            # go to the current object's location
            turtle.goto(objects_x[counter], objects_y[counter])
    
            # put down the pen
            turtle.pendown()
    
            # set the correct fill color & begin filling
            turtle.fillcolor(objects_c[counter][0], objects_c[counter][1], objects_c[counter][2])
            turtle.begin_fill()
    
            # draw a box
            for x in range(4):
                turtle.forward(20)
                turtle.right(90)        
    
            # stop filling when we are done drawing the box
            turtle.end_fill()
    
            # move the object based on its speed (stored in the speed lists)
            objects_x[counter] += objects_sx[counter]
            objects_y[counter] += objects_sy[counter]
    
            # bouncing - check to see if the object hit and edge and
            # react accordingly (nudge it back onto the screen and flip
            # its speed so it moves in the opposite direction)
            if objects_x[counter] > 200:
                objects_x[counter] = 199
                objects_sx[counter] *= -1
            if objects_x[counter] < -200:             
                objects_x[counter] = -199
                objects_sx[counter] *= -1         
            if objects_y[counter] > 200:
                objects_y[counter] = 199
                objects_sy[counter] *= -1
            if objects_y[counter] < -200:
                objects_y[counter] = -199
                objects_sy[counter] *= -1
    
        # once we have redrawn the screen we can force Python to update
        # all graphics
        turtle.update()
    
        # call this function again in 30ms
        turtle.ontimer(gameloop, 30)
    
    #### MAIN PROGRAM ####
    
    # set up our canvas
    turtle.setup(500,500,0,0)
    
    # tell Python not to draw to the screen - we will call the "update" method
    # when we need to draw (makes things a lot faster)
    turtle.tracer(0)
    
    # call our gameloop function to start up the program
    gameloop()
    
    # when the user clicks on the screen we can end the program
    turtle.exitonclick()

Day 23 Wrap Up

Today we continued our discussing lists and how to use them in Python. Here’s an overview of the new topics that we covered:

  1. We started off talking about the sort() method.  This method takes no arguments and will reorder your list elements so that they appear in ascending order. The list data type contains a related method called the list.reverse() method, which lets you reverse a list. It won’t sort it though, it simply flips the first element with the last element, the second element with the second to last element, etc. Here’s an example:
      mylist = [1,2,3,99]
      print (mylist)
      mylist.reverse()
      print (mylist)
    
      >> [1, 2, 3, 99]
      >> [99, 3, 2, 1]
  2. Next we talked about how to determine the location of an element in a list. We already covered how you can test to see if an item is in a list (using the ‘in’ operator) but we didn’t talk about how to find the position of the element in the list. You can do this by using the list.index() method. This method takes one argument (a value) and returns the integer position of element that contains that value in the list. If no element contains that value then it will raise an exception, so we generally use this method in conjunction with the “in” operator. Here’s an example:
    mylist = ['apple', 'pear', 'peach', 'cake']
    
    if 'banana' in mylist:
        location = mylist.index('banana')
        print ("banana is at position", location)
  3. We then used the list.index() method to write a program that let a user look up a product and its corresponding price. In order to do this we utilized two lists – one list held our product names and one held the product prices. We assumed that position 0 of the first list related to position 0 of the second list. Given this information we were able to write a product lookup system — here’s what we built:
    products = ['fish', 'meat', 'cake']
    prices   = [1.99, 2.99, 3.99]
    
    # get a name from the user
    name = input("Give me a name: ")
    
    # do we have this product?
    if name in products:
    
        # get its position so we can get its price
        location = products.index(name)
    
        # print out the price
        print (name, 'costs', prices[location])
  4. Next we talked about two different ways in which we could remove an item from a list. The first method removes an element based on its content. For example, if you wanted to remove a list element that contains the word ‘cat’, you could do the following
    mylist = ['cat', 'dog', 'pig']
    mylist.remove('cat')
  5. This method will only remove the first instance of the value ‘cat’ from the list. If there were multiple elements that contained the word ‘cat’ the subsequent instances would be unaffected.
  6. The other method to remove an element is to remove it by position. You can use the “del” command to tell Python to completely delete an element from a list that occupies a particular position. Here’s an example:
    mylist = ['cat', 'dog', 'pig']
    del mylist[1]
    print (mylist)
    
    >> ['cat', 'pig']
  7. We then used the “del” command to update our price lookup system to function as a product deletion routine that deletes both a product and its corresponding price from two different lists.
    products = ['fish', 'meat', 'cake']
    prices   = [1.99, 2.99, 3.99]
    
    print (products)
    print (prices)
    
    # find out where the string 'meat' is in the first list
    meat_location = products.index('meat')
    
    # remove location from both lists
    del products[meat_location]
    del prices[meat_location]
    
    print (products)
    print (prices)
  8. Python has two built in functions that let you get the highest and lowest values in a list. They are called “min” and “max” – here’s an example:
    prices = [3.99, 2.99, 1.99]
    biggest = max(prices)
    smallest = min(prices)
    print (smallest, 'up to', biggest)
  9. Next we wrote a “weight loss” program that used a list to keep track of weight values over the course of a week:
    # how many days?
    numdays = int(input("How many days? "))
    
    # 7 days worth of values
    weight = [0] * numdays
    
    print (weight)
    
    # get weight values
    counter = 0
    while counter < len(weight):
    
        # get this days weight
        w = int(input("Weight for today: "))
    
        # put the weight into the list
        weight[ counter ] = w
    
        # increase counter to go to the next day
        counter += 1
    
    print (weight)
    
    print ("Weight on first day:", weight[0])
    print ("Weight on the last day:", weight[len(weight)-1])
    print ("Change from first to last", weight[0] - weight[len(weight)-1])
    print ("Average weight:", sum(weight) / len(weight))
    print ("Highest weight:", max(weight))
    print ("Lowest weight:", min(weight))
  10. We discussed a new technique that allows us to “split” a string up into smaller parts so that we can “unpack” it into a list.  For example, consider the following string:
    x = “Craig,Professor,NYU”
  11. This string contains three pieces of data – a name, a title and an employer. The data has been “packaged” into a single string in a meaningful way (i.e. we know that the comma character separates the different data points)
  12. We can use a technique called “splitting” to “unpack” a string and extract its individual values. The trick is to isolate a “separator” character that can be used to delimit the data you want to extract. Here’s an example:
    mystring = “Craig,Professor,NYU”
    splitstring = mystring.split(“,”)
    print (splitstring)
    
    >> [‘Craig’, ‘Professor’, ‘NYU’]
  13. General Algorithm:
    1. Obtain a string that contains data organized in a certain way
    2. Split the string into a list
    3. Process the list using list mechanics
  14. We then wrote a program that processed a string that contained grade information and generated the average score for the student, the highest and lowest scores, and the average with the lowest score “dropped.”
    scores = "95,100,67,33,88"
    
    # convert scores into a list
    mylist = scores.split(",")
    
    # convert all values int he list to ints
    for x in range(len(mylist)):
        mylist[x] = int(mylist[x])
    
    total = 0
    
    # get info from list
    for x in mylist:
        total += x
    
    print ("Average:", total / len(mylist))
    print ("Best:", max(mylist))
    print ("Word:", min(mylist))
    
    # dropping the lowest
    newtotal = total - min(mylist)
    newaverage = newtotal / (len(mylist) - 1)
    
    print ("Average with dropped:", newaverage)
  15. Next we moved on to talk about how we can obtain data from outside of our Python programs. When writing a large program you generally won’t be “hard coding” data into your programs – most times you will be processing information that comes to you in the form of user input or via an external data source.
  16. One way to easily get information into Python from the “outside world” is to initiate a connection to the Web and obtain data via a URL.  Here is the general algorithm for doing this:
    1. Initiate a web connection to an external data source
    2. Obtain the data that exists at that location as a String
    3. “Clean up” the string into a meaningful form that your program can understand. This usually involves making sure that the character set used in the external file is one that your computer can handle.
    4. Parse your string into a list
    5. Process this list and generate output
  17. You can initiate web connections using the “urllib” module. Example:
    import urllib.request
    
    # where should we obtain data from?
    url = “http://www.nyu.edu”
    
    # initiate request to URL
    response = urllib.request.urlopen(url)
    
    # read data from URL as a string
    data = response.read().decode(‘utf-8’)
    print (data)
  18. Some notes of caution:
    1. The web is a volatile place – servers can go up and down at a moment’s notice, and your connection to the network may not always be available.
    2. You need to be prepared to deal with connection errors in your programs.
    3. You can use the try / except / else suite to help “catch” errors before they crash your program. For example:
      try:
           # initiate request to URL
           response = urllib.request.urlopen(url)
      except:
           print (“Looks like the request failed!”)
      else:
           # read data from URL as a string
           data = response.read().decode(‘utf-8’);

Day 22 Wrap Up

Today we began discussing the idea of “lists” in Python. Here’s an overview of what we covered:

  1. So far we have been working with variables, which can be thought of as little “buckets” that can hold a particular piece of data. Variables are incredibly useful, but they can only hold one piece of data at a time. Example:
    x = 5
    y = 5.0
    z = ‘hello’
    q = True
  2. Each of the above variables will hold a single value – an int, float, String or Boolean. There are times when we need to keep track of multiple pieces of data at the same time, and a single variable is limited to holding just one thing at a time.
  3. “Lists” are a built-in data type in Python that are considered a “sequence” objects. Sequence objects have the ability to hold multiple pieces of data at the same time, and we can use a single sequence variable to hold any number of values. In most programming languages we call these “arrays,” but in Python we call them “lists.”
  4. You can create a list in Python by using bracket notation. Example:
    my_list = [1, 2, 3]
  5. The above code will create a new list in Python that holds three integers – 1, 2 and 3 – in that order. Think of a list as a “book” that holds a series of sheets of paper (variables)
  6. Lists can contain any data type that we have covered so far.
    my_list = [‘Craig’, ‘John’, ‘Chris’]
  7. Lists can also mix data types.
    my_list = [‘Craig’, 5.0, True, 67]
  8. You can print the value of a list using the print() function.
    print (my_list)
    >> ['Craig', 5.0, True, 67]
  9. You can also create lists using the range() function. For example, to create a list of all even numbers between 0 and 100 you can do the following:
    even_numbers = list(range(0,100,2))
  10. You can use the repetition operation (“*”) to ask Python to repeat a list, much like how you would repeat a string.
    my_list = [1, 2, 3] * 3
    print (my_list)
    >> [1, 2, 3, 1, 2, 3, 1, 2, 3]
  11. You can use the concatenation operation (“+”) to ask Python to combine lists, much like how you would combine strings.
    my_list = [1, 2, 3] + [99, 100, 101]
    print (my_list)
    >> [1, 2, 3, 99, 100, 101]
  12. In a book you can reference a page by its page number. In a list you can reference an element by its index number. Indexes always start at the number zero and increase to the length of the list – 1.
    my_list = [‘Craig’, ‘John’, ‘Chris’]
    print (my_list[0])
    
    >> Craig
  13. Lists are “mutable,” which means that they can be changed once they have been created.
    my_list = [1, 2, 3]
    print (my_list)
    >> [1,2,3] 
    
    my_list[0] = 99
    print (my_list)
    >> [99,2,3]
  14. You can use a while loop to iterate through a list.
    my_list = ['Craig', 'John', 'Chris’]
    index = 0while index < 3:
       print (index, "is", my_list[index])
       index += 1
  15. You will raise an exception if you attempt to access an element outside the range of a list.
    my_list = ['Craig', 'John', 'Chris’]
    print (my_list[4]) # Index doesn’t exist!
  16. You can use the len() function to determine the size of a list. The len() function can take a list as an argument and will return the integer value of the size of the list.
    my_list = ['Craig', 'John', 'Chris’]
    index = 0
    while index < len(my_list):
       print (index, "is", my_list[index])
       index += 1
  17. Here’s an example:
    mylist = [1.99, 2.99, 3.99, 4.99, 5.99]
    
    # loop through list
    counter = 0
    
    while counter < len(mylist):
    
        print (mylist[counter], mylist[counter] *1.07)
    
        counter += 1
  18. You can also use a for loop to iterate through a list. When you do this the target variable of your loop assumes each value of each element of the list in order.
    my_list = [1,2,3]
    for number in my_list:
       print (number)
    
    >> 1
    >> 2
    >> 3
  19. Since you cannot access an element outside of the range of a list it is sometimes necessary to set up a correctly sized list before you begin working with it.
    # create a list of 7 zeros
    daily_sales = [0] * 7
  20. We then wrote a program that uses a list with 7 empty elements to store sales figures for a week.
    # create an empty list filled with zeros
    mylist = [0] * 7
    
    # loop through 7 times
    for x in range(7):
    
        # get sales figure for this day
        sales = float(input("give me a figure: "))
    
        # store the figure at the next spot in the list
        mylist[x] = sales
    
    print (mylist)
  21. Sometimes you need to extract multiple items from a list. Python contains some built in functions that make it easy for you to “slice” out a portion of a list.
    list_1 = ['zero', 'one', 'two', 'three', 'four', 'five’]
    list_2 = list_1[1:3]
    print (list_1)
    print (list_2)
    >> ['zero', 'one', 'two', 'three', 'four', 'five’]
    >> ['one', 'two']
  22. If you omit the start_index in a slice operation, Python will start at the first element of the list. If you omit the end_index in a slice operation, Python will go until the last element of the list. If you supply a third index, Python will assume you want to use a step value. This works the same as the step value you would pass to the range() function.
  23. You can easily find a particular item in a list by using the “in” operator. The “in” operator will return a Boolean value that indicates whether the item exists somewhere in the list.
    my_list = ['pie', 'cake', 'pizza’]
    if 'cake' in my_list:
       print ("I found cake!”)
    else:
       print ("No cake found.")
  24. You have already seen a few ways in which you can add items to lists. You could repeat the list using the “*” operator, or you could concatenate the list using the “+” operator. Another way to add items to a list is to use the “append” method.The append method is a function that is built into the list datatype which allows you to add items to the end of a list.
    mylist = [‘Christine’, ‘Jasmine’, ‘Renee’]
    mylist.append('Kate')
    print (mylist)
    >> [‘Christine’, ‘Jasmine’, ‘Renee’, ‘Kate’]
  25. You can also have Python sort items in a list using the sort() method.
    my_list = ['pie', 'cake', 'pizza’]
    my_list.append('apple’)
    print (my_list)
    my_list.sort()
    print (my_list)
    
    >> ['pie', 'cake', 'pizza', 'apple’]
    >> ['apple', 'cake', 'pie', 'pizza']

Day 21 Wrap Up

Today we continued our discussion of string manipulation.  Here’s what we covered:

  1. We started off by reviewing how to handle exceptions and basic string manipulation concepts.  See the blog posting for Day 19 for more information on these topics.
  2. We wrote a “Palindrome Tester” program in class to review basic string manipulation concepts.  Just as a review, a palindrome is a word that reads the same backwards as it does forwards. Here’s the code:
    # ask the user for a word
    word = input("Give me a word: ")
    
    # convert it to lowercase - makes things
    # easier when we need to compare the word
    # later on
    word = str.lower(word)
    
    # construct an empty string to hold the
    # backwards version of the word
    backwards = ""
    
    # iterate over each character in the string
    # begin at the end of the string ( len(word) - 1)
    # stop at the beginning of the string (0, but we
    # go to -1 since the range function never reaches
    # its end point)
    # skip -1 each time
    for x in range(len(word)-1, -1, -1):
        # add the character we are looking at to our
        # "backwards" string
        backwards = backwards + word[x]
    
    # compare the two strings - if they are the
    # same then we have a palindrome!
    if word == backwards:
        print ("Palindrome!")
    else:
        print ("Sorry, not a palindrome!")
  3. We then moved on to talk about “string slicing” which is a technique that lets you extract a span of characters from a string into a new string.  You can specify which characters you want to extract using index notation. For example, the following program will extract the first four characters from the string full_name:
    full_name =“John Smith”
    first_name = full_name[0:4]
    print (first_name)
    
    >> John
  4. When you ask Python to slice a string you need to use bracket notation to specify the index range of the characters you wish to extract.  The syntax for this notation is as follows:
    substring = bigstring[start:end:step]
  5. You must supply at least a start or an ending index value when slicing a string, but the step value is always optional.  Substrings created in this way contain all characters starting at the start value specified and continue up to (but do not include) the ending value.
  6. Omitting a starting or ending index value will cause Python to assume you want to start at the beginning of the string (if you omit a start value) or you want to continue slicing to the end of the string (if you omit the end value).  This should remind you a lot of the range function!
  7. You can also specify negative indexes in your slices.  This will allow you to grab a substring from the end of the string rather than from the beginning. For example, this slice will grab the last 5 characters from a string:
    phrase = "This is a test!"
    lastfive = phrase[-5:]
  8. We did some in-class slicing to demonstrate how this works.  Here’s the code we covered:
    word = "Superman sings in the shower."
    
    print (word[0:8])   # Superman
    print (word[9:14])  # sings
    print (word[:5])    # Super
    print (word[9:])    # sings in the shower.
    print (word[-7:])   # shower.
    print (word[0:len(word):3]) # Seasgit or
  9. Next we used string slicing to write a program that would generate NYU Net ID account names for incoming students.  Here are the specificaitons for this program:

     You just accepted a position at NYU’s ITS department and your first task is to write a program that generates student Net IDs for all incoming freshmen.  Net IDs are generated as follows:

    * The first two characters of a student’s first name

    * The first two characters of a student’s last name

    * The last three characters of a student’s N#

    Write a program that asks the user for these three pieces of information (first name, last name and N#) and generate their Net ID

    # get the student's first name
    first = input("First name: ")
    
    # get the student's last name
    last = input("Last name: ")
    
    # get the student's N number
    nnum = input("N Number: ")
    
    # extract the specified characters
    first_two_first = first[0:2]
    first_two_last  = last[0:2]
    last_three_nnum = nnum[-3:]
    
    # make a net ID
    net_id = first_two_first + first_two_last + last_three_nnum
    
    print (net_id)
  10. You can test to see if a substring exists inside of another string by using the “in” operator. For example:
    word = "Jackson James John Chris Tom”
    if "Chris" in word:
         print ("found him!”)
    else:
         print ("can't find Chris")
  11. When you construct an expression with the “in” operator the result will evaluate to a Boolean.  You can also test to see if a string is not in another string by using the “not” keyword in your expression. For example:
    word = "Jackson James John Chris Tom”
    if ”Johnny" not in word:
         print (”No Johnny!”)
    else:
         print (”Johnny is here!")
  12. We illustrated this concept by writing a program that did the following:

    Write a program that asks a user for a username and a password.  Ensure that the user doesn’t use their username as part of their password.

    # obtain a username and password from the user
    username = input("Username: ")
    password = input("Password: ")
    
    # make sure the username was not used in the password
    if username in password:
        print ("That's not secure at all.")
    
    else:
        print ("Password OK")
  13. Next we discussed the idea of a “method,” which can be thought of as a function that belongs to an “object”, and performs some operation on that object. The general syntax for calling a method on an object looks a lot like a function call, with the notable exception that the method call generally does not need to pass an argument to the function specifying which piece of data is being operated on.  Syntax:
    stringvariable.method(arguments)
  14. Strings have a number of built in methods that you can use right away.  For example, there exist a series of string testing methods allow you to determine if certain patterns existing within a given string variable. For example:
    word = '1234’
    if word.isdigit() == True:
         print ("All chars in word are digits!”)
    else:
         print ("Not all chars in word are digits!”)

    In the above example we are calling the “isdigit” method on the string variable “word”. This method returns True if all characters contained in this string are numeric digits (0-9) and False if not.

  15. Here are a list of string testing methods that we covered in class:
    Method     Output
    isalnum()  True if all characters are alphanumeric
    isalpha()  True if all characters are alphabetic
    isdigit()  True if all characters are digits
    islower()  True is all alpha characters are lower
    isspace()  True if all characters are “whitespace”
    isupper()  True if all alpha characters are upper
  16. We used some of these string testing methods to write a basic “password validator” that is based on the following specification:

    Write a program that asks a user to supply a password.  Determine if the password they supplied is valid. Valid passwords are as follows:
    * Contain only alpha numeric characters
    * Contain no spaces
    * Contain at least one uppercase character
    * Contain at least one lowercase character

    # get a password from the user
    password = input("Password: ")
    
    # if the password is made up of only alpha numeric chars
    if password.isalnum() == True:
    
        print ("password is alpha numeric!")
    
        # next check to see if it has uppercase
        # strings
    
        # go through each character
        # keep track of how many uppercase we find
        numupper = 0
    
        for c in password:
    
            # is this uppercase?  if so, increase
            # our accum variable
            if c.isupper() == True:
                numupper += 1
    
        # when we are all done we can see if
        # we have any uppercase characters
        if numupper > 0:
    
            print ("you have at least 1 uppercase in this password!")
    
        else:
            print ("you have no uppercase in this passwrod")
    
    else:
    
        print ("password contains other characters!")
  17. Programs often need to perform search and replace functions on data.  You can find whether a string exists inside another string by using the find() method – for example:
    word = "Like finding a needle in a haystack!”
    location = word.find("needle”)
    print (location)

    The find() method will return the index of the first occurrence of a substring within a string.  If the find() method cannot find the desired substring it will return the integer value -1. We demonstrated how this can be used by writing the following program:

    “He Who Shall Not Be Named”
    Write a program that asks a user to type in a message..  If they use the word “voldemort” anywhere in their message you should warn them to be more careful!

    phrase = input("Give me a phrase: ")
    
    # see if "voldemort" is in the phrase
    location = phrase.find("voldemort")
    
    # if we found it at a postitive location
    # then we can warn the user
    if location > 0:
    
        print ("Don't say it out loud!")

    We also used the “replace” method to replace a string with another string. Here’s the “his” -> “her” pronoun switcher we wrote in class:

    word = "Pat said that his score on his history test was 86"
    
    # replace all his with her
    # note - there is a leading and trailing space in both
    # strings below.  This allows us to replace " his " and not
    # "history"
    newword = word.replace(" his ", " her ")
    
    print (newword)