Midterm Preparation

Hey everyone, sorry this is late. I did intend for this to be available closer to the midterm date, but here it goes!

Things to Remember

1. Read the Question!

No matter how many times I remind students to read the whole problem before answering (see: every lab so far), many people attempt to solve the problem before reading the whole question. You can’t answer a question you don’t understand. Take a breath, read the question, think, then look at the answers if you don’t already know. Do not read half the question, parse the possible answers and pick one you think is right. The questions will try to trick you, so make sure you get all the information out of them.

2. Watch the Time

There are several different types of questions on the midterm. Some take less time than others, and you will be more comfortable with some than with others. Make sure that you do not spend a long time on one question if you’re stuck. Move on and come back to it later if you have time. Speed is your friend, so finish the questions you know first. That will ease your mind, as you’ve already accomplished part of the test, and ensure that you don’t get stuck at the end with no time to finish.

3. Do the Problems

Practice problems are given to you for your own good. Use them! My advice is to scan all lecture and tutorial slides for questions, and answer them on paper as you go. Once you’re done going through all the slides, attempt the practice midterm without looking at the solutions. Once you’re done, throw out your answers and do it again. Only after the second attempt should you check your work with the answers. Trust me on this one. There are also some practice problems some of my tutorials.

4. Write the Code

If you’re stuck on a question that includes segments of code and asks something like “what is the result?” or “are both outputs the same?”, do not refer to the answers. Open IDLE and type the code yourself. Use “print” statements where necessary to see how the code is working, and use that to determine your answer.

5. Ask for Help

If you don’t understand a question, don’t accept failure. Ask your friends, check Google, or even visit the help centre. For the rest of the night, I will be answering specific questions via email as well. Use all your resources. Check PyDocs if you are confused about a data type or a function as well. This documentation is complete and always helpful.

Practice Problems

Here’s a small set of practice problems that I think will help you. Attempt all questions before looking at the answers. If you’re still struggling, check out Codecademy. It has great free tutorials on Python and can start from the ground up if you’re really stuck.

Also check out Google’s Python class (navigate using the left sidebar). Note that these lessons may differ from your course material, so be careful!


Define the following terms (write it down!)

• Variable
• Expression
• Statement
• Operator
• String

Answer:

Variable: A reference to reserved memory intended to store some value of any type. It is easy to think of a variable as a labelled jar. This jar can contain any information, but can only store one value at a time (a list still counts as one value). A variable can be any sequence of characters that begins with an underscore or a letter, and is not otherwise reserved by Python.

Expression: Expressions are statements that only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator “()” the subscription operator “[]” and similar, and can be reduced to some kind of “value”, which can be any Python object. Think of it as code with the ability to “express” some value. For example:

3 + 5 #expresses the value 8 
myList[0] #expresses the value of the first element in myList 
range(y) #expresses the range from 0 to y-1

Statement: Statements are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. For example:

print 42 
for i in range(0, 10) ...
a = 7 #the assignment statement

Operator: Used to construct Python statements, operators aused to create an expression or statement that is not a function or variablection or variable. These include things like +, -, /, =, and even the quotes .

String: A String is a data type which stores any sequence of characters that express “text”. This includes any sentence or word (“Hello”), any single character (“A”), and the empty string (“”). Think of it as “stringing” together several characters.


Identify three errors

in the following expression without typing it in to IDLE:

math.pi*(myNumber**(1/2))*8cos(pi/2)

Then, correct the code.

Answer:

Errors:
– Integer division
cos and pi must be referenced through the math library
(b)a is not correct. It must be entered as (b)*a

math.pi*(myNumber**(1.0/2.0))*8*math.cos(math.pi/2.0)

Enter the following commands into IDLE:
1 == 1 
'1' == '1' 
'1' == 1

For now, don’t worry too much about the use of conditionals. Why are the first two true (1 is in fact equal to 1, and “1” is equal to “1”), but the third statement is not?

Answer:

The third statement checks the equality of the integer 1, and the string “1”. Though the string looks the same, its value is much different. (Why is this? Think about its Unicode value).


Additional Practice

Write ten valid Python statements, and ten invalid Python statements. Try your best to be creative, and try some out to see what the Shell gives you. This will help you develop an understanding of what you can and can’t do in Python.