Part 1 (helloworld.
py)
How to call Python in Anaconda:
1. Anaconda3 – Anaconda Prompt – type “ipython”, or
2. Anaconda3 - jupyter notebook – (1) new – python3 or (2)
upload or (3) click any ipynb
3. #, ##, ### to control font size.
4. In any ipynb - File – save and checkpoint – download as
ipynb/html
5. To edit ipynb – “scissors” or insert cell above/below or
code/markdown
6. In ipynb, make sure to run codes sequentially !!
Python preliminaries:
in ipython
cd “d:”
For students, please put all your files in d: for simplicity
(OR from anywhere directly to your directory, e.g., cd “d:\data\eric”,
cd “c:\users\data”, cd “d:\”, etc.)
ls *.py
run [Link]
exit
good luck !
code editing: notepad++ -> notepad++ (single click)
always save your code as [Link] (don’t forget .py !)
[Link]
python-code-examples (need debugging !)
Part 2 ([Link])
Text manipulation in Python
See [Link]
Part 3 ([Link], [Link])
Screen input output eg magic8ball.
Part 4 ([Link], [Link])
How to read write data in python?
See [Link] and [Link]
Part 5 ([Link], [Link], [Link],
actuarial [Link], [Link], [Link])
Quick Start for Life Contingencies
The names of the formulas follow the International Actuarial Notation
and are easily guessable, with a few exceptions regarding special
characters (for example, A'x is replaced by AEx).
The reserved variables (in addition of python languages) are the
following:
For the mortality table:
nt = The actuarial table used to perform life contingencies
calculations. Syntax: nt=GKM95 (Note: GKM95 must be
included in [Link])
i = interest rate. The effective rate of interest, namely, the total
interest earned in a year. Syntax: i=0.02
perc = Optional variable to indicate the percentage of mortality
to be applied. Syntax: perc=85 Variable perc can be omitted, in
this case it will be 100 by default.
For actuarial formulas:
x = The age of the insured.
n = The horizon (term of insurance) in years or payment
duration
m = Number of fractional payments per period. If missing, m is
set as 0.
d = The deferring period in years.
See also actuarial [Link] !!
Finally, see [Link] and [Link] for data methods in python !
Part 6 ([Link])
Simple plot of life contingencies
Part 7 ([Link])
How to use variable length argument lists in Python.
The special syntax, *args and **kwargs in function definitions is used
to pass a variable number of arguments to a function. The single
asterisk form (*args) is used to pass a non-keyworded, variable-length
argument list, and the double asterisk form is used to pass
a keyworded, variable-length argument list. Here is an example of
how to use the non-keyworded form. This example passes one formal
(positional) argument, and two more variable length arguments.
def test_var_args(farg, *args):
print "formal arg:", farg
for arg in args:
print "another arg:", arg
test_var_args(1, "two", 3)
Results:
formal arg: 1
another arg: two
another arg: 3
Here is an example of how to use the keyworded form. Again, one
formal argument and two keyworded variable arguments are passed.
def test_var_kwargs(farg, **kwargs):
print "formal arg:", farg
for key in kwargs:
print "another keyword arg: %s: %s" % (key, kwargs[key])
test_var_kwargs(farg=1, myarg2="two", myarg3=3)
Results: (note %s = value of a string)
formal arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
Using *args and **kwargs when calling a function
This special syntax can be used, not only in function definitions, but
also when calling a function.
def test_var_args_call(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
args = ("two", 3)
test_var_args_call(1, *args)
Results:
arg1: 1
arg2: two
arg3: 3
Here is an example using the keyworded form when calling a
function:
def test_var_args_call(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
kwargs = {"arg3": 3, "arg2": "two"}
test_var_args_call(1, **kwargs)
Results: (note the order of arg3 and arg2 ! very snart !)
arg1: 1
arg2: two
arg3: 3
Part 8 ([Link])
Explore some plot features
Part 9 ([Link])
Advanced plot features
Part 10 ([Link])
How to write an ipython notebook
Part 11 (Summary Introduction to [Link])
Putting it all together !