Chapter 2 Exercise Set 0: Chapter Review¶
Asking Python for Datatype¶
Use the
type(...)
command to determine the type of each of the following:42
4.2
‘42’
4, 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¶
Describe in your own words how what the
input
function does, and how it works.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¶
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))
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¶
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¶
Add parenthesis to the expression
6 * 1 - 2
to change its value from 4 to -6.
Adding a comment¶
Place a comment before a line of code that previously worked, and record what happens when you rerun the program.
NameError¶
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 thatbruce + 4
evaluates to10
.
“Hello, Bob!”¶
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
¶
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
Write a Python program that assigns the principal amount of $100 to variable
p
, assigns ton
the value 12, and assigns tor
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 aftert
years.Tip
Remember to use type converter functions to turn the strings returned by the
input
function into numbers.
What school?¶
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. $