Professional Documents
Culture Documents
nypkoh@gmail.com
• 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 !!
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/
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?
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
• 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
• 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
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)
• 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!""")
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.
• 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
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