You are on page 1of 56

Programming in Python for

banking & finance industry


Career & Attachment Office, Student & Academic Services Department

Mr Koh Choon Chye


Conducted by

nypkoh@gmail.com

Knowledge On Hand Training Series


© Skyrich Info-Tech LLP
Python for Banking & Finance Run 2019.11
 
Seminar No Date Time Remarks
1 28 Dec 2019 (Saturday) 9 am to 12 noon LHN-LT
2 29 Dec 2019 (Sunday) 9 am to 12 noon LHN-LT
3 4 Jan  2020 (Saturday) 9 am to 12 noon LHN-LT
4 5 Jan  2020 (Sunday) 9 am to 12 noon LHN-LT
5 11 Jan 2020 (Saturday) 9 am to 12 noon LHS-TR+47

6 12 Jan 2020 (Sunday) 9 am to 12 noon LHN-LT


Course Objectives
• This course aim to introduce participants potentially entering into the
Banking industry on what programming skills are required in their
career path. It aims to equip the participants the essential skills they
require when learning any new programming language that are
commonly used in the banking industry as well.

• This course assumes NO prior programming experience.


How this course will be conducted
• Go through simple explanation based on the provided notes and from
any relevant website (so that you know where to find them after the
course or anytime in your career path)

• Conduct workshop to apply all the concepts covered with relevant


case studies in the banking and finance industry.

• Provide practise opportunities for you to try out additional case


studies
Seminar 1: Introduction to Python Scripting/Programming

• Understanding the needs of programming in Banking and Finance


• Introduction to the Python Language (Python 3)
• IDE environments available for Python
• Python programming in Interactive vs Script Mode
• Programming Syntax
• Data Types in Python and variables
• Basic Input->Process-> Output programming concept
• The print function in python
• Escape sequence in printing
• String Formatting in Python
• Case Studies :
• Print an announcement notice
• Print a loan repayment schedule
Disclaimer
• In view of the dynamic growth in the programming languages, the
learning experience of participants needs to be tailored to suit the
complementary standards. Hence the above course outline served
as a guide in content development, it may be altered for the benefits
of the participants
Ice Breaking
• Who am I?

• To contact me
• nypkoh@gmail.com
Now you …
• What is your background
• If you have done programming before, which language you learned?
• What is your 1 sentence aim upon completion of this course?
What is my expectations?
• Try, think, try
• Practice Practice Practice
• Ask, discuss, ask
• Contribute your thoughts, mistakes , requirements/needs !!

Google for answer?


What is missing ??
Where are programming languages used?
• Front-End
• Back-end

https://en.wikipedia.org/wiki/Programming_languages_used_in_most_popular_websites
Programming Languages
• Python
•R
• Java
• C#
• Visual Basic
Product Delivery
• Console
• Desktop
• Web
• Mobile Native
• Mobile Web
This course : Python https://www.python.org/downloads/

For Mac, after


installation, double-
click on IDLE file to
launch the IDLE shell
Differences Among Programming Languages
Scripting Programming Language
• These programming languages are often
procedural and may comprise object-
oriented language elements, but they fall
into their own category as they are
normally not full-fledged programming
languages with support for development
of large systems. For example, they may
not have compile-time type checking.
Usually, these languages require tiny
syntax to get started

https://www.typesnuses.com/types-of-programming-languages-with-differences/
• How entire program is structured
Syntax differences in
• How code block is structured
Programming
• How each statement ended
Languages
• How additional commands/libraries/modules are
added
What are compilers, translators, interpreters, and assemblers?

• are software programming tools that


convert code into another type of code, but
each term has specific meaning.
• getting a high-level programming language
translated into machine code that the
central processing unit (CPU) can
understand
• all are programs themselves

https://www.microcontrollertips.com/compilers-translators-interpreters-assemblers-faq/
Why Python?
• Version 3.0 and above

• interpreted

https://www.geeksforgeeks.org/python-programming-language/
IDE environments available in
Python

• IDLE
• PyCharm developed by the Czech company JetBrains
• Anaconda Anaconda, Inc. (previously 
Continuum Analytics)
• Visual Studio (Window)
• ………
Python programming in Interactive
vs Script Mode

• Interactive mode is a command line shell which


gives immediate feedback for each statement,
while running previously fed statements in active
memory. As new lines are fed into the interpreter,
the fed program is evaluated both in part and in
whole.
# is a comment in
• Interactive mode is a good way to play around and script mode,
try variations on syntax. interpreter ignore
whatever behind it
Main Purpose of Programming
• Automate tasks that are repetitive

• Perform tasks quicker

• Reduce/eliminate errors
Understanding the needs of programming in
Banking and Finance Combine
Financial Data
from
• Automate tasks that are repetitive difference
Financial sources
Schedule
Generate
• Perform tasks quicker financial
reports

• Reduce/eliminate errors Update


Exchange
Rates
Customer’s
Financial Statement
Ratios
Compute
Interest
Reserved words
(Keywords)

• The Python
language reserves a
small set of
keywords that
designate special
language
functionality.
• No object can have
the same name as a
reserved word.
https://www.geeksforgeeks.org/python-data-types/
How to know which data type python is
using?
• Type command
• In the interactive session
• type(100)
• type(3.44)
• type(“This is how you use type command”)
• What happens when a int plus a float?
• type(33+4.5)
What is Boolean
data type?
• Boolean
• True
• False
• Test the following
• type(True)
• type(False)
• type(true) Python is case
• type(false) sensitive !
• type(“True”) True is not the same
as true !
• type(“False”)
• type(True + False)
• type(True or False)
?????

https://crossroadsantigua.org/the-only-constant-in-life-is-change-heraclitus/
Variables
• Like your M+ in your calculator
• variable names in Python can be any length and can consist of
uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the
underscore character (_).
• The first character of a variable name cannot be a digit
• Variables in python is always declared with an assignment (=)
• distance = 100
• Discount = 0.3
Interesting feature in python
• Parallel Assignment
x, y =100,200 How to swap 2 variables in a program?
i.e. to swap the content of variables x & y
x
y
Workshop:
How to Calculate the Percentage Gain or Loss on an Investment

>>> priceSold = 120


>>> pricePurchased = 100
>>> investmentPCGain= (priceSold-pricePurchased)/pricePurchased * 100
>>> investmentPCGain
20.0
>>>
• Set variables
• price_sold or priceSold
• purchase_price or pricePurchase
• Work out answer
• investment_pc_gain or investmentPCGain

https://www.investopedia.com/ask/answers/how-do-you-calculate-percentage-gain-or-loss-investment/
Repeat the computation on Interactive mode
• Reassign priceSold and pricePurchased
• Use Alt+p to get the new calculation done
• Use Alt+p to get the new calculation shown
>>> priceSold = 120
>>> pricePurchased = 100
>>> investmentPCGain= (priceSold-pricePurchased)/pricePurchased * 100
>>> investmentPCGain
20.0
>>> priceSold = 200
>>> pricePurchased = 133
>>> {Alt}+{P} investmentPCGain= (priceSold-pricePurchased)/pricePurchased * 100
>>> {Alt}+{P} investmentPCGain
50.37593984962406
>>>
Standard Programming Concept

• Input
• Get the working
Input
value
• Process
• Compute Gain
Process
• Output
• Show result

Output
Interactive mode will not keep forever, we
need to keep this for re-run in future.
• Transfer all codes in the interactive mode to the scripting mode using
file new
• Run the code after you saved it to a try1.py file and observe what
happens.
• There is no response
• Modify the code for last sentence to print(investmentPCGain)

• In the interactive mode environment, when you enter a variable name, the interpreter
will assume you are asking for the output
• In a scripting mode, when there is command line with variable name, the interpreter
assume nothing is done on the variable
• So “print” is one of the way to produce OUTPUT for python
• print(“…….”) is the simplest way of printing
print function
• The print
function in Python is
a function that outputs to
your console window
whatever you say you want
to print out. At first blush, it
might appear that the print
function is rather useless for
programming, but it is
actually one of the most
widely used functions in all
of python.

https://www.w3schools.com/python/ref_func_print.asp
Workshop: Printing Announcement
• Sample website from banks (illustrated from ocbc)
print ("Subsidise up to 90% of your NUS, NTU, or NIE education")
print ("Subsidise up to",90, "% of your NUS, NTU, or NIE education")
print ("Subsidise up to ",90, "% of your NUS, NTU, or NIE education")
print ("Subsidise up to ",90, "% of your NUS, NTU, or NIE education", sep="")
print ("Subsidise up to ",90, "% of your NUS, NTU, or NIE education", sep="\t")
print ("Subsidise up to ",90, "% of your NUS, NTU, or NIE education", sep="\n")
Escape sequences in Python

https://linuxconfig.org/list-of-python-escape-sequence-characters-with-examples

http://python-ds.com/python-3-escape-sequences
What if I need to print a long paragraph?
• Trible double quotes illustrate
enclosed statements form a
print ("""Subsidise up to 90% of paragram
your NUS, NTU, or NIE • Hence there are many ways
to write a program
education""")
• It has to be correct and
efficient !!
Python String Formatting Best Practices
• Old Style String Formatting (% Operator)

• New Style String Formatting (str.format)

• String Interpolation /f-Strings (Python 3.6+)

• Template Strings (Standard Library)

Note : template strings don’t allow format specifiers


https://realpython.com/python-string-formatting/ need to manually transform the int numbers into a string
Python String Formatting Best Practices
• Old Style String Formatting (% Operator)

• New Style String Formatting (str.format)

• String Interpolation /f-Strings (Python 3.6+)

• Template Strings (Standard Library)

Note : template strings don’t allow format specifiers


https://realpython.com/python-string-formatting/ need to manually transform the int numbers into a string
Python comes with very small built.
Needs to import other libraries to cater for utilities
https://www.tutorialspoint.com/python/python_modules.htm
• Should not hardcode printing, so template printing may be more
welcome in programming

# one of the template printing


print( “I am {} years old”.format(20))
Problem Solving 1
• I want my output to be :

• But I got this , how ?

• My original code:

print("Enjoy low initial deposit of just S$500, no min balances required


for 1st 6 months and low $38 annual fee only!")
Solution – Handling long string with
triple quotes

print("""
Enjoy low initial deposit of
just S$500, no min balances
required for 1st 6 months and
low $38 annual fee only!""")
Dynamic formatting
deposit = 500
firstMth = 6
annualFee = 38
print("""Enjoy low initial deposit of
just S${}, no min balances
required for 1st {} months and
low ${} annual fee only!""".format(deposit, firstMth, annualFee))
Dynamic formatting
deposit = 500
firstMth = 6
annualFee = 38
print("""Enjoy low initial deposit of
just S${:.2f}, no min balances
required for 1st {} months and
low ${} annual fee only!""".format(deposit, firstMth, annualFee))
Dynamic formatting
deposit = 500
firstMth = 6
annualFee = 38
print("""Enjoy low initial deposit of
just S${:10.2f}, no min balances
required for 1st {:2d} months and
low ${:10.4f} annual fee only!""".format(deposit, firstMth, annualFee))
Dynamic formatting
deposit = 500
firstMth = 6
annualFee = 38
print("""Enjoy low initial deposit of
just S${0:10.2f}, no min balances
required for 1st {1:2d} months and
low ${2:10.4f} annual fee only!""".format(deposit, firstMth,
annualFee))
Problem Solving 2 (Homework?)
• Print the valuation of a server for the next 5 years as follows, you can
use any method of print you have learnt in this seminar.
Seminar 1, Problem Solving 2
• Print the valuation of a server for the next 5 years as follows, you can
use any method of print you have learnt in this seminar.

print("Projected Valuation of the server for the next 5 years is as follows:")


print("By end of Year 1 Valuation is $ 5000.00")
print("By end of Year 2 Valuation is $ 2500.00")
print("By end of Year 3 Valuation is $ 1250.00")
print("By end of Year 4 Valuation is $ 625.00")
print("By end of Year 5 Valuation is $ 312.50")
Problem Solving Technique and Design Thinking
• Problem definition
• Think of algorithm to solve the problem
• Think of alternative solutions
• Select the best alternative
• Correctness
• Efficiency User Experience
• Reusability
• Coding
• Sequence
• Branching
• Loop
print("Projected Valuation of the server for the next 5 years is as follows:")
print("By end of Year 1 Valuation is $ 5000.00")
print("By end of Year 2 Valuation is $ 2500.00")
print("By end of Year 3 Valuation is $ 1250.00")
print("By end of Year 4 Valuation is $ 625.00")
print("By end of Year 5 Valuation is $ 312.50")

• Correctness
• Programming, it is correctly sequenced
• Content, all amount shown are manually computed and entered, error prone
• Efficiency
• Program Execution, the program can execute the sentences quickly one after
another
• Coding, if the program need more years, coding this way is not efficient
• Reusability
• There are too many hardcoding so, it is hard to reuse the codes
Other alternative
• Year start from 1 to 5
• The initial valuation of the server should be 10,000.00, it is halved
every year

print("Projected Valuation of the server for the next 5 years is as follows:")


print("By end of Year 1 Valuation is $ 5000.00")
print("By end of Year 2 Valuation is $ 2500.00")
print("By end of Year 3 Valuation is $ 1250.00")
print("By end of Year 4 Valuation is $ 625.00")
print("By end of Year 5 Valuation is $ 312.50")
“for Loops” Syntax in Python
• Note the indentation required after :
• Note the starting value being printed =>0
• And the last value => one less than the number shown

https://www.w3schools.com/python/python_for_loops.asp
New Coding design
• Print the first line
• Change the remaining 5 lines to a loop starting from 0+1 to 4+5, total
lines required is 5
• Starting amount 10,000. Then divide by 2 in every iteration
print("Projected Valuation of the server for the next 5 years is as follows:")
print("By end of Year 1 Valuation is $ 5000.00")
print("By end of Year 2 Valuation is $ 2500.00")
print("By end of Year 3 Valuation is $ 1250.00")
print("Projected Valuation of the server for the next 5 years
print("By is as
end of follows:")
Year 4 Valuation is $ 625.00")
amount = 10000 print("By end of Year 5 Valuation is $ 312.50")
for i in range(5):
amount = amount/2
print("By end of Year {} Valuation is ${:8.2f}".format(i+1,amount))
Common error from coders

• Mostly likely is due to missing left or right brackets


• Interpreter will report source error from the line after the print (as
above)
By now, you should be able to
• Tell others why programming is needed in Banking and Finance, giving them some
examples of application
• Know the current version of Python Language is in version 3
• State some IDE environments available for Python
• Know Python programming in Interactive vs Script Mode
• Identify Programming Syntax
• Know the Data Types available in Python
• Use variables
• Understand Basic Input->Process-> Output programming concept
• Use the print function in python
• Use Escape sequence in printing
• Understand the need to Import functions into Python

You might also like