Sep 13-17, 2021 Vikrant Patil
These notes are available online at https://notes.pipal.in/2021/arcesium_finop_batch1/module1-day1.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from https://lab.pipal.in for this training.
esc + m -> coverts the cell into markdown cell (where you can text or some notes) if there is no square bracket , it is markdown cell.
esc + y -> converts the cell into code cell (in this you type python code) if you see a square baracket before the cell, it is code cell!
shift + enter -> executes the cell
esc + m -> somethin
File "<ipython-input-5-df71062539e0>", line 1 esc + m -> somethin ^ SyntaxError: invalid syntax
42 + 42 # this is first python statement
84
42 - 42
0
42*2
84
5 /2 # output this is not integer
2.5
4/2 # division operator
2.0
4//2 # integer division operator
2
3 ** 4 # power, 3 raised to power 4
81
3.5 + 5.0
8.5
1 + 2.0 # combination of float and integer results in float
3.0
1 * 4.0
4.0
3 + 4
7
3 + 4*5
23
5 % 2
1
operators priority
========= ========
** 1
% // / * 2
+ - 3
>>> 2 ** 5%5/2*7
>>> 32 %5 /2*7
>>> 2/2*7
>>> 1*7
>>> 7
"Hello this is text" # anything that is in single or double quote ...
'Hello this is text'
'with sigle quote'
'with sigle quote'
"this is string with ' in it"
"this is string with ' in it"
"He said, 'I am fine'"
"He said, 'I am fine'"
"\n" #this is special charecter
'\n'
print("this is line1 \nthis is line2\nthis is line3")
this is line1 this is line2 this is line3
print("word1\tword2\tword3")
word1 word2 word3
print("c:\\Program Files\\Python3\\bin\\pip")
c:\Program Files\Python3\bin\pip
"hello" + "world" #concatenates the strings
'helloworld'
"hello"*5
'hellohellohellohellohello'
"*"*5
'*****'
"hello" # repr ... representative ..it is meant for debugging purposes while programing
'hello'
print("hello")
hello
"2"
'2'
2
2
Problems
P -> principle 26780
r -> rate of interest 7%
n -> number of times interest gets compounded per year , quarterly 4
t -> invertment timeline, 5
# 5% -> 0.05 in excel
5% # it will expect a number! because it is modulus
File "<ipython-input-45-c132dc599b2d>", line 2 5% # it will expect a number! because it is modulus ^ SyntaxError: invalid syntax
26780*(1 + 0.07/4)**(4*5)
37887.76008234032
in addition to arithmatic opearators there is a special operator called assignment operator =
3 + 4
7
x = 10
x
10
y = 30
print(x)
10
print(y)
30
x + y
40
x * 2 # here x is variable and 2 is litteral
20
vikrant = 10 # variable
"vikrant" # this is not a variable .. this is litteral string
'vikrant'
vikrant # this is a variable name
10
"vikrant" # this just text!
'vikrant'
"text" + "hello"
'texthello'
textvar = "text"
hellovar = "hello"
textvar + hellovar
'texthello'
"test"
'test'
'test'
'test'
"""first
second
third
"""
'first\nsecond\nthird\n'
'''first
second
third
'''
'first\nsecond\nthird\n'
name = "arcesium"
name2 = 'arcesium'
name
'arcesium'
name2
'arcesium'
x = 10
y = x
x = x +10
what will the the value of y?
x = 10
y = x
x = x+10
print(y)
10
x = 10
y = x
y = 25
What will be value of x?
Lets do more stuff with text data
s = "hello"
s[0] # zeroth char from s
'h'
s[4] # 4th char
'o'
s[-1] # last char
'o'
+---+---+---+---+---+---+
| p | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 ------>
-6 -5 -4 -3 -2 -1 <------
[1, 2, 34, 4] # list of integers
[1, 2, 34, 4]
numbers = [1, 2, 3, 4, 5, 6]
numbers[0]
1
numbers[1]
2
numbers[-1]
6
numbers[-2]
5
print(numbers)
[1, 2, 3, 4, 5, 6]
lists are nothing but pythons array.
words = ["one", "two", "three"]
mixed = ["one", "text", 1, 2, 3]
mixed[0]
'one'
mixed[-1]
3
l = [["few", "words"], [1, 2, 3, 4]]
l[0]
['few', 'words']
l[1]
[1, 2, 3, 4]
l[0][0]
'few'
l[1][2]
3
words
['one', 'two', 'three']
l[0,0] # for list this will not work
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-99-fa980025eae7> in <module> ----> 1 l[0,0] # for list this will not work TypeError: list indices must be integers or slices, not tuple
words[0]
'one'
words[0]
'one'
words[1]
'two'
words[2]
'three'
words[3]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-104-8728b4503fbc> in <module> ----> 1 words[3] IndexError: list index out of range
words[2]
'three'
words[2] = "new"
dfdfd
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-108-e60c8e749a91> in <module> ----> 1 dfdfd NameError: name 'dfdfd' is not defined
words
['one', 'two', 'new']
s
'hello'
s[0]
'h'
s[2]
'l'
s[0] ="x" # strings are immutable .. once created can not be modified
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-114-a3a24c62866b> in <module> ----> 1 s[0] ="x" # strings are immutable .. once created can not be modified TypeError: 'str' object does not support item assignment
words[2]
'new'
words[2][-1] # last char from 2nd word
'w'
[1, 1] * 2
[1, 1, 1, 1]
word1 = "one"
word2 = "two"
word3 = "three"
w = [word1, word2, word3]
w
['one', 'two', 'three']
w[2] = "new"
w
['one', 'two', 'new']
word3
'three'
w[1][1]= "e"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-126-ae33a5ed38df> in <module> ----> 1 w[1][1]= "e" TypeError: 'str' object does not support item assignment
numbers = [1, 2, 4, 5, 7]
numbers[-1] = "name"
numbers
[1, 2, 4, 5, 'name']
numbers[-1][-1] = "a"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-130-c1f541214c05> in <module> ----> 1 numbers[-1][-1] = "a" TypeError: 'str' object does not support item assignment
["hello", "world"]*2 # repeat the list twice
['hello', 'world', 'hello', 'world']
[1, 1, 2, 2] * 3
[1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2]
[1, 1, 1] + [2, 2, 2]
[1, 1, 1, 2, 2, 2]
c = "hello"
c[0]="f"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-136-e3f192cbd027> in <module> ----> 1 c[0]="f" TypeError: 'str' object does not support item assignment
Another collection type is tuple, which is very similar to list, but is immutable
color = (0,0, 256)
color[0]
0
color[-1]
256
color[2]
256
color[0] = 255
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-141-2b3a41c9613e> in <module> ----> 1 color[0] = 255 TypeError: 'tuple' object does not support item assignment
score = {"rupali":20, "alice":19, "maya":18, "kavya":20}
score['rupali']
20
score['kavya']
20
score['alex'] # item with this name is stored
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-146-df92a8cce224> in <module> ----> 1 score['alex'] # item with this name is stored KeyError: 'alex'
score['alex'] = 20
score
{'rupali': 20, 'alice': 19, 'maya': 18, 'kavya': 20, 'alex': 20}
score['alex']
20
score['alice'] = 20
score
{'rupali': 20, 'alice': 20, 'maya': 18, 'kavya': 20, 'alex': 20}
stock = {"name":"IBM", "open":123, "close":126, "high":128, "low":120}
stock['name']
'IBM'
stock['close']
126
stock['high']
128
portfolio = {"risky": [{"name":"xyz", "open":123, "close":120}, {"name":"abc", "open":150, "close":130}],
"stable": [{'name':"stable1", "open":50, "close":40}, {"name":"stable2", "open":89, "close":85}]}
portfolio['name']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-158-f1521bd52c4c> in <module> ----> 1 portfolio['name'] KeyError: 'name'
portfolio = {"risky": [{"name":"xyz", "open":123, "close":120}, {"name":"abc", "open":150, "close":130}],
"stable": [{'name':"stable1", "open":50, "close":40}, {"name":"stable2", "open":89, "close":85}],
"name":"portfolio1"}
portfolio['name']
'portfolio1'
portfolio['risky']
[{'name': 'xyz', 'open': 123, 'close': 120},
{'name': 'abc', 'open': 150, 'close': 130}]
portfolio['risky'][0]
{'name': 'xyz', 'open': 123, 'close': 120}
portfolio['risky'][0]['name']
'xyz'
stock[0]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-166-7c66a92b186a> in <module> ----> 1 stock[0] KeyError: 0
intdict = {1:"one", 2:"two"}
intdict[1]
'one'
intdict[2] = "TWO"
intdict
{1: 'one', 2: 'TWO'}
intdict['capital2'] = "TWO"
intdict
{1: 'one', 2: 'TWO', 'capital2': 'TWO'}
del intdict[2] # it will delete key:value for given key
intdict
{1: 'one', 'capital2': 'TWO'}
[1, 1, 1]*-2
[]