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
1
Floating numbers¶
1.0
Mathematical Operations¶
1 + 4
1 * 3
1 / 2
Exponents or Power¶
Use two asterisks
2 ** 4
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
4 % 2
5 % 2
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
2 + 3 * 5 + 5
(2 + 3) * (5 + 5)
Variable Assignment¶
# Can not start with number or special characters
name_of_var = 2
x = 2
y = 3
z = x + y
z
Re-assignment is possible¶
z = z + 2
z
Strings¶
String is also called called text, i.e. not-numbers. In Python, we enclose string in either single quote or double quotes.
'single quotes'
"double quotes"
" wrap lot's of other quotes"
Printing¶
x = 'hello'
x
print(x)
num = 12
name = 'Sam'
Pass Variables to placeholders created with curly brackets¶
print('My number is: {one}, and my name is: {two}'.format(one=num,two=name))
print('My number is: {}, and my name is: {}'.format(num,name))
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.
s = 'hello'
# Get the first element of s
s[0]
# Get the last element
s[4]
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
# Get all element after the second element
s = 'Pakistan'
s[1:]
Slice up to¶
If a number is put after : e.g. [:4], it will slice letters from start up to that number
# Get letters Pak in the word Pakistani
s = 'Pakistan'
s[:3]
# Question: How to get kist in the word Pakistan
s[2:6]
Lists¶
List is a sequence of elements wrapped in square brackets, separated by commas. It can take any data type.
a = [1,2,3]
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
a.append(4)
b.append('d')
a
Slicing Lists¶
Lists can sliced the same way as the strings, i.e. pass the index of the item in square brackets
# Get the first item of list a
a[0]
# Get the elements of a from second till third index
a[0: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
a[1] = 200
a
if, elif, else Statements¶
if 1 < 2:
print('Yep!')
if 1 < 2:
print('yep!')
if 1 < 2:
print('first')
else:
print('last')
if 1 > 2:
print('first')
else:
print('last')
if 1 == 2:
print('first')
elif 3 == 3:
print('middle')
else:
print('Last')
for Loops¶
seq = [1,2,3,4,5]
for item in seq:
print(item)
for item in seq:
print('Yep')
for jelly in seq:
print(jelly+jelly)
while Loops¶
i = 1
while i < 5:
print('i is: {}'.format(i))
i = i+1
functions¶
def square(x):
return x**2
out = square(2)
print(out)
methods¶
st = 'hello my name is Sam'
st.lower()
st.upper()
st.split()
tweet = 'Go Sports! #Sports'
tweet.split('#')
tweet.split('#')[1]