Can Computers Think?
Introduction to Computer Science

CSC 106
Union College
Winter 2010

Preparing for the (midterm) exam

What to do to prepare

Important concepts to know

Types of questions you can expect on the midterm

Understanding basic Python syntax

For each of the following code snippets, say whether they are a valid Python expression, a valid Python statement or not valid. For expressions, also say what value they refer to. For statements explain what they do. For code snippets that are not valid explain why they are not valid.

  1. cat"
  2. 4 + 6
  3. math.sqrt(9)
  4. print random.randint(1,50)
Being able to trace what a Python program does step by step

a) What do you see on the screen, if we run the following program? How does the value that the variable 'count' refers to change throughout the execution of the code?

count = 0
while count <= 20:
      if count%2==0:
            print count
      count = count + 1

b) The following program is supposed to calculate the average of all numbers from 1 to 10. Instead we get an error message when we run it.

count = 1
while count <= 10:
      total = total + count
      count = count + 1
avg = float(total) / 10

Explain what causes this error. Then make a suggestion how it could be fixed.

Writing small functions or snippets of Python code (CodeLab-type questions)
Devising strategies for solving a problem computationally

Assume you are given a string and are asked to create a new string that looks the same as the first except that an underscore has been inserted between all letters. For example, dog yields d_o_g, elephant yields e_l_e_p_h_a_n_t and a yields a.

Write down in English a step-by-step strategy for solving this problem.