Workshop
SMM283-INTRODUCTION TO PYTHON Dr. A. Euler
1 | P a g e
WEEK 2
[FORMATIVE HOME WORK]
TASK SET 1
(a)
i.Write a program that prompts the user to enter a real number, computers the square root
of the number and prints it out in two decimal places.
ii.Write a program that prompts the user to enter a real number and an integer number n,
computers the number in power n and prints it out in two decimal places.
iii.Write a program that generates a random number between 0.0 and 1.0 and prints it out
in two decimal spaces.
(b)
i.Write a program that prompts the user for his/her initials, validates the case of the
characters (upper or lower) and prints them out in upper case.
ii.Write a program that prompts the user for his/her name and prints it out in lower case.
iii.Write a program that it prompts the user for the time t validated to be positive only, an
amount to be received at time t validated to a positive real number, the discounting rate
validated to be between 0.0 and 1.0 and computes the discounted value. Make use of
arithmetical, short cut assignment, and logical operators where appropriate.
iv.Write a program that prompts the user for a character, checks to see whether it is a
white space character. White space characters are ‘ ‘, ‘\n’, ‘\t’. If the non-white character
is entered in upper case it converts it to lower case. The output could be as follows:
Enter a character: X
The character in lower case is x
or
Enter a character:
You entered a white space character!
TASK SET 2
(a)
SMM283-INTRODUCTION TO PYTHON Dr. A. Euler
2 | P a g e
Consider an equity based call option with an exercise price of K= 100. Write a program
that prompts the user the for market stock price at time t, it validates the stock price
against the exercise and computes the option payoff and a comment on whether the “right
to buy” is exercised or not. Use the time t option payoff formula 0,KSMAXC t −= . Create
various versions of the program using different conditional statements.
(b)
Assume a simple case of a two-fund separation problem with stock x with return of 15%
and stock y with a rate of return on 21% combined in a portfolio. Write a program that
prompts the user for the capital weights and computes the portfolio’s rate of return. Add
validation safeguards where appropriate. Use the formulae wp = wx + wy and rp = wxrx +
wyry.
TASK SET 3
(a)
Assume you plan to retire in t years and want to accumulate enough by then to provide
yourself with $30,000 a year for 15 years. The interest rate is 10 percent. Write a program
that prompts the user for the time t to retirement and computes the amount accumulated
by the time you retire. The time entered should be positive. Use the formula
+
−=
t
)r1(
11
rr
PMTPV
Note: The manual calculation would be
39.182,228$
)10.1(
11
15
=
−=
0.100.10
30,000PV
(b)
Assume you plan to retire in t years and want to accumulate enough by then to provide
yourself with $30,000 a year for 15 years. The interest rate is 10 percent. Write a
program that prompts the user for the time t to retirement and computes the amount
accumulated by the time you retire. The time entered should be positive. Use the formula
− +
=
t
1I
I
)r1(
PMT
PV
TASK SET 4
(a)
SMM283-INTRODUCTION TO PYTHON Dr. A. Euler
3 | P a g e
i.Write a program to count blanks, tabs, and newlines
ii.Write a program to copy its input to its output, replacing each string of one or more
blanks by a single blank.
iii.Write a program to copy its input to its output, replacing each tab by \t, each
backspace by \b, and each backslash by \\. This makes tabs and backspaces visible
in an unambiguous way.
iv. Write a program that prints its input one word per line.
(b)
i.Write a program that prompts the user to enter his/her name one character at a time as
part of a loop and assigns them to a string variable. The program should also print the
name one character at a time using another for loop.
ii.Write a program that prompts the user to enter a sentence and it should use a for loop
to pick up and counts the number of times the character ‘a’ or ‘A’ is present in the
sentence. The output could be as follows:
Enter a sentence: Hello John! Are you going to school today?
The frequency of the character ‘a’ (and ‘A’) is 2.
TASK SET 5
(a)
Recreate all Python scripts from lecture 2. Discuss the main concepts.
(b)
Refer to end of chapter 2 exercises in Schneider (2015) and code the following: exercise
set from section 3.2., questions 34, 35, 39, set 3.3 , questions 31, 61, 64, 65.