Chapter 2 Exercise Set 0: Chapter Review

Asking Python for Datatype

  1. Use the type(...) command to determine the type of each of the following:

    • 42

    • 4.2

    • ‘42’

    • 4, 2

  2. Now repeat the same process (using type(...)) and note what you get from each of the following:

    • type(3 + 6)

    • type(‘3 + 6’)

    • type(True)

    • type([1, 2, 3])

    • type((1, 2, 3))

The input function

  1. Describe in your own words how what the input function does, and how it works.

  2. Look at the following Python REPL session:

    >>> num = input("Please enter a number between 1 and 10: ")
    >>> type(num)
    

    How will the Python interpreter respond? Explain why.

Converting between types

  1. Type each of the following into the Python REPL and think about what you see:

    • >>> int('17')
      
    • >>> type(int('17'))
      
    • >>> str(17)
      
    • >>> type(str(17))
      
  2. What to you think will happen when you enter the following? Think about it before you try it!

    >>> int('3 + 6')
    

    Did you get what you expected? Why or why not?

All work and no play

  1. Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

Order of operations

  1. Add parenthesis to the expression 6 * 1 - 2 to change its value from 4 to -6.

Adding a comment

  1. Place a comment before a line of code that previously worked, and record what happens when you rerun the program.

NameError

  1. Start the Python interpreter and enter bruce + 4 at the prompt. This will give you an error:

    NameError: name 'bruce' is not defined
    

    Assign a value to bruce so that bruce + 4 evaluates to 10.

“Hello, Bob!”

  1. Write a program in a file named hello_bob.py that will produce a session something like this:

    $ python3 hello_bob.py
    
    Hi there! What's your name? Susmita
    
    Well, I'm pleased to meet you, Susmita!
    
    $
    

An n year old who likes x

  1. Write a program in a file named n_year_old.py that will produce a session something like this:

    $ python3 n_year_old.py
    
    How old are you? 16
    
    What is your favorite food? spam
    
    Ah, a 16 year old who likes spam.  How interesting!
    
    $
    

Compound interest

The formula for computing the final amount if one is earning compound interest is

formula for compound interest
  1. Write a Python program that assigns the principal amount of $100 to variable p, assigns to n the value 12, and assigns to r the interest rate of 8% (as a float). Then have the program prompt the user for the number of years, t, for which the money will be compounded. Calculate and print the final amount after t years.

    Tip

    Remember to use type converter functions to turn the strings returned by the input function into numbers.

What school?

  1. Write a program in a file named what_school.py that will produce a session something like this:

    $ python3 what_school.py
    
    What is your name? Miguel
    How old are you, Miguel? 17
    Excellent! 17 is a great age to be.
    What school do you attend, Miguel? NVCC
    A fine school, NVCC.
    
    $