Python Virtual Training For Arcesium - Module I - Day 1¶

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.

How to use jupyter¶

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

In [5]:
esc + m -> somethin 
  File "<ipython-input-5-df71062539e0>", line 1
    esc + m -> somethin
            ^
SyntaxError: invalid syntax

Programming¶

  • set of instructions to get desired output
  • its a language with which you communicate with system (computer)
  • Pyhton is interpreted programming language
  • It is higher level langange, means it is very close to english
  • Programs are written first for humans and later for machines.

Primitive data types of python programming¶

Numeric and Text data¶

In [7]:
42 + 42 # this is first python statement
Out[7]:
84
In [8]:
42 - 42
Out[8]:
0
In [9]:
42*2
Out[9]:
84
In [11]:
5 /2 # output this is not integer
Out[11]:
2.5
In [13]:
4/2 # division operator
Out[13]:
2.0
In [14]:
4//2 # integer division operator
Out[14]:
2
In [ ]:
 
In [16]:
3 ** 4 # power, 3 raised to power 4
Out[16]:
81
In [17]:
3.5 + 5.0
Out[17]:
8.5
In [19]:
1 + 2.0 # combination of float and integer results in float
Out[19]:
3.0
In [20]:
1 * 4.0
Out[20]:
4.0
In [21]:
3 + 4
Out[21]:
7
In [22]:
3 + 4*5
Out[22]:
23
In [23]:
5 % 2
Out[23]:
1
operators   priority
=========   ========

**          1
% // / *    2
+ -         3
>>> 2 ** 5%5/2*7
>>> 32 %5 /2*7
>>> 2/2*7
>>> 1*7
>>> 7
In [25]:
"Hello this is text" # anything that is in single or double quote ...
Out[25]:
'Hello this is text'
In [26]:
'with sigle quote'
Out[26]:
'with sigle quote'
In [27]:
"this is string with ' in it"
Out[27]:
"this is string with ' in it"
In [29]:
"He said, 'I am fine'"
Out[29]:
"He said, 'I am fine'"
In [30]:
"\n" #this is special charecter
Out[30]:
'\n'
In [32]:
print("this is line1 \nthis is line2\nthis is line3")
this is line1 
this is line2
this is line3
In [33]:
print("word1\tword2\tword3")
word1	word2	word3
In [35]:
print("c:\\Program Files\\Python3\\bin\\pip")
c:\Program Files\Python3\bin\pip
In [38]:
"hello" + "world" #concatenates the strings
Out[38]:
'helloworld'
In [39]:
"hello"*5
Out[39]:
'hellohellohellohellohello'
In [40]:
"*"*5
Out[40]:
'*****'
In [41]:
"hello" # repr ... representative ..it is meant for debugging purposes while programing
Out[41]:
'hello'
In [42]:
print("hello")
hello
In [43]:
"2"
Out[43]:
'2'
In [44]:
2
Out[44]:
2

Problems

  • Convert 12 bitcoins into rupees # conversion rate? 35700
  • Compound interest is calculated using this formula P(1+r/n)^nt.
  P -> principle 26780
  r -> rate of interest 7%
  n -> number of times interest gets compounded per year , quarterly 4
  t -> invertment timeline, 5
In [45]:
# 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
In [46]:
26780*(1 + 0.07/4)**(4*5)
Out[46]:
37887.76008234032

Variable and litterals¶

in addition to arithmatic opearators there is a special operator called assignment operator =

In [47]:
 3 + 4
Out[47]:
7
In [48]:
x = 10
In [49]:
x
Out[49]:
10
In [50]:
y = 30
In [51]:
print(x)
10
In [52]:
print(y)
30
In [53]:
x + y
Out[53]:
40
In [54]:
x * 2 # here x is variable and 2 is litteral
Out[54]:
20
In [55]:
vikrant = 10 # variable
In [56]:
"vikrant" # this is not a variable .. this is litteral string
Out[56]:
'vikrant'
In [58]:
vikrant # this is a variable name
Out[58]:
10
In [59]:
"vikrant" # this just text!
Out[59]:
'vikrant'
In [60]:
"text" + "hello"
Out[60]:
'texthello'
In [61]:
textvar = "text"
In [62]:
hellovar = "hello"
In [63]:
textvar + hellovar
Out[63]:
'texthello'
In [64]:
"test"
Out[64]:
'test'
In [65]:
'test'
Out[65]:
'test'
In [66]:
"""first
second
third
"""
Out[66]:
'first\nsecond\nthird\n'
In [67]:
'''first
second
third
'''
Out[67]:
'first\nsecond\nthird\n'
In [68]:
name = "arcesium"
In [69]:
name2 = 'arcesium'
In [70]:
name
Out[70]:
'arcesium'
In [71]:
name2
Out[71]:
'arcesium'

variable assignment¶

x  = 10
y = x
x = x +10

what will the the value of y?

In [72]:
x = 10
y = x
x = x+10
In [73]:
print(y)
10
x = 10
y = x
y = 25

What will be value of x?

Lets do more stuff with text data

In [75]:
s = "hello"
In [76]:
s[0] # zeroth char from s
Out[76]:
'h'
In [77]:
s[4] # 4th char
Out[77]:
'o'
In [78]:
s[-1] # last char
Out[78]:
'o'
+---+---+---+---+---+---+
| p | y | t | h | o | n |
+---+---+---+---+---+---+
  0   1   2   3   4   5     ------>
 -6  -5  -4  -3  -2  -1     <------

Collections¶

In [79]:
[1, 2, 34, 4]  # list of integers
Out[79]:
[1, 2, 34, 4]
In [80]:
numbers = [1, 2, 3, 4, 5, 6] 
In [81]:
numbers[0]
Out[81]:
1
In [82]:
numbers[1]
Out[82]:
2
In [83]:
numbers[-1]
Out[83]:
6
In [84]:
numbers[-2]
Out[84]:
5
In [85]:
print(numbers)
[1, 2, 3, 4, 5, 6]

lists are nothing but pythons array.

In [87]:
words = ["one", "two", "three"]
In [88]:
mixed = ["one", "text", 1, 2, 3]
In [89]:
mixed[0]
Out[89]:
'one'
In [90]:
mixed[-1]
Out[90]:
3
In [92]:
l = [["few", "words"], [1, 2, 3, 4]]
In [93]:
l[0]
Out[93]:
['few', 'words']
In [94]:
l[1]
Out[94]:
[1, 2, 3, 4]
In [95]:
l[0][0]
Out[95]:
'few'
In [96]:
l[1][2]
Out[96]:
3
In [97]:
words
Out[97]:
['one', 'two', 'three']
In [99]:
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
In [100]:
words[0]
Out[100]:
'one'
In [101]:
words[0]
Out[101]:
'one'
In [102]:
words[1]
Out[102]:
'two'
In [103]:
words[2]
Out[103]:
'three'
In [104]:
words[3]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-104-8728b4503fbc> in <module>
----> 1 words[3]

IndexError: list index out of range
In [105]:
words[2] 
Out[105]:
'three'
In [107]:
words[2] = "new"
In [108]:
dfdfd
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-108-e60c8e749a91> in <module>
----> 1 dfdfd

NameError: name 'dfdfd' is not defined
In [109]:
words
Out[109]:
['one', 'two', 'new']
In [110]:
s
Out[110]:
'hello'
In [111]:
s[0]
Out[111]:
'h'
In [112]:
s[2]
Out[112]:
'l'
In [114]:
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
In [117]:
words[2]
Out[117]:
'new'
In [118]:
words[2][-1] # last char from 2nd word
Out[118]:
'w'
In [119]:
[1, 1] * 2
Out[119]:
[1, 1, 1, 1]
In [120]:
word1 = "one"
word2 = "two"
word3 = "three"
In [121]:
w = [word1, word2, word3]
In [122]:
w
Out[122]:
['one', 'two', 'three']
In [123]:
w[2] = "new"
In [124]:
w
Out[124]:
['one', 'two', 'new']
In [125]:
word3
Out[125]:
'three'
In [126]:
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
In [127]:
numbers = [1, 2, 4, 5, 7]
In [128]:
numbers[-1] = "name"
In [129]:
numbers
Out[129]:
[1, 2, 4, 5, 'name']
In [130]:
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
In [132]:
["hello", "world"]*2 # repeat the list twice
Out[132]:
['hello', 'world', 'hello', 'world']
In [133]:
[1, 1, 2, 2] * 3
Out[133]:
[1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2]
In [134]:
[1, 1, 1] + [2, 2, 2]
Out[134]:
[1, 1, 1, 2, 2, 2]
In [135]:
c = "hello"
In [136]:
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

In [137]:
color = (0,0, 256)
In [138]:
color[0]
Out[138]:
0
In [139]:
color[-1]
Out[139]:
256
In [140]:
color[2]
Out[140]:
256
In [141]:
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

Dictionary - named collection¶

In [142]:
score = {"rupali":20, "alice":19, "maya":18, "kavya":20}
In [143]:
score['rupali']
Out[143]:
20
In [144]:
score['kavya']
Out[144]:
20
In [146]:
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'
In [147]:
score['alex'] = 20
In [148]:
score
Out[148]:
{'rupali': 20, 'alice': 19, 'maya': 18, 'kavya': 20, 'alex': 20}
In [149]:
score['alex']
Out[149]:
20
In [150]:
score['alice'] = 20
In [151]:
score
Out[151]:
{'rupali': 20, 'alice': 20, 'maya': 18, 'kavya': 20, 'alex': 20}
In [152]:
stock = {"name":"IBM", "open":123, "close":126, "high":128, "low":120}
In [153]:
stock['name']
Out[153]:
'IBM'
In [154]:
stock['close']
Out[154]:
126
In [155]:
stock['high']
Out[155]:
128
In [157]:
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}]}
In [158]:
portfolio['name']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-158-f1521bd52c4c> in <module>
----> 1 portfolio['name']

KeyError: 'name'
In [159]:
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"}
In [160]:
portfolio['name']
Out[160]:
'portfolio1'
In [163]:
portfolio['risky']
Out[163]:
[{'name': 'xyz', 'open': 123, 'close': 120},
 {'name': 'abc', 'open': 150, 'close': 130}]
In [164]:
portfolio['risky'][0]
Out[164]:
{'name': 'xyz', 'open': 123, 'close': 120}
In [165]:
portfolio['risky'][0]['name']
Out[165]:
'xyz'

Key things to remembers¶

  • to make a list we make use square brackes []
  • to make a string we make use of single or double quotes
  • to make a tuple we make use of round brackets ()
  • to make a dictionary we make use of curley brackets {}, inside you have to give "key":value pairs separated by comma
  • to access item from a string we use square bracket and integer index inside that string[2]
  • to access item from a list we use square bracket and integer index inside that list[2]
  • to access item from a tuple we use square bracket and integer index inside that tuple[2]
  • to access item from a dictionary we use square bracket and key inside that d['key']to make a dictionary we make use of curley brackets {}, inside you have to give
In [166]:
stock[0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-166-7c66a92b186a> in <module>
----> 1 stock[0]

KeyError: 0
In [176]:
intdict = {1:"one", 2:"two"}
In [177]:
intdict[1]
Out[177]:
'one'
In [178]:
intdict[2] = "TWO"
In [179]:
intdict
Out[179]:
{1: 'one', 2: 'TWO'}
In [182]:
intdict['capital2'] = "TWO"
In [183]:
intdict
Out[183]:
{1: 'one', 2: 'TWO', 'capital2': 'TWO'}
In [184]:
del intdict[2] # it will delete key:value for given key
In [185]:
intdict
Out[185]:
{1: 'one', 'capital2': 'TWO'}
In [187]:
[1, 1, 1]*-2
Out[187]:
[]
In [ ]: