Activity 2-1

Adapted from Brown University CSCI0931. Used with Permission.

Task 1: Python Expressions and Assignments

In this task, you will get more practice writing, evaluating, and assigning expressions to variables in Python. In particular, we will explore lists and strings. Open IDLE and interact with the terminal to complete the following steps:

  1. Write an expression that averages the numbers 2, 5, and 9.
  2. Write expressions to do the following (sequentially):
    1. Assign 5 to a, 8 to b
    2. Assign the value of a plus b to c and the value of a minus b to d
    3. Assign the value of a plus b plus b to e
    4. Assign the value of a plus b plus a plus b times b to c
    5. Assign the value of the remainder of c plus a, then divided by d, to b
    6. Assign the value of b to the power 5 to d
    7. Assign the value of b divided by a to c
    8. Print out the values of a, b, c, d
  3. Write programming statements to find out the answer to the following questions:
    1. Can you add two strings together?
    2. Can you multiply two strings together?
    3. Can you add a whole number to a string? A fractional number?
    4. Can you multiply a whole number with a string? A fractional number?
    5. Can you divide a string by a whole number? A whole number by a string?
    6. If var is a variable storing a string, what does len(var) do?
  4. Assign the list of numbers [2,5,9] to a variable called myList. Then
    1. Write an expression that averages the elements of myList.
    2. How would you find the number of elements inside myList? (Hint: Look at part (f) of the previous question.)
  5. Assign the string "Hong Kong Polytechnic University" to a variable called myU. Then:
    1. Write the expression myU.split(). What is the output? What does split() do?
    2. Write a programming statement to assign the result of myU.split() to the variable myWords.
    3. Write a programming statement to get the t from myWords (Note: there are two ways of doing this.)
    4. Write one programming statement to get the string PolyU from myWords
    5. How would you find out the number of characters inside myU?

Task 2: Python Program Files

In this task, you will get some practice with writing programs and running them in Python.

  1. Try out a new file. Download example.py (you will probably need to right-click on the link and use Save As) and open it in IDLE.
    1. Run it by choosing Run in the menu, and then Run Module. Alternatively, you can just hit F5
    2. Try to edit the file so that it says hello to you instead of to the world. Remember to save it before running again!
  2. Now you can try writing a new program on your own. In IDLE, go to File... new File. A new (blank) window should pop up.
  3. Write programming statements to:
    1. Assign the number 70 to the variable rmb
    2. Assign the number 1.22 to the variable exchange
    3. Calculate the product of rmb and exchange. Store the answer in hkd
    4. Print out the result of hkd
  4. Save your file into ACT2-1-2.py.
  5. Now run your program. What shows up in your IDLE window?
  6. Put a blank line at the beginning of your program. In that line, type in the following: This is my first program.
  7. Save your program and run it again. What happens? Will it run?
  8. Now, put a # at the beginning of that line (so that it will be # This is my first program.) Save your program and run it again. What happens now?
  9. From this, what do we understand about the # character?
  10. You have just made a currency exchange program that converts RMB to HKD. Suppose that I bought an Android tablet in Shenzhen that cost me RMB600. How much would that be in HKD? What lines would you need to change in your program?