Data Sciences for Finance

This notebook will just go through the basic topics in order:

  • Data types
    • Numbers
    • Strings
    • Printing
    • Lists
    • Dictionaries
    • Booleans
    • Tuples
    • Sets
  • Comparison Operators
  • if, elif, else Statements
  • for Loops
  • while Loops
  • range()
  • list comprehension
  • functions
  • lambda expressions
  • map and filter
  • methods

DATA TYPES

Python has two types of numbers. The integers and the floating point numbers, that support decimal points.

Integers

In [1]:
1
Out[1]:
1

Floating numbers

In [3]:
1.0
Out[3]:
1.0

Mathematical Operations

In [2]:
1 + 4
Out[2]:
5
In [7]:
1 * 3
Out[7]:
3
In [8]:
1 / 2
Out[8]:
0.5

Exponents or Power

Use two asterisks

In [9]:
2 ** 4
Out[9]:
16

Modulo

The modulo operation finds the remainder after division of one number by the other, if the number is fully divisible, the remainder will be zero. Use the % symbol for modulo

In [10]:
4 % 2
Out[10]:
0
In [11]:
5 % 2
Out[11]:
1

Order of Operations

Follow the PEMDAS rule
P       Parentheses , then
E       Exponents, then
MD    Multiplication and division, left to right, then
AS     Addition and subtraction, left to right

In [5]:
2 + 3 * 5 + 5
Out[5]:
22
In [12]:
(2 + 3) * (5 + 5)
Out[12]:
50

Variable Assignment

In [13]:
# Can not start with number or special characters
name_of_var = 2
In [16]:
x = 2
y = 3
In [18]:
z = x + y
z
Out[18]:
5

Re-assignment is possible

In [19]:
z = z + 2
z
Out[19]:
7

Strings

String is also called called text, i.e. not-numbers. In Python, we enclose string in either single quote or double quotes.

In [17]:
'single quotes'
Out[17]:
'single quotes'
In [18]:
"double quotes"
Out[18]:
'double quotes'
In [19]:
" wrap lot's of other quotes"
Out[19]:
" wrap lot's of other quotes"

Printing

In [20]:
x = 'hello'
In [21]:
x
Out[21]:
'hello'
In [22]:
print(x)
hello
In [23]:
num = 12
name = 'Sam'

Pass Variables to placeholders created with curly brackets

In [24]:
print('My number is: {one}, and my name is: {two}'.format(one=num,two=name))
My number is: 12, and my name is: Sam
In [25]:
print('My number is: {}, and my name is: {}'.format(num,name))
My number is: 12, and my name is: Sam

Indexing String

Each letter in a string object is called element, which can be obtained by using 0,1,2,3. So the word hello has 5 elements, where h is located at index 0, e is located at index 1, etc.

In [20]:
s = 'hello'
In [21]:
# Get the first element of s
s[0]
Out[21]:
'h'
In [23]:
# Get the last element
s[4]
Out[23]:
'o'

Slicing Elements

Slicing means to get out a specific part from the string. In the following example, the colon : means everything onwards, so 1: means starting at element 2 and everything coming thereafter.

Slice From

If a number is put before : e.g. [1:], it will slice letters from the that number till end

In [25]:
# Get all element after the second element
s = 'Pakistan'
s[1:]
Out[25]:
'akistan'

Slice up to

If a number is put after : e.g. [:4], it will slice letters from start up to that number

In [27]:
# Get letters Pak in the word Pakistani
s = 'Pakistan'
s[:3]
Out[27]:
'Pak'
In [30]:
# Question: How to get kist in the word Pakistan
s[2:6]
Out[30]:
'kist'

Lists

List is a sequence of elements wrapped in square brackets, separated by commas. It can take any data type.

In [36]:
a = [1,2,3]
In [43]:
b = ['a','b','c']

Append to the list

Elements can be appended to the list using the .append() method, where the new elements will be passed in the brackets. So let us append 4 to list a and 'd' to list b

In [37]:
a.append(4)
b.append('d')
a
Out[37]:
[1, 2, 3, 4]

Slicing Lists

Lists can sliced the same way as the strings, i.e. pass the index of the item in square brackets

In [39]:
# Get the first item of list a
a[0]
Out[39]:
1
In [46]:
# Get the elements of a from second till  third index
a[0:3]
Out[46]:
[1, 2, 3]

Change elements in a list

say that we want to replace the second element of list a with 200, it can be done by using the same index notations

In [48]:
a[1] = 200
a
Out[48]:
[1, 200, 3, 4]

if, elif, else Statements

In [56]:
if 1 < 2:
    print('Yep!')
Yep!
In [57]:
if 1 < 2:
    print('yep!')
yep!
In [58]:
if 1 < 2:
    print('first')
else:
    print('last')
first
In [59]:
if 1 > 2:
    print('first')
else:
    print('last')
last
In [60]:
if 1 == 2:
    print('first')
elif 3 == 3:
    print('middle')
else:
    print('Last')
middle

for Loops

In [61]:
seq = [1,2,3,4,5]
In [62]:
for item in seq:
    print(item)
1
2
3
4
5
In [63]:
for item in seq:
    print('Yep')
Yep
Yep
Yep
Yep
Yep
In [64]:
for jelly in seq:
    print(jelly+jelly)
2
4
6
8
10

while Loops

In [65]:
i = 1
while i < 5:
    print('i is: {}'.format(i))
    i = i+1
i is: 1
i is: 2
i is: 3
i is: 4

functions

In [77]:
def square(x):
    return x**2
In [78]:
out = square(2)
In [79]:
print(out)
4

methods

In [111]:
st = 'hello my name is Sam'
In [112]:
st.lower()
Out[112]:
'hello my name is sam'
In [113]:
st.upper()
Out[113]:
'HELLO MY NAME IS SAM'
In [103]:
st.split()
Out[103]:
['hello', 'my', 'name', 'is', 'Sam']
In [104]:
tweet = 'Go Sports! #Sports'
In [106]:
tweet.split('#')
Out[106]:
['Go Sports! ', 'Sports']
In [107]:
tweet.split('#')[1]
Out[107]:
'Sports'