Introduction to Python

davidbpython.com




In-Class Exercises, Session 8



PLEASE REFER to pythonreference.com for syntax to follow in coding these Exercises

 

REVIEW: FUNCTION ARGUMENTS AND RETURN VALUES

 
Ex. 8.1 Write a function that takes two arguments and returns one value.

Define function 'add' that takes two arguments for int values, adds them together and returns the sum. Note: you must not use 'a', 'b', 'x' or 'y' inside the function

# your function definition here

a = 5
b = 10
val1 = add(a, b)
print(val1)                     # 15

x = 10
y = 100
val2 = add(x, y)
print(val2)                     # 110
Expected Output:
15
110
 
Ex. 8.2 Write a function with one argument and one return value.

Define function 'get_half' that takes one argument, divides the value by two, and returns the halved value. Note: you must not use 'a' or 'b' inside the function, and you must not print inside the function -- value must be returned.

# your function code here

a = 5
x = get_half(a)
print(x)           # 2.5

b = 100
y = get_half(b)
print(y)           # 50.0
Expected Output:
2.5
50.0
 
Ex. 8.3 Write a function with one argument and two return values.

Define function 'double_and_halve' that takes one numeric argument, and returns the value doubled as well as halved. Note: you must not use 'a' or 'b' inside the function, and you must not print inside the function - values must be returned

# your function def here

a = 50

d, h = double_and_halve(a)

print(d)                # 100
print(h)                # 25.0
Expected Output:
100
25.0
 
Ex. 8.4 Write a function with two arguments and one return value.

Define function 'get_sum' that takes two numeric arguments and returns the values summed. Note: you must not use 'x' or 'y' inside the function, and you must not print inside the function - value must be returned.

# your function def here

x = 50
y = 25

z = get_sum(x, y)
print(z)           # 75
Expected Output:
75
 

LAB 1

 
Ex. 8.5 Write a function with two arguments and one return value.

Define function get_diff() that takes two numeric arguments and returns the difference between the two. Note: you must not use 'x' or 'y' inside the function, and you must not print inside the function - value must be returned.

# your function def here

x = 50
y = 10

z = get_diff(x, y)         # int, 40
print(z)                   # 40
 
Ex. 8.6 Write a function with two arguments and one return value.

Define function get_quotient() that takes two numeric arguments, divides the first by the second, and returns the result.

# your function def here



aa = 90
bb = 10

c = get_quotient(aa, bb)   # float, 9.0
print(c)                   # 9.0
 
Ex. 8.7 Write a function with one argument and one return value.

Define function get_lower() that takes a string argument and returns the value lowercased.

# your function def here



x = 'WELCOME TO THE WORLD.'
y = get_lower(x)              # str, 'welcome to the world.'

print(y)                      # welcome to the world.
 
Ex. 8.8 Write a function with one argument and one return value.

Define function get_split() that takes a string argument for a comma-separated CSV string line and returns a list of values from the split.

# your function def here



line = 'this,that,other'

items = get_split(line)        # list, ['this', 'that', 'other']

print(items)                   # ['this', 'that', 'other']
 
Ex. 8.9 Write a function with one argument and one return value.

Define function get_last_item() that takes a string argument for a comma-separated CSV string line and returns just the last comma-separated item from the line.

# your function def here


line = '2017-09-03,Alpha Corp.'

last = get_last_item(line)        # str, 'Alpha Corp.'

print(last)                       # Alpha Corp.
 
Ex. 8.10 Write a function that raises an exception.

The function below should detect if an incorrect state name is passed to it, and raise a ValueError exception if it is.

def get_tristate_pop(state):
    states = {'ny': 19.45, 'nj': 8.88, 'ct': 3.57}

    # if state is not in the states dict, please
    # raise a ValueError exception with below message


    return states[state]


popny = get_tristate_pop('ny')       # 19.45
print(popny)

popca = get_tristate_pop('ca')       # ValueError:
                                     # state "ca" not found
print(popca)
 

FILE INSPECTIONS

 
Ex. 8.11 Show the present/current working directory.

This is the one from which you are running your Python program. In most cases (but not always) it is the same as the location of the file you are running. Use os.getcwd(). Remember to import os.

 
Ex. 8.12 List a directory.

Loop through the warmup_exercises/ directory list, printing each item. Use os.listdir(). (Note: don't forget to import os.)

import os

this_dir = '../warmup_exercises'
Expected Output:
warmup_8.3.py
warmup_8.6.py
warmup_8.2.py
warmup_8.5.py
warmup_8.1.py
warmup_8.4.py

(your file output order may be different)

 
Ex. 8.13 Build a filepath.

Given the below directory name and filename, build a complete filepath. Use os.path.join()

import os

dirname = '../warmup_exercises/'

filename = 'warmup_1.1.py'

Note that on Windows computers, os.path.join() will insert a backslash instead of a forward slash. This completed path is still valid.

Expected Output:
../warmup_exercises/warmup_1.1.py
 
Ex. 8.14 Check to see if an entry is a file.

Given the below filename, confirm that the entry is a file by printing is a file!. Use os.path.isfile().

import os

fname = '../pyku.txt'
Expected Output:
is a file!
 
Ex. 8.15 Check to see if an entry is a directory.

Given the below directory name, confirm the entry is a directory by printing is a directory!. Use os.path.isdir().

import os

dirname = '../shakespeare'
Expected Output:
is a directory!
 
Ex. 8.16 Get the size of a file.

Given the below filepath, check the size of the file. Use os.path.getsize().

import os

fname = '../pyku.txt'
Expected Output:
80
 
Ex. 8.17 (running from command line only) Read from sys.argv.

Running the below code from the command line, print the value of sys.argv. Then, run the code again with the following 3 "words" on the same line when executing the script: greetings filesystem arguments.

import sys

print(sys.argv)
Expected Output:
['inclass_X.py', 'greetings', 'filesystem', 'arguments']
 

REVIEW: TRAPPING EXCEPTIONS -- TRY/EXCEPT

 
Ex. 8.18 Trap a "bad index" error. The below code takes user input and converts to int, then attempts to read the list item at that index value. A correct index (0-3) should allow you to print the value at that index. An incorrect index (>3) should raise an exception. Once you have observed this, use a 'try/except' statement to trap the exception and instead print 'no value at that index'. Make sure to put your 'try' block around just one line of code.
x = ['a', 'b', 'c', 'd']

uidx = int(input('please enter an index:  '))

print(x[uidx])
Expected Output:
please enter an index:  4
no value at that index
 
Ex. 8.19 Trap a "bad value" error.

Since the input line also converts to int an input value that is not all digits should also raise an exception. Once you have observed this, use a 'try/except' statement to trap the exception and instead print 'numbers, only please'. Make sure to put your new 'try' block around just one line of code. Also, add an exit() to the except: block so the code doesn't continue executing past that line. (Note that in Jupyter Notebook you must use sys.exit() and that Jupyter will respond as if this is an error.)

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

uidx = int(input('please enter an index:  '))

try:
    print(x[uidx])
except IndexError:
    print('no value at that index')
Expected Output:
please enter an index:  hello
numbers only, please
 
Ex. 8.20 Combine traps. Place the try: block around both the input line and the list index line. Now move your except: blocks so that they follow one another consecutively. Test the program both with a bad integer value and a bad index, to see that either exception can be handled.
x = ['a', 'b', 'c', 'd']

try:
    uidx = int(input('please enter an index:  '))
except ValueError:
    print('numbers only, please')

try:
    print(x[uidx])
except IndexError:
    print('no value at that index')
Sample program run:
please enter an index:  4
no value at that index
Sample program run:
please enter an index:  hello
numbers only, please
 
Ex. 8.21 Trap a "missing file" error. The below code attempts to open a file specified by the user's input. An existing filename should allow the program to show the length of the file in bytes. A non-existent filename should raise an exception. Once you have observed this, use a 'try/except' statement to trap the exception and instead print 'file does not exist'. Make sure to put your 'try' block around just one line.
# existing filename:  ../pyku.txt

import os

uifn = input("enter filename to see size in bytes: ")

print(f"this file's length is {os.path.getsize(uifn)}")
Expected Output:
enter filename to see size in bytes: ../pyku.txt
this file's length is 80
 
Ex. 8.22 Trap a "bad directory" error.

The below code attempts to list a directory specified by the user's input. An existing directory should allow the program to loop through the items in that directory. A non-existent directory should raise an exception. Once you have observed this, use a 'try/except' statement to trap the exception and instead print 'directory not found'. Make sure to put your 'try' block around just one line, and make sure to add an exit() to the end of the except: block so the program doesn't continue if the directory can't be found. (Note that in Jupyter Notebook you must use sys.exit() and that Jupyter will respond as if this is an error.) Test for both correct directory (shown at top) and incorrect directory.

# existing directory name:  ../shakespeare

import os

uidn = input("name of directory to list: ")

uidpath = os.path.join('..', uidn)

items = os.listdir(uidpath)

for item in items:
    print(item)
Expected Output:
directory not found
 
Ex. 8.23 Command line only: Identify error type and trap exception for missing command line argument.
Suppose that the user has provided arguments at the command line resulting in sys.argv having the list values, as shown:
command line argument:  1927

sys.argv value:          ['inclass_X.X.py', '1927']

The program currently reads and prints the 1st command line argument ('1927', i.e. 2nd item of the list). Now run the program without any argument and see what exception results. Then use a try/except to trap the error if it occurs. If it does, print 'please enter an argument'.

import sys

arg = sys.argv[1]

print('argument is {arg}')
Expected Output:
please enter an argument
 

LAB 2

 
Ex. 8.24 List a directory and show full path to each file.

Starting with the below directory, list the directory's contents, joining each entry to the directory name so it becomes a full path to the directory.

dirname = '../testdir'
Expected Output:
../testdir/file2.txt
../testdir/file3.txt
../testdir/file1.txt
../testdir/testdir3
../testdir/testdir2

Note the above results may be in a different order on your system. Also, you may see additional entries .DS_Store, .Trashes or other "hidden" files starting with a period.

 
Ex. 8.25 List a directory and show file sizes.

Extend the previous solution by printing the file size of each file or directory. Use a comma between varaible names to print two values on the same line, e.g. print(this, that).

import os

dirname = '../testdir'

items = os.listdir(dirname)

for name in items:
    fpath = os.path.join(dirname, name)
    print(fpath)
Expected Output:
../testdir/file2.txt 752
../testdir/file3.txt 200
../testdir/testdir3 160
../testdir/testdir2 128
 
Ex. 8.26 List a directory and identify file or directory.

Modify the previous solution so that the program identifies each listing as either a file or directory.

import os

dirname = '../testdir'

entries = os.listdir(dirnames)

for name in entries:
    fpath = os.path.join(dirname, name)
    print(fpath)
Expected Output:
../testdir/file2.txt:  file
../testdir/file3.txt:  file
../testdir/testdir3:  dir
../testdir/testdir2:  dir

In the above example, I am using an f'' strings to print the colon and file or dir after each entry.

 
Ex. 8.27 See if a file exists as a file; if not, show present working directory.

Take input for a filepath; check to see if the file exists. If not, show the present working directory.

filename = input('please enter a filename: ')
If the file can't be opened, we should see a message like this one:
Error:  "thisfile.txt" does not exist.  Present working directory is
/Users/david/Downloads/session_08_files_dirs_stdout/inclass_exercises
 
Ex. 8.28 A function to generate a file listing.

Write a function get_listing() that takes a directory name and returns the list of entries in that directory. If the directory name is not a valid directory, have the function raise a NotADirectoryError exception.

# your function def here



# this should succeed
listing = get_listing('.')      # list, files and dirs in this dir
print(listing)
print()


# this should raise NotADirectoryError exception
listing = get_listing('wrong')
      # NotADirectoryError:  directory "wrong" could not be opened
 
Ex. 8.29 A function to process command line arguments (test from command line only).

Write a function get_args() that reads from sys.argv and returns the 2nd through last items in the sys.argv list (i.e., a slice omitting the first item). If no arguments are passed at the command line, the function will return an empty list.

import sys

# your function def here



args = get_args()          # returns a list with 2nd through
                           # last items in sys.argv
print(args)

You must of course run this program from the command line and pass arguments at the command line to see this program work properly.

 
Ex. 8.30 Raise an exception if input is correct (command line only).

Extend the previous solution to accept an argument, an integer. The integer will specify how many arguments should be in sys.argv (not counting the initial filename argument of course). If there are not the indicated number of arguments, the function will raise a ValueError exception.

import sys

def get_args():             # add argument here
    args = sys.argv[1:]
    # add the size test and exception raising here

    return args


args = get_args(2)
Expected Output:
# if no arguments are passed
args = get_args(2)              # ValueError('must pass 2 cmd args')


# if two arguments are passed (this that)
args = get_args(2)              # list, ['this', 'that']
 

WRITING TO FILES

 
Ex. 8.31 Write to a file.

Open a file for writing (using a new filename such as newfile.txt and the additional argument 'w') and write some text to it (using the file .write() method. After running the program/cell, open the file and look at it to see it has been written to (make sure to call .close() on the file or you may not see the written text.)

 
Ex. 8.32 Write lines to a file, including newlines.

Open a file for writing (using a new filename) and write 3 lines to it (they can be as simple as 'line1', 'line2' and 'line3'. Add the newline character to each line. After running the program/cell, open the file and look at it to see it has been written to (make sure to call .close() on the file or you may not see the written text.)

 
Ex. 8.33 Append to an existing file.

Open the file you created previously for appending (using the 'a' argument instead of 'w') and write one or more additional lines to it. Again, add the newline character to each line. After running the program/cell, open the file and look at it to see it has been written to (make sure to call .close() on the file or you may not see the written text.)

 
[pr]