# Exam 2 Review¶

Here are all the sample problems you should study before you take the second exam. There might be a few problems we discussed in class that are not included in the notes. You should look them up, either in the text, or by doing a bit of research.

## Python Functions¶

• We (jokingly) said that a “function” is a box with a troll in it. Why did I make that critter a troll? (Why is it important not to look inside the box when you use a function?)

• What are the most important parts of a function you need to know to properly use one?

• Write a simple function named “cuber” that takes a single parameter and returns the cube of that parameter.

• What happens if you call a function that returns a value without capturing the return value? Something like this:

sqrt(x)

• Why is it consider bad programming to write a function that uses global variables?

## More Functions¶

• Even though it is considered bad form to use variables defined outside of a function, occasionally we do that. What must we do to allow the function to modify a variable outside of the function body?
• What do we mean when we talk about the “scope” of a variable/
• Show how we can use out “cuber” function (from the lecture 6 questions) to produce the sixth power of a number. You must write this as a single assignment statement.
• Explain how the one-way mirror analogy helps show what names you can use in your code within a single file.
• When we write programs using code in multiple files, we need to use the “import” statement. What does this line do?
• Show two ways you can use the cos function, defined in the “math” library, in your code.
• How does Python locate a library of functions.
• How can you find out what names are available in the “math” library?

### Python Strings¶

• How can you create strings in Python? Show an example.
• How can you determine the number of characters in a string?
• How would you write a look that examines every character in a string?
• How can we convert a string into all lower case?
• How do we copare the string held in some variable to a known value?
• How do we convert a string into a float (assuming th string is a valid float)?
• How do we convert a float into a string?

## Files and Lists¶

### Files¶

• What exactly is a “file”?
• How do you connect your program to a file on your system?
• Python can access a file three different ways. What are they?
• What would cause your program to fail when you try to connect with a file? Note that there is more than one way for things to fail!
• There are two basic types of files (we have only used one of them.) Describe the difference between them.
• Why is it important to close a file when you are finished using it?
• What do we mean by the term “file path”?
• Show a simple chunk of code that reads numbers from a text file in a loop (one number per line).
• Show a chunk of code that tries to open a file named “test.dat” and prints out “Can’t do that!” if it fails.
• Show an example data file that holds the names of several people (think Flintstones for an example). You just need to use the first and last names. After each name in the data file, add a line that shows their age (make something up). Show a chunk of code that could read this data.

### Lists¶

• What is a python list?
• Show a chunk of code that creates a list of numbers from 3 to 20.
• Show a loop that prints out the numbers in the previous list.
• Show how we can add another number to the end of the list.
• Show how to access the fifth element in the list.
• What happens when you try to access an element that is not in the list?
• Show an example of a two-dimensional array with three elements in each direction. Each cell should contain a string with the row number and the column number with a comma between those numbers (ex: “1,1”).
• What is a “tuple”?
• Show how Python uses tuples to create an output with place-holders for numbers you provide using a tuple.
• Show a chink of code that finds the biggest number in a list of numbers.
• What do we mean by the terms “mutable” and “immutable”?
• How do we access the last item in a list?
• What is a “slice” and how do you create one?

## Modules¶

• What is a Python module?
• Show an example of a simple “mymath” module containing one function names “mySqrt”. You can use the real Python “math” module to do the work n that function.
• Why do we add the “if __name__” block at the end of module files?
• Show the code for a function named “MaxMin” that takes two parameters and returns the minimum and maximum values in the right order. Show an example call that uses this function.
• Why is it important to create tests for the functions in a module?

## Packages¶

• Why do we use Modules and Packages in Python?
• What does the import statement do?
• In a Python package, what is the purpose of the __init__.py file?
• What is a “namespace” and how can you see it in Python?