Career Hackers: Hack Your Career Today Python in Finance
Career Hackers: Hack Your Career Today Python in Finance
Python in Finance
Fintech Development in Banking
Introduction
Dear students,
Thank you for supporting our Giveaway Campaigns and thank you so much for your
continual support to Career Hackers. We are very grateful for your support.
This book is geared towards helping you understand more about recent
development for financial technology in banking industry. We have outlined the
major development in banks like Citi, JP Morgan and more.
We hope you will enjoy this book. We wish you all the best in this recruitment
season.
If you want technical training plus real work experience on your resume, you can
take a look at our Global Markets Externship or Management Consulting Externship.
Past students have ended up at large financial institutions like Point72 and Morgan
Stanley.
Regards,
Career Hackers Team
2. Expressive Language
5. Extensible
8. Interpreted Language
PyCharm is a cross-platform IDE, and hence it can be installed on a variety of operating systems.
In this section of the book, we will cover the installation process of PyCharm on Windows,
MacOS, CentOS, and Ubuntu where you can write python code. In order to install PyCharm on
Windows Operating System, visit the link
https://www.jetbrains.com/pycharm/download/downloadthanks.html?platform=windows to
download the executable installer. Then install PyCharm by following the steps on the install
wizard and you are ready to start coding!
This e-book will give you a basic idea of how python works and will put you in a position to carry
on further learning by yourself. So, let’s begin this wonderful journey of a world of endless
possibilities - “Python”.
2. Variables
Variables are containers for storing data values. Unlike other programming languages, Python
has no command for declaring a variable. A variable is created the moment you first assign a
value to it. However, there are some rules when it comes to declaring variables:
• The variable’s first character should be an alphabet or underscore(_)
• All the characters except the first character may be an alphabet of lower-case(a-z),
upper-case (A-Z), underscore or digit (0-9).
• The identifier name cannot contain any white space or any special characters (!, @, #, %,
^, &, *)
• The identifier name cannot be the same as a keyword that is defined in the language.
• The identifier names are case sensitive
Examples of valid identifiers: d345, _number, f_1, number
In python when declaring a variable, there is no rule that we have to declare the variable before
using the application. We are allowed to create the variable at the time it is required. The (=)
operator is used to assign a value to the variable as can be seen below:
d=5
name=’jack’
name_id=787
3. Comments
Comments are parts of the code that are ignored by the compiler and are not considered
when you execute a code. Comments can be extremely helpful for a variety of purposes
and provide many advantages. They make the code more presentable and informative and
provide the user/third-party (someone else who is reading the code) a sense of what is
going on in different parts of the code if it's properly commented. Commenting your code
is considered a good practice and everyone should include some informative comments
while writing codes.
There are two types of comments:
1. Single Line Comments
2. Multi Line Comments
These comments span over multiple lines and are strings that are enclosed within triple
quotes. For example:
Note: Triple quoted strings are considered as docstrings in python but when they are not
docstrings (the first line in a class/function/module), they are ignored. This will be
revisited again when we discuss “Functions” later.
4. Data Types
A variable can hold different types of values. There are five data types in Python. They are:
1. Numbers
2. String
3. List
4. Tuple
5. Dictionaries
a) Numbers
• Number stores numeric values. Python creates number objects when a number is assigned to
a variable.
o For example: x=2, y=10 # x and y are number objects
• The 4 types of numeric data that python supports are:
o int (3, 4, 8, 1000)
o long (long integers that are used)
o float (used to store floating point numbers)
o complex (used to store complex numbers)
b) Strings
Strings are one of the most commonly encountered data types in Python. They can store a single
character or a sequence of characters (alphabets, digits, special characters, etc.). Strings can be
created by enclosing characters in quotes, either single or double.
o For example:
In python, characters are treated as strings of unit length. To access values in strings, we use
their indices in the following way:
OUTPUT
As shown above, a part of the string can also be accessed using a colon (“:”) and specifying the
starting and terminating index before and after the colon.
Concatenation of strings means adding two strings together. It can be done in many ways which
has been shown through the following examples:
Example 1:
OUTPUT
Example 2:
This example uses accessing values in strings method along with concatenation. An interesting
thing to notice is that you can leave the starting or terminating index blank which makes the
compiler assume that the starting index is 0 or terminating index is the length of the string
respectively.
OUTPUT
Example 3:
Strings can be concatenated with a sequence of characters in double or single quotes in the print
statement.
OUTPUT
c) Lists
If you have ever learned the C programming language, then you must have heard about arrays.
Lists are like arrays, but lists can contain data of different types separated by commas ‘,’ and
enclosed between square brackets []. In order to get an output, we use the print() function. You
can also find in the below diagram how list indexes are given in a list and some code snippets
below with the output.
Length = 5
index 0 1 2 3 4
negative index -5 -4 -3 -2 -1
OUTPUT
d) Tuples
A tuple is very similar to a list in many ways. Just like lists, tuples also contain a collection of
items that have each been separated with a comma (,) and the encloses in parenthesis (). The
size and the value of items of a tuple cannot be modified and so is only a read only data structure.
OUTPUT
e) Dictionaries
OUTPUT
5. Operators
Operator can be defined as a symbol that performs a particular operation between two operands.
Python provides a variety of operators. Here are some of them:
Operator Precedence
• **
• >><<
• &
• ^|
In almost all programming languages, decision making is the most important feature. Decision
making enables us to run a specific piece segment of code for any particular decision. In this case,
the decisions are made based on whether the given conditions are satisfied or not. Condition
checking is the foundation behind decision making.
STATEMENT DESCRIPTION
The if else statement provides the code block for both the cases when the
If- else
condition is true, and the condition is false. If the condition provided in the
statement
if statement is false, then the ‘else’ statement will be executed.
Condition
If condition
is true
If condition
is false
if block
else block
Now let’s look at a simple example that involves if statements.
OUTPUT
In addition to just “if” and else, “elif” statements can also be used for decision making. “elif”
statements are only executed if the “if” statements before them are false.
Condition
Condition
Condition
Next Statement
Let’s look at another example:
OUTPUT
Loops
Loops play a key role in any programming language as the execution of a particular code may have
to be done several times. For this purpose, programming languages provide various types of loops
which help to repeat the same code for a specific number of times. The concept of loops can be
understood in the below diagram.
Loop Body
If condition is
Condition true
If condition
is false
Advantages of loops
Loop
Description
Statement
For loops are usually used when we need to execute a particular part of a code
for loop
until a given condition is satisfied. It is usually better to use the for loop when
As opposed to for loops, we use while loops in instances where we don’t know
While loop the exact number of iterations in advance. The code block will be executed
until the code the condition in the while loop is satisfied.
For Loops
Python for loops are commonly used to traverse in data structures like list, tuple or dictionary. As
the definition of “for” loop was explained in the above table, let's now look at the logic behind the
“for” loop and an example.
If no item is left in
the sequence
Item from the
sequence
Next Item
Loop Body
OUTPUT OUTPUT
for i in sequence:
for j in sequence:
#block of statements
#other statements
OUTPUTS
While Loops
The python “while” loop as mentioned in the table earlier, allows a block of code to be executed as
long as the given condition is true. It can be seen as a repeating if statement. The “while” loop is
mostly used in instances where the number of iterations is usually not known in advance.
Now let’s look at the syntax.
while expression:
statements
If Condition is false
Condition
If Condition is true
Loop Body
OUTPUT
OUTPUT
7. Functions
Functions are blocks of code that can be defined in a program and reused to perform a specific
action/task. Functions provide added modularity and save time because of high degree of
reusability.
Types of Functions:
2. User-Defined Functions: These are the functions that the users can define for themselves
according to particular needs.
Defining Functions:
It is the process of letting python know that the part of the code you are about to type is a
function. There are some simple rules that one should follow while defining functions:
1. All functions begin with the keyword def followed by the name of the function and
parentheses (( )).
2. The body of the function starts after a colon (:) and it is indented.
3. The first line of a function is optional - the documentation string of the function or
docstring.
4. A function ends with a return statement, optionally returning back a value to the caller.
A return statement with no arguments is the same as return None.
Syntax:
def functionname(parameters):
"""function_docstring"""
function_body
return_statement
Example of a function:
Calling a Function:
Defining a function only gives it a name, specifies the parameters that are to be included in the
function and structures the blocks of code. Now that you have defined a function, we want to use
it for the task we defined it for. You can execute a function (make it do what it is supposed to
do) by calling it from another function or directly from the python prompt.
The above function “square” takes an argument (“x”) and returns the square of the input to the
caller. Here is how you can execute this function to give you the desired output:
OUTPUT
As you can see, when we give 3 as the argument of the function “square”, it returns the square
of the number 3, i.e., 9.
Required Arguments:
These are the arguments passed to a function in correct order as specified while defining a
function. The number of arguments in the function call should match exactly with the function
definition.
The following example illustrates how you have to provide the two arguments ‘x’ and ‘y’ in order
to execute the function ‘_square_and_cube’ with two required arguments:
OUTPUT
If you do not provide a function with all the required arguments while calling it, you will encounter
an error.
When you try to call the above function “square_and_cube” as shown below, you will get the
following error:
OUTPUT
As you can see, the error tells you that an argument is missing and in order to properly call the
function, you have to provide all the arguments.
Default Arguments:
A default argument is an argument that assumes a specified value (while defining the function) if
a value is not provided while calling the function. The following example illustrates how you can
use default arguments in the definition of a function:
As you can see above, a default argument for ‘y’ has been provided in the function definition.
The first function call provides both the arguments while the second function call only provides
the ‘x’ argument. The following shows how the output varies because of default arguments:
When the argument for ’y’ is not provided, the function automatically assumes the input to be ‘4’
which has been specified in the function definition.
There you go for the Introduction to Python Programming. It is completely fine that you don’t
recite any of the above as long as you can now think from a programming perspective, a more
critical perspective and, most importantly, start applying it to whatever context you can at work.
About Us
Funded by Hong Kong Science and Technology Park, Career Hackers aims
to democratize career secrets by connecting students with industry
insiders. All the insiders need to go through a vetting process where we
verified the firms that they are working in and the positions that they
are holding, in order to make sure every information on our platform is
verified.
Once again, thank you for your support to Career Hackers and wish you
all the best.