 # 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.

In :
```1
```
Out:
`1`

In :
```1.0
```
Out:
`1.0`

In :
```1 + 4
```
Out:
`5`
In :
```1 * 3
```
Out:
`3`
In :
```1 / 2
```
Out:
`0.5`

## Exponents or Power¶

Use two asterisks

In :
```2 ** 4
```
Out:
`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 :
```4 % 2
```
Out:
`0`
In :
```5 % 2
```
Out:
`1`

## Order of Operations¶

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

In :
```2 + 3 * 5 + 5
```
Out:
`22`
In :
```(2 + 3) * (5 + 5)
```
Out:
`50`

### Variable Assignment¶

In :
```# Can not start with number or special characters
name_of_var = 2
```
In :
```x = 2
y = 3
```
In :
```z = x + y
z
```
Out:
`5`

## Re-assignment is possible¶

In :
```z = z + 2
z
```
Out:
`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 :
```'single quotes'
```
Out:
`'single quotes'`
In :
```"double quotes"
```
Out:
`'double quotes'`
In :
```" wrap lot's of other quotes"
```
Out:
`" wrap lot's of other quotes"`

### Printing¶

In :
```x = 'hello'
```
In :
```x
```
Out:
`'hello'`
In :
```print(x)
```
```hello
```
In :
```num = 12
name = 'Sam'
```

### Pass Variables to placeholders created with curly brackets¶

In :
```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 :
```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 :
```s = 'hello'
```
In :
```# Get the first element of s
s
```
Out:
`'h'`
In :
```# Get the last element
s
```
Out:
`'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 :
```# Get all element after the second element
s = 'Pakistan'
s[1:]
```
Out:
`'akistan'`

### Slice up to¶

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

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

### Lists¶

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

In :
```a = [1,2,3]
```
In :
```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 :
```a.append(4)
b.append('d')
a
```
Out:
`[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 :
```# Get the first item of list a
a
```
Out:
`1`
In :
```# Get the elements of a from second till  third index
a[0:3]
```
Out:
`[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 :
```a = 200
a
```
Out:
`[1, 200, 3, 4]`

## if, elif, else Statements¶

In :
```if 1 < 2:
print('Yep!')
```
```Yep!
```
In :
```if 1 < 2:
print('yep!')
```
```yep!
```
In :
```if 1 < 2:
print('first')
else:
print('last')
```
```first
```
In :
```if 1 > 2:
print('first')
else:
print('last')
```
```last
```
In :
```if 1 == 2:
print('first')
elif 3 == 3:
print('middle')
else:
print('Last')
```
```middle
```

## for Loops¶

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

## while Loops¶

In :
```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 :
```def square(x):
return x**2
```
In :
```out = square(2)
```
In :
```print(out)
```
```4
```

## methods¶

In :
```st = 'hello my name is Sam'
```
In :
```st.lower()
```
Out:
`'hello my name is sam'`
In :
```st.upper()
```
Out:
`'HELLO MY NAME IS SAM'`
In :
```st.split()
```
Out:
`['hello', 'my', 'name', 'is', 'Sam']`
In :
```tweet = 'Go Sports! #Sports'
```
In :
```tweet.split('#')
```
Out:
`['Go Sports! ', 'Sports']`
In :
```tweet.split('#')
```
Out:
`'Sports'`