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

Jan 16-20, 2023 Vikrant Patil

All notes are available online at https://notes.pipal.in/2023/arcesium_finop_jan/

Please accept the invitation that you have received in your email and login to

https://engage.pipal.in/

© Pipal Academy LLP

Shortcuts for working with jupyter notebook¶

Headline 1¶

headline 2¶

healine 3¶

esc + m -> markdown cell
esc + y -> code cell
shift + enter -> execute the contents of cell and creates new cell
!command -> executes system command from jupyter
In [1]:
3 + 4
Out[1]:
7

Numeric And Text data¶

Just like any programming language python supports basic data types to work with

In [3]:
42 
Out[3]:
42
In [4]:
42 + 56 
Out[4]:
98
In [5]:
42 * 2
Out[5]:
84
In [6]:
42 ** 3 # this is comment ... it is power
Out[6]:
74088
In [7]:
5 ** 2
Out[7]:
25
In [8]:
5 // 2 # integer division
Out[8]:
2
In [9]:
5 / 2 # division
Out[9]:
2.5
In [10]:
2.5 # float , to represent real numbers
Out[10]:
2.5
In [11]:
5.3 + 4.5
Out[11]:
9.8
In [47]:
5.3 * 3.3
Out[47]:
17.49
In [13]:
5.5 / 2.0
Out[13]:
2.75
In [15]:
4 + 5 + 8.0 # anywhere if there is float in the expression it results into float
Out[15]:
17.0
In [16]:
10%2 # remainder
Out[16]:
0
In [17]:
11%2
Out[17]:
1

operators and priorities


last number in row is priority of the mathematical operator
+   addition     3
-   substraction  3
*   multiplication 2
/   division 2
//  intteger division 2
%   modulus 2
**  power 1
()  parenthesis 0

In [19]:
2 + 3**2 # priority decides which operation is to be done first
Out[19]:
11
In [21]:
4*(4+5)**2
Out[21]:
324

Text data¶

In [22]:
"Hello this is text data"
Out[22]:
'Hello this is text data'
In [23]:
2 + 2
Out[23]:
4
In [24]:
2
Out[24]:
2
In [25]:
"text"
Out[25]:
'text'
In [26]:
'text'
Out[26]:
'text'
In [27]:
'This is also text data'
Out[27]:
'This is also text data'

Python also supports multiline strings

In [28]:
"""This is my first line of poem
this is second line
this is third line"""
Out[28]:
'This is my first line of poem\nthis is second line\nthis is third line'
In [29]:
'''you can also 
give single tripple
quote ..and that
also makes a 
multiline string'''
Out[29]:
'you can also \ngive single tripple\nquote ..and that\nalso makes a \nmultiline string'
In [30]:
'just like this text..multiline string is stored internally!'
Out[30]:
'just like this text..multiline string is stored internally!'
In [31]:
'just like this text.\nmultiline string is stored internally!'
Out[31]:
'just like this text.\nmultiline string is stored internally!'

some special chars


\n  new line
\t  means tab
\\

In [33]:
"c:\\program files\\python\\bin\\python.exe"
Out[33]:
'c:\\program files\\python\\bin\\python.exe'
In [34]:
"\\n"
Out[34]:
'\\n'
In [35]:
print("Hello World!") # this is function to print something on screen
Hello World!
In [36]:
print("""line1
line2
line3""")
line1
line2
line3
In [37]:
print(4 + 2)
6
In [39]:
4 + 2 # this is interpreter's response is given only last line of code
Out[39]:
6
In [41]:
4 * 1
4 * 2
4 * 3
4 * 4 # response of interpreter is only for last line
Out[41]:
16
In [42]:
print(4 * 1)
print(4 * 2)
print(4 * 3)
print(4 * 4) 
4
8
12
16
In [46]:
"""line1
line2
line3""" # interpreter responds back with the object
Out[46]:
'line1\nline2\nline3'
In [45]:
print("one\ntwo") # in print \n means end this line and go to start of line on next line
one
two

problem

Compound interst is calculated using formula P(1+r/n)$^n$$^t$. Where


P  principal amount = 26780
r  rate of interest = 7%
n  number of times interest in compounded in a year = 4
t  number of years = 5

In [48]:
4 +
  Cell In[48], line 1
    4 +
       ^
SyntaxError: invalid syntax
In [50]:
4 + 5
Out[50]:
9
In [51]:
26780(1 + 0.007/4)**(4*5)
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
/tmp/ipykernel_5303/1641895379.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  26780(1 + 0.007/4)**(4*5)
/tmp/ipykernel_5303/1641895379.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  26780(1 + 0.007/4)**(4*5)
/tmp/ipykernel_5303/1641895379.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  26780(1 + 0.007/4)**(4*5)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[51], line 1
----> 1 26780(1 + 0.007/4)**(4*5)

TypeError: 'int' object is not callable
In [52]:
print() # calling function print

In [53]:
7() # call 7 as a function
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
/tmp/ipykernel_5303/121486445.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  7() # call 7 as a function
/tmp/ipykernel_5303/121486445.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  7() # call 7 as a function
/tmp/ipykernel_5303/121486445.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  7() # call 7 as a function
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[53], line 1
----> 1 7() # call 7 as a function

TypeError: 'int' object is not callable
In [54]:
26780*(1 + 0.007/4)**(4*5)
Out[54]:
27733.047453680432

Variables and literals¶

We talked about mathematical operators.. but we did not talk about =

In [55]:
x = 10 # it means save 10 (integer!) and whenever I recall with x ..I should get back 10
In [56]:
x
Out[56]:
10
In [57]:
x + 2
Out[57]:
12

x is called as variable

10 is called as literal

In [58]:
10
Out[58]:
10
In [59]:
name = "python" 
In [61]:
name # variable
Out[61]:
'python'
In [63]:
"name" # this is not a variable, it is literal which 
Out[63]:
'name'
In [64]:
x = 10
In [65]:
"x" # this literal for text data 'x'
Out[65]:
'x'
In [66]:
x
Out[66]:
10
In [67]:
vikrant = 20
In [69]:
vikrant # without any quotes...
Out[69]:
20
In [70]:
"vikrant" # it is text data... it is literal
Out[70]:
'vikrant'
In [71]:
square2 = 2*2
In [72]:
2square = 2*2
  Cell In[72], line 1
    2square = 2*2
    ^
SyntaxError: invalid decimal literal

What can be used as variable name has some rules

  • The variable name can't start with number
  • it can be a single word (it can not have space, hyphen, comma, any operator)
  • it can have alphapbets, numbers and underscore
In [73]:
square_2 = 2**2
In [74]:
square2 =  2**2
In [75]:
file_path = "c:\\program files"
In [76]:
a, b  = 2, 3
In [77]:
print(a)
2
In [78]:
print(b)
3

problems

Have a look at following python statements

x = 10
y = x
x = x + 10

What will be values of y after this?

In [79]:
x = 10
y = x
x = x + 10
In [80]:
y
Out[80]:
10
In [81]:
x
Out[81]:
20
x = 10
y = x
y = 25

What will be values of x after executing these statemet

Now we have variable let's do more of text processing

In [82]:
s = "hello"
In [84]:
s * 2# in python you can multiply by ints .. it will repeat the text data those many times
Out[84]:
'hellohello'
In [85]:
"*"*5
Out[85]:
'*****'
In [86]:
first = "pyhton"
last = "programmer"
In [88]:
first + last # it concatenates the string
Out[88]:
'pyhtonprogrammer'
In [89]:
s
Out[89]:
'hello'
In [90]:
first
Out[90]:
'pyhton'
In [91]:
last
Out[91]:
'programmer'
In [92]:
first + last
Out[92]:
'pyhtonprogrammer'
In [94]:
first + 3 # you can add only two strings
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[94], line 1
----> 1 first + 3 # you can add only two strings

TypeError: can only concatenate str (not "int") to str
In [96]:
first * last  # you can only multiply by an integer to a string
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[96], line 1
----> 1 first * last  # you can only multiply by an integer to a string

TypeError: can't multiply sequence by non-int of type 'str'
In [97]:
first - last
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[97], line 1
----> 1 first - last

TypeError: unsupported operand type(s) for -: 'str' and 'str'
In [99]:
first - "p"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[99], line 1
----> 1 first - "p"

TypeError: unsupported operand type(s) for -: 'str' and 'str'
In [100]:
s
Out[100]:
'hello'
In [101]:
s[0] # in python this where it starts
Out[101]:
'h'
In [102]:
s[1]
Out[102]:
'e'
In [103]:
s[3]
Out[103]:
'l'
In [104]:
s[4]
Out[104]:
'o'
In [105]:
s[5]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[105], line 1
----> 1 s[5]

IndexError: string index out of range
In [107]:
s[-1] # last char
Out[107]:
'o'
In [108]:
s[-2]  # second last char
Out[108]:
'l'
In [110]:
s(0) # this is calling function... not for accessing item from text
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[110], line 1
----> 1 s(0) # this is calling function... not for accessing item from text

TypeError: 'str' object is not callable
> 0  1  2  3  4  5
  p  y  t  h  o  n
 -6 -5 -4 -3 -2 -1  <-

Collections¶

Other than basic data types python also supports some collections

In [112]:
ones = [1, 1, 1, 1]
In [114]:
ones # this is a list
Out[114]:
[1, 1, 1, 1]
In [115]:
ones[0] # zeroth item from ones
Out[115]:
1
In [116]:
numbers = [2, 1, 4, 3, 6]
In [118]:
numbers[0] 
Out[118]:
2
In [119]:
numbers[1]
Out[119]:
1
In [120]:
numbers[3]
Out[120]:
3
In [121]:
numbers[-1]
Out[121]:
6
In [123]:
names = ['Aakash', 'Yash', 'Saketh', 'Avinash'] # list containing words
In [124]:
names[0] # zeroth item from the list
Out[124]:
'Aakash'
In [125]:
names[-1]
Out[125]:
'Avinash'
In [126]:
mixed_list = [1, "one", 2 , "two", 3, "three"]
In [127]:
mixed_list[0]
Out[127]:
1
In [128]:
mixed_list[1]
Out[128]:
'one'
In [129]:
mixed_list = [1, "one", 2 , "two", 3, "three",] 
In [130]:
mixed_list
Out[130]:
[1, 'one', 2, 'two', 3, 'three']
In [131]:
mixed_list = [1, "one", 2 , "two", 3, "three",,] 
  Cell In[131], line 1
    mixed_list = [1, "one", 2 , "two", 3, "three",,]
                                                  ^
SyntaxError: invalid syntax
In [132]:
empty = ""
In [133]:
None # special data .. just like null ..nothing
In [134]:
[1, 2, 3, None, 4, 5]
Out[134]:
[1, 2, 3, None, 4, 5]
In [135]:
items = [1, "one", 2, "two", None, 5, "five"]
In [136]:
items
Out[136]:
[1, 'one', 2, 'two', None, 5, 'five']
In [140]:
items[-3] # interprenter will not respond for None
In [141]:
print(items[-3])
None
In [142]:
empty_text = "" 
In [143]:
empty_list = []
In [147]:
complex_list = [[1, 2, 3], ["one", "two", "three"]]
In [145]:
complex_list[0] # what will this be?
Out[145]:
[1, 2, 3]
In [148]:
complex_list
Out[148]:
[[1, 2, 3], ['one', 'two', 'three']]
In [149]:
complex_list[0][0] # what will this give
Out[149]:
1
In [150]:
complex_list[0][-1]
Out[150]:
3
In [152]:
numeric, text = complex_list[0][0], complex_list[1][0]
print(numeric, text)
1 one
In [153]:
index = 0
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
1 one
In [154]:
index = 1
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
2 two
In [155]:
index = 2
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
3 three
In [157]:
numbers, text # this is something tuple
Out[157]:
([2, 1, 4, 3, 6], 'three')
In [161]:
1, 2, 3, 4 # this is not a function call .. it actually creates a tuple
Out[161]:
(1, 2, 3, 4)
In [159]:
print(1, 2, 3, 4) # this is function call
1 2 3 4
In [160]:
print(1, 2, 3, 4, sep=",")
1,2,3,4
In [162]:
t = (1, 2, 3)
In [169]:
t[0] = -1 # a tuple can not be modified
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[169], line 1
----> 1 t[0] = -1 # a tuple can not be modified

TypeError: 'tuple' object does not support item assignment
In [164]:
nums = [1, 2, 3]
In [167]:
nums[0] = 0 # a list can be modified
In [166]:
nums
Out[166]:
[0, 2, 3]
In [170]:
name
Out[170]:
'python'
In [172]:
name[0] = "P" # string can not be edited
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[172], line 1
----> 1 name[0] = "P" # string can not be edited

TypeError: 'str' object does not support item assignment
In [173]:
(1, 2, 3) + [5, 6, 7]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[173], line 1
----> 1 (1, 2, 3) + [5, 6, 7]

TypeError: can only concatenate tuple (not "list") to tuple
In [179]:
(1, 2, 3) + (2, 3) # two tuples can be added, concatenation
Out[179]:
(1, 2, 3, 2, 3)
In [178]:
[1, 2, 3] + [5, 6, 7] # two lists can be added
Out[178]:
[1, 2, 3, 5, 6, 7]
In [180]:
t
Out[180]:
(1, 2, 3)
In [181]:
t[0]
Out[181]:
1

tuple and list addition does not do vector addition... it concatenates

In [182]:
t * 2 #
Out[182]:
(1, 2, 3, 1, 2, 3)
In [183]:
nums * 2
Out[183]:
[0, 2, 3, 0, 2, 3]
In [184]:
ones = [1]*5
text_ones = ["one"]*5
In [185]:
ones
Out[185]:
[1, 1, 1, 1, 1]
In [186]:
text_ones
Out[186]:
['one', 'one', 'one', 'one', 'one']

Last collection type, dictionary¶

Dictionary is also like a list, but in a list you access by location, In dictionary you can access by name

In [187]:
scores_list = [20, 19, 18, 16] 
names = ["Rupali", "Rupak", "Alice", "Kavya"]

index = 0
print(names[index], scores_list[index])
Rupali 20
In [188]:
scores = {"Rupali": 20, "Rupak": 19, "Alice": 18, "Kavya":16}
In [189]:
scores["Rupak"]
Out[189]:
19
In [190]:
scores['Alice"]
  Cell In[190], line 1
    scores['Alice"]
           ^
SyntaxError: unterminated string literal (detected at line 1)
In [191]:
scores['Alice']
Out[191]:
18
In [192]:
scores['Yash']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[192], line 1
----> 1 scores['Yash']

KeyError: 'Yash'
In [193]:
aadhar = {"1200 1233 1213": "Rupali",
          "2343 2323 2323" : "Rupak"}
In [194]:
aadhar['1200 1233 1213']
Out[194]:
'Rupali'

{ key1 : value1,
  key2 : value2,
  }

Keys can be (anything that can not be changed once created)

  • integers
  • strings
  • tuples

Keys can not be anything that can be edited

  • list

and values can be anything

In [195]:
aadhar = {"1200 1233 1213": ["Rupali", "Address1"],
          "2343 2323 2323" : ["Rupak", "Address2"]}
In [196]:
aadhar["1200 1233 1213"]
Out[196]:
['Rupali', 'Address1']
In [198]:
digits_words = {1: "one",
                2: "two",
               3: "three",
               4: "four"}
In [199]:
digits_words[1]
Out[199]:
'one'
In [200]:
nums = ["zero", "one", "two", "three", "four"]
In [202]:
nums[0] # nums is a dict or a list or tuple or a string?
Out[202]:
'zero'
In [203]:
digits = {"one" : 1,
            "two" : 2,
           "three" : 3,
           "four": 4}
In [204]:
digits["one"]
Out[204]:
1
In [205]:
scores
Out[205]:
{'Rupali': 20, 'Rupak': 19, 'Alice': 18, 'Kavya': 16}
In [207]:
scores[20] # you can't queary for a name whose value is given
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[207], line 1
----> 1 scores[20] # you can't queary for a name whose value is given

KeyError: 20
In [208]:
scores
Out[208]:
{'Rupali': 20, 'Rupak': 19, 'Alice': 18, 'Kavya': 16}

Question: can you give value as index in a dictionary

No, you can give only keys

Question: can you give location as index in a dictionary

No, you lost freedom with dict. you can use list of you want like that

which bracket to use when¶

  • [] is used to create a list
  • [] is used to access element from string, list, dict, tuple ..but in this you use it after variable name
  • () is used for calling a function, creating a tuple, as a parenthesis if it in a formula
  • {} is used for creating a dictionary, set
    • in dictinary you use key:value separated as elements while creating
    • in set you give only comma seperated elements in {}.
In [209]:
{1, 2, 3, 4, 5, 6, 7,4, 5, 6, 2, 1, 3}
Out[209]:
{1, 2, 3, 4, 5, 6, 7}
In [211]:
{3, 4, 5, 1} # order is not gauranteed... not to be used for sorting
Out[211]:
{1, 3, 4, 5}
In [212]:
s = {1, 2, 3, 4}
In [214]:
s[0] # because there no location in set .. there is only belongingness is there
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[214], line 1
----> 1 s[0] # because there no location in set .. there is only belongingness is there

TypeError: 'set' object is not subscriptable

Functions¶

  • Function has name
  • it has arguments (some functions have limited number of args, some can have any number of)
  • it returns some value
In [215]:
print(1, 2, 3)
1 2 3
In [216]:
print(1, 2, 3, 4, 5, 55, 5, 6, "Done")
1 2 3 4 5 55 5 6 Done
In [217]:
name
Out[217]:
'python'
In [218]:
names
Out[218]:
['Rupali', 'Rupak', 'Alice', 'Kavya']
In [219]:
scores
Out[219]:
{'Rupali': 20, 'Rupak': 19, 'Alice': 18, 'Kavya': 16}
In [220]:
t
Out[220]:
(1, 2, 3)
In [221]:
s
Out[221]:
{1, 2, 3, 4}
In [223]:
len(name) # len(string) returns number of chars in string
Out[223]:
6
In [230]:
size = len(name) # it is possible to save the return value of a function in a variable
In [225]:
size
Out[225]:
6
In [226]:
print(2, 3, 4, 5)
2 3 4 5
In [227]:
x = print(2, 3, 4, 5)
2 3 4 5
In [228]:
x
In [229]:
print(x)
None
In [231]:
len(s) # length of a set
Out[231]:
4
In [232]:
len(scores) # length of a dictionary
Out[232]:
4
In [233]:
len(t) # length of a tuple
Out[233]:
3
In [234]:
{1, 1, 1, 2, 3}
Out[234]:
{1, 2, 3}
In [235]:
scores
Out[235]:
{'Rupali': 20, 'Rupak': 19, 'Alice': 18, 'Kavya': 16}
In [236]:
len(scores)
Out[236]:
4
In [238]:
sum([1, 2, 3, 4])
Out[238]:
10
In [ ]: