CS 100

Logo

Scientific Computing

Activity 3 - Variables

PART 1 - Command/Shell Area

  1. In the shell area, type one at a time the following, hitting the Return/Enter key after each line. Observe the results.
    i = 2 + 3
    print(i)
    i = i + 7
    print("i = ", i)
    
  2. Without running any code, what will the value of the variable n be after the following Python instructions are executed?
    j = 5
    k = 10
    n = j*k
    n = n+1
    
  3. After making a prediction, run the instructions from Step 2 one at a time in the Command Area of Thonny. What value does n have at the end? How did you figure out what value n had? Was your prediction correct?
  4. Run the command
    int(3.2)
    

    What happens? Try running the same command a few more times, each time replacing the 3.2 by some other number. What do you think int does in Python?

  5. Run the command
    float(7)
    

    What happens? Try running the same command a few more times, each time replacing the 7 by some other number. What do you think float does in Python?

PART 2 - File Editor

  1. Open Thonny and create a new file called variables.py saved to the cs100/ch1 folder you created on the desktop.
  2. In that file type the following:
    i = 2 + 3
    print(i)
    i = i + 7
    print("i = ", i)
    
  3. Click the green arrow in Thonny to run the program. You’ll know it worked when something happens in the Command Area (Shell Area).
  4. Add a line which calculates the product of 10 and 2; save it in a variable called p; then, print out the value of p with a message similar to “p is 20” (just like you did with the variable i).
  5. Compute 2 to the power of 32. Store it in a variable called x. Print the result as “The power is: ___” , where your answer fills in the blank.

PART 3 - If you finish early, here are some things you can work on

  1. Add code that computes the average of 23, 15, 18, and 21; save that average in a variable called avg; then, display it with a message similar to “The average of these numbers is 19.25”.
  2. Create a variable called pi with the value 3.14159. Use it to compute the area of a circle with radius 3, and print out the result.
  3. If the universe is 15 billion years old, how many seconds old is it?

How to submit

Submit your variables.py file to Activity 3 on Moodle. Make sure it runs without errors!