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

Jun 20-24, 2022 Vikrant Patil

All notes are available online at https://notes.pipal.in/2022/arcesium_finop_batch1/

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

https://engage.pipal.in/

© Pipal Academy LLP

multiply two and five and write down result in your notebook!

In [1]:
2*3
Out[1]:
6

Working with jupyter notebook¶

In [2]:
If you try to type notes in this
  Input In [2]
    If you try to type notes in this
       ^
SyntaxError: invalid syntax
keys action
esc+m convert the cell into markdown cell
esc+y cpnvert the cell into code cell
shift+enter execute current cell
esp+b insert new cell after current cell
!command execute system command

Header1¶

Header2¶

Header3¶

  1. we will learn first how to use jupyter notebook
  2. then we will start learning about python
  3. we will write programs
  • item1
  • item2
  • item3
  • item4

Numeric And Text data¶

Very way primitive way of programming is manipulating numbers and text. To manipulate numbers python has got basic numeric capabilities.

integers

In [1]:
42 + 42
Out[1]:
84
In [2]:
42 - 42
Out[2]:
0
In [3]:
42 / 2
Out[3]:
21.0
In [4]:
42**2 # comment ..** is for power
Out[4]:
1764
In [5]:
2**5
Out[5]:
32
In [6]:
5 % 2 # modulus operator
Out[6]:
1
In [11]:
5 // 2 
Out[11]:
2
In [8]:
42 // 2
Out[8]:
21

Numeric data in python has two forms ..integers and floats

float

In [9]:
1.1 + 1.1
Out[9]:
2.2
In [10]:
1 + 1.1 # it results in float
Out[10]:
2.1
In [12]:
5.0 * 3
Out[12]:
15.0
In [13]:
5.0 / 2.0
Out[13]:
2.5
In [14]:
5.0 // 2.0 
Out[14]:
2.0
In [15]:
2.0 ** 5
Out[15]:
32.0
In [16]:
2**5
Out[16]:
32
In [17]:
100**5
Out[17]:
10000000000

There is certain priority about the mathematical operation, which will be executed first!

In [19]:
2 + 3*5 # this means * has higher priority
Out[19]:
17
In [20]:
2 + 2**5
Out[20]:
34
In [22]:
3 * 2**5 # among * and ** , ** has higher priority
Out[22]:
96
2**5%5/2*7
32%5/2*7
2/2*7
1*7
7
operator priority
** 1
% // / * 2
+ - 3

And if we want to change the priority, then we make use of braces to make sure that relevant part is executed first

In [23]:
7 + 2 * 3
Out[23]:
13
In [24]:
(7+2)*8
Out[24]:
72

string

In [26]:
"Hello, this is text data"
Out[26]:
'Hello, this is text data'
In [27]:
2
Out[27]:
2
In [28]:
'text'
Out[28]:
'text'
In [29]:
"text"
Out[29]:
'text'
In [30]:
"text"*2
Out[30]:
'texttext'
In [31]:
"ten"*10
Out[31]:
'tentententententententententen'
In [32]:
"first" + "second"
Out[32]:
'firstsecond'
In [33]:
"vikrant" + "patil"
Out[33]:
'vikrantpatil'
In [35]:
"vikrant" - "vikr" # it does not supprt substraction
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [35], in <cell line: 1>()
----> 1 "vikrant" - "vikr"

TypeError: unsupported operand type(s) for -: 'str' and 'str'
In [36]:
"*"*50
Out[36]:
'**************************************************'

As told earlier python is higher level language. i.e it is very close to english, human language! So python can write poems

In [37]:
"""The Zen of Python, by Tim Peters

Beautiful is better than ugly
Explicit is better than implicit
Simple beteer than complex
"""
Out[37]:
'The Zen of Python, by Tim Peters\n\nBeautiful is better than ugly\nExplicit is better than implicit\nSimple beteer than complex\n'
In [38]:
'''Flat is better than nested
Sparse is better than dense
Redability counts'''
Out[38]:
'Flat is better than nested\nSparse is better than dense\nRedability counts'
In [39]:
"text\thello"
Out[39]:
'text\thello'

problems

  • Use python to convert asset value , 20345.5 originally given in EUR to INR
  • Compound is interst is calculated using P (1 + r/n)^(nt). In this formula P is principal amount, r is rate of interest per annum, n denotes the number of times the interest gets compounded and t denotes the number of yyess. Use python to compute compound interest for principle of 26780, rate of interest 7%, interest is compounded quarterlym and investment is done for 5 years
In [41]:
20345.5*86.66
Out[41]:
1763141.03
In [42]:
2(3+4)
<>: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_3772/679493015.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2(3+4)
/tmp/ipykernel_3772/679493015.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2(3+4)
/tmp/ipykernel_3772/679493015.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2(3+4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [42], in <cell line: 1>()
----> 1 2(3+4)

TypeError: 'int' object is not callable
In [43]:
2*(3+4)
Out[43]:
14
In [45]:
26780*(1+0.07/4)**(5*4)
Out[45]:
37887.76008234032

Variable and literals¶

In [46]:
2 # integer ....literally 2!! 
Out[46]:
2
In [47]:
45 # this is called literal
Out[47]:
45
In [49]:
x = 10 # = is called as assignment operator
In [51]:
x # interpreter reponds back with what x is pointing to! or what is stored inside x! 
Out[51]:
10
In [58]:
P = 50000
r = 0.07
t = 4
n = 5
In [59]:
P*(1+r/t)**(n*t)
Out[59]:
70738.90978778999

x,P,r,t, and n are called as variables

In [61]:
26780 # just a literal
Out[61]:
26780
In [62]:
"hello" # literal string
Out[62]:
'hello'
In [63]:
greeting = "hello"
In [64]:
greeting # is a variable
Out[64]:
'hello'
In [65]:
ten = 10 # is a variable
In [67]:
"ten" # is not a variable
Out[67]:
'ten'
In [68]:
ten
Out[68]:
10
In [69]:
"ten"
Out[69]:
'ten'

Q: What can be used as a variable?

In [70]:
2 = 5
  Input In [70]
    2 = 5
    ^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
In [71]:
- = 5
  Input In [71]
    - = 5
      ^
SyntaxError: invalid syntax
In [72]:
test = 54
In [73]:
test232 = 3434
In [74]:
y-z = 5
  Input In [74]
    y-z = 5
    ^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
In [75]:
greet_hello = "hello"

Rules

  • The variable name can't start with a number ..so it can't be a number also
  • the variable can not have operators or quotes in it
  • it can be single word (meaning no hyphen or no space)
  • it can have alphabets, numbers and underscore
In [76]:
a = 54
In [77]:
a,b = 2,3
In [78]:
a
Out[78]:
2
In [79]:
b
Out[79]:
3

Problem

Have a look at following statements. what will be value of y after thi

In [84]:
x = 10
y = x
x = x + 10
In [81]:
y
Out[81]:
10
In [82]:
x = 10
y = x
y = 25

What will be value of x after this?

In [83]:
x
Out[83]:
10

Lets work with text data that we have variables

In [85]:
s = "hello"
In [87]:
s[0] # first item from the text data .. in python first starts at zero
Out[87]:
'h'
In [90]:
s[4] # item (charecter) at forth index
Out[90]:
'o'
In [91]:
s[-1] # last element!
Out[91]:
'o'

The indices work like this in python

+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
->0   1   2   3   4   5   
 -6  -5  -4  -3  -2  -1  <-
In [92]:
p = "Python"
In [93]:
p[0]
Out[93]:
'P'
In [94]:
p[1]
Out[94]:
'y'
In [95]:
p[-1]
Out[95]:
'n'
In [96]:
p[-6]
Out[96]:
'P'
In [97]:
p[100]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [97], in <cell line: 1>()
----> 1 p[100]

IndexError: string index out of range
In [99]:
p[-7]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [99], in <cell line: 1>()
----> 1 p[-7]

IndexError: string index out of range

Collections¶

Collection of basic data into some format of array or column or row...

list

In [100]:
[1, 1, 1] # this is list of three 1s
Out[100]:
[1, 1, 1]
In [101]:
ones = [1, 1, 1, 1]
In [103]:
ones[0] # indexing works just like text data
Out[103]:
1
In [104]:
nums = [1, 2, 3, 4, 5]
In [105]:
nums[0]
Out[105]:
1
In [106]:
nums[-1]
Out[106]:
5
In [107]:
words = ["hello", "howdy", "welcome", "hiee"]
In [108]:
words[0]
Out[108]:
'hello'
In [109]:
words[-1]
Out[109]:
'hiee'
In [110]:
words
Out[110]:
['hello', 'howdy', 'welcome', 'hiee']
In [111]:
ones
Out[111]:
[1, 1, 1, 1]
In [112]:
nums
Out[112]:
[1, 2, 3, 4, 5]
In [113]:
mixed = [5, 2, "one", "two" , "hello"]
In [114]:
mixed
Out[114]:
[5, 2, 'one', 'two', 'hello']
In [115]:
mixed[0]
Out[115]:
5
In [116]:
mixed[1]
Out[116]:
2
In [117]:
mixed[3]
Out[117]:
'two'
In [118]:
matrix = [[1, 2, 3],[4,5,6],[7,8,9]]
In [119]:
matrix
Out[119]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [120]:
matrix[0]
Out[120]:
[1, 2, 3]
In [121]:
matrix[1]
Out[121]:
[4, 5, 6]
In [122]:
matrix[2]
Out[122]:
[7, 8, 9]
In [124]:
matrix[0][0]
Out[124]:
1
In [125]:
matrix[1][2]
Out[125]:
6
In [126]:
matrix[2][2]
Out[126]:
9
In [127]:
matrix[-1][-1]
Out[127]:
9
In [128]:
words
Out[128]:
['hello', 'howdy', 'welcome', 'hiee']
In [129]:
"hello" + "welcome"
Out[129]:
'hellowelcome'
In [131]:
words + nums # it concatenates
Out[131]:
['hello', 'howdy', 'welcome', 'hiee', 1, 2, 3, 4, 5]
In [134]:
words*2 # repeates the list two times
Out[134]:
['hello', 'howdy', 'welcome', 'hiee', 'hello', 'howdy', 'welcome', 'hiee']
In [133]:
words
Out[133]:
['hello', 'howdy', 'welcome', 'hiee']

problem can you create a list with 100 ones in it?

In [137]:
one = [1]
ones_100 = one*100 # this will have 100 ones in it
In [139]:
words
Out[139]:
['hello', 'howdy', 'welcome', 'hiee']
In [141]:
words[0]
Out[141]:
'hello'
In [142]:
words[0] = "HELLO"
In [ ]:
words
Out[ ]:
['HELLO', 'howdy', 'welcome', 'hiee']
In [144]:
words[-1] = "Hi"
In [145]:
words
Out[145]:
['HELLO', 'howdy', 'welcome', 'Hi']

tuple

In [147]:
color = (0, 0, 256) # instaed if square bracket we use round bracket
In [148]:
color*2
Out[148]:
(0, 0, 256, 0, 0, 256)
In [150]:
color + color # concatenates
Out[150]:
(0, 0, 256, 0, 0, 256)
In [151]:
color[0]
Out[151]:
0
In [152]:
color[-1]
Out[152]:
256
In [153]:
color[0] = 255
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [153], in <cell line: 1>()
----> 1 color[0] = 255

TypeError: 'tuple' object does not support item assignment

tuple is immutable object! that means something that is once created can not be modified

dictionary

is collection of key and value pairs. Keys have to be uniue. Values need not be unique.

In [154]:
score = {"Sachin": 20,
        "Sanskruti": 23,
        "Shila": 20,
        "Vedant":22,
        "Tom": 21}
In [155]:
score['Tom']
Out[155]:
21
In [156]:
score['Vedant']
Out[156]:
22
In [157]:
stock = {"Name":"IBM", "value":123, "count": 250}
In [158]:
stock['Name']
Out[158]:
'IBM'
In [159]:
stock['value']
Out[159]:
123

Boolean

In [160]:
True
Out[160]:
True
In [161]:
False
Out[161]:
False

None

In [162]:
None
In [163]:
"" # empty string
Out[163]:
''
In [164]:
[] # empty list
Out[164]:
[]

Key things to remember¶

  • to make a list we make use of square bracket []
  • to make a string we make use of double or single quotes
  • to create multiline string we use tripple quotes
  • to make tuple we use round braces ()
  • to make a dictionary we make use of curley brackets{}, inside you give pairs of key:value separated by comma
  • to access item from string, list , tuple we use square bracket and index inside quare bracket list[1],string[2],tuple[2]
  • to access item from dictionary we use square bracket and key inside it. d['key']

Functions¶

In [166]:
name = "Rupali"
In [167]:
len(name) # len is a function which is getting called in this statement with a parameter name
Out[167]:
6
In [170]:
2()
<>: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_3772/1017438639.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2()
/tmp/ipykernel_3772/1017438639.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2()
/tmp/ipykernel_3772/1017438639.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [170], in <cell line: 1>()
----> 1 2()

TypeError: 'int' object is not callable
In [171]:
nums
Out[171]:
[1, 2, 3, 4, 5]
In [172]:
len(nums)
Out[172]:
5
In [173]:
len(words)
Out[173]:
4
In [174]:
len([1]*100)
Out[174]:
100
In [175]:
stock
Out[175]:
{'Name': 'IBM', 'value': 123, 'count': 250}
In [177]:
len(stock)
Out[177]:
3
In [178]:
"23" # this is text not a number
Out[178]:
'23'
In [180]:
"23"*2 # will not work as a mathematical statement
Out[180]:
'2323'
In [181]:
int("23")*2
Out[181]:
46
In [182]:
int("343543")
Out[182]:
343543
In [183]:
int("test")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [183], in <cell line: 1>()
----> 1 int("test")

ValueError: invalid literal for int() with base 10: 'test'
In [184]:
int("2.3")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [184], in <cell line: 1>()
----> 1 int("2.3")

ValueError: invalid literal for int() with base 10: '2.3'
In [185]:
int(2.3)
Out[185]:
2
In [186]:
str(2312132432432)
Out[186]:
'2312132432432'
In [187]:
!ls
index.html  index.ipynb  Makefile  module1-day1.html  module1-day1.ipynb  push
In [ ]: