Sep 20-24, 2021 Vikrant Patil
These notes are available online at https://notes.pipal.in/2021/arcesium_finop_batch2/
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from https://lab2.pipal.in for this training.
login to hub and create a notebook with name module1-day1
esc + m -> will convert code cell to markdown cell
esc + y -> code cell
shift + enter -> execute the cell contets
3+4
7
5+5
10
300+ 30
330
42*3
126
35/5
7.0
7.0 # float has a decimal point in it
7.0
7 # this integer
7
35/5 # this always results into a float
7.0
35//5 # integer division operator
7
3**100 # power operator
515377520732011331036461129765621272702107522001
"this is text data" # it starts with " ends with "
'this is text data'
'i can also have single quotes'
'i can also have single quotes'
"type in between"
'type in between'
"""some poem
which has got
multiple lines in it
this is called
multiline text"""
'some poem\nwhich has got\nmultiple lines in it\nthis is called\nmultiline text'
"*"*5 # * as a string/text multiply it by 5 as integer
'*****'
"hello"*3
'hellohellohello'
"hello" + "world" # + operator will concatenate the text data
'helloworld'
+,-,*,%,/,//,** these are different arithmatic operators and they have some priority by they will be executed in a statement
operator priority
======== ==========
** 1
* / // % 2
+ - 3
5+5*3/2
12.5
5*3+2**3/4
17.0
5*3+8/4
17.0
5*3+8/4
15+8/4
15+2
17
3*(3+4) # although * has higher priority I want to execute 3+4 first! ..brackets will do that
21
problems
"""multiline string is possible
only with three quote"""
'multiline string is possible\nonly with three quote'
"this is
not a multiline
text
"
File "<ipython-input-28-420da6d58e51>", line 1 "this is ^ SyntaxError: EOL while scanning string literal
2(1+3)
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? <>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? <ipython-input-29-005eb838b4eb>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? 2(1+3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-29-005eb838b4eb> in <module> ----> 1 2(1+3) TypeError: 'int' object is not callable
2*(1+3)
8
2 4 # between these two number you have to specify some operation!
File "<ipython-input-31-bac5aba3820b>", line 1 2 4 # between these two number you have to specify some operation! ^ SyntaxError: invalid syntax
2 + 4
6
2 (2+3)
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? <>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? <ipython-input-33-ed01990b99e8>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? 2 (2+3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-33-ed01990b99e8> in <module> ----> 1 2 (2+3) TypeError: 'int' object is not callable
2 * (2+3)
10
2^5 # binary logical bitwise operator xor
7
2**5
32
26800*(1+0.07/4)**(4*5)
37916.05564625544
x = 10 # you can store result into some name
x*2 # and recall it later
20
x here is called as variable
x = 20
x
20
20 # this is called literal
20
x # this is variable
20
vikrant = 10
"vikrant" # this is a literal text which has value "vikrant"
'vikrant'
vikrant
10
Here are some rules to name a variable
justaname = 0
with_some_more_details = 2
program_files = "whatever"
#interest_rate, number_of_years
first, second = "vikrant", "patil"
first
'vikrant'
second
'patil'
x = 10
y = x
x = x + 10
y
10
x = 10
y = x
y = 25
x
10
text = "hello" # it is collection of characters
text[0] # indexing
'h'
text[1]
'e'
text[2]
'l'
text[3]
'l'
text[4]
'o'
text[5]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-70-6c046ddfa74f> in <module> ----> 1 text[5] IndexError: string index out of range
text[-1] # last items?
'o'
text[-2] # second last item?
'l'
p y t h o n
-> 0 1 2 3 4 5
-6 -5 -4 -3 -2 -1 <---
[1, 2, 3]
[1, 2, 3]
numbers = [1, 2, 3, 4, 5] # start with [ and end ], in between specifiy the items with , as a seperator
x = 10
y = 20
z = 30
numbers = [x, y, z] # this code can be dynamic
words = ["hello","some", "words", "for", "lists"]
words[0] # indexing!
'hello'
words[1]
'some'
words[2]
'words'
words[3]
'for'
words[4]
'lists'
words[5]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-84-f6a2fb6dbef1> in <module> ----> 1 words[5] IndexError: list index out of range
words[-1]
'lists'
words[-2]
'for'
mixed = [1, 2, 3, "one", "two", "three"]
mixed[0]
1
mixed[-1]
'three'
complicated = [[1, 2, 3],["one","two","three"]]
complicated[0]# with tab you can complete you variable name
[1, 2, 3]
complicated[1]
['one', 'two', 'three']
complicated[0][0]
1
complicated[-1][-1]
'three'
ones = [1, 1, 1]
ones*5
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ones
[1, 1, 1]
ones + ones
[1, 1, 1, 1, 1, 1]
Program Files -> program files is not same! variable and function names are case sensitive
x
10
y
20
z
30
[x,y,z]
[10, 20, 30]
[M, N]
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-103-e394f61fe223> in <module> ----> 1 [M, N] NameError: name 'M' is not defined
ANY_VARAIABLE_WHICH_IS_NOT_DEFINED * 10
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-105-79ae9cb0426d> in <module> ----> 1 ANY_VARAIABLE_WHICH_IS_NOT_DEFINED * 10 NameError: name 'ANY_VARAIABLE_WHICH_IS_NOT_DEFINED' is not defined
["M","N"] # this is list of text "M" and "N"
['M', 'N']
[M,N] # this is list of variable M, N (which are defined hece we get error)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-109-ce1db31dbb9f> in <module> ----> 1 [M,N] # this is list of variable M, N (which are defined hece we get error) NameError: name 'M' is not defined
difference between tuple and lists is that tuples, once created can not be modified!
coordinate = (0, 0, 1)
coordinate[0]
0
coordinate[-1]
1
words
['hello', 'some', 'words', 'for', 'lists']
words[0] = "welcome"
words
['welcome', 'some', 'words', 'for', 'lists']
coordinate[1] = 10
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-116-4c662e91a943> in <module> ----> 1 coordinate[1] = 10 TypeError: 'tuple' object does not support item assignment
coordinate
(0, 0, 1)
are collections in which data is stored by name and not by index
scores = {"rupali":20, "alice":19, "maya":18, "kavya":20}
scores
{'rupali': 20, 'alice': 19, 'maya': 18, 'kavya': 20}
scores['alice'] # index is a name/text!
19
words[0] # index is an integer
'welcome'
portfolios = {"risky":["AT&T","AGILLENT","STGENOMICS"],
"stable":["IBM","APPLE","FORD"]
}
portfolios['risky']
['AT&T', 'AGILLENT', 'STGENOMICS']
portfolios['stable']
['IBM', 'APPLE', 'FORD']
albums = {"ARR":["Vande Mataram","Roja","Bombay"],
"Alisha": ["Made in India", "XYZ"]}
albums["ARR"]
['Vande Mataram', 'Roja', 'Bombay']
albums['Alisha']
['Made in India', 'XYZ']
stocks = {"risky": [{"name": "AT&T", "value": 150}, {"name":"Agillent", "value":160}],
"stable": [{"name": "IBM", "value": 123}]}
stocks['risky']
[{'name': 'AT&T', 'value': 150}, {'name': 'Agillent', 'value': 160}]
stocks['risky'][0]
{'name': 'AT&T', 'value': 150}
d = {[1, 2, 3]: 2} # list/dictionary can be given as key
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-136-05ea416d5a30> in <module> ----> 1 d = {[1, 2, 3]: 2} # list/dictionary can be given as key TypeError: unhashable type: 'list'
{2:[1, 2, 3, 4]}
{2: [1, 2, 3, 4]}
ALBUMS = {"ARR":["Vande Mataram","Roja","Bombay"],
"Alisha": ["Made in India", "XYZ"]}
ALBUMS['Alisha']
['Made in India', 'XYZ']
ALBUMS['Alisha']
['Made in India', 'XYZ']
x = 10
y = 21
z = 31
items = [x, y, z]
x,y,z
(10, 21, 31)
ALBUMS = {"ARR":["Vande Mataram","Roja","Bombay"],
"Alisha": ["Made in India", "XYZ"]}
ALBUMS['Alisha']
['Made in India', 'XYZ']
ALBUMS = {"ARR":["Vande Mataram","Roja","Bombay"],
"Alisha": ["Made in India", "XYZ"]}
ALBUMS['Alisha']
ALBUMS["ARR"]
['Vande Mataram', 'Roja', 'Bombay']
2 + 3
5
2+3
2*3
2**3 # it will show only last line
8
print(2+3)
print(2*3)
print(2**3)
5 6 8
print("Hello World!")
Hello World!
True
True
False
False
We have learnt to make staments, some complicated types ..lets look at slightly higher abstraction im which we try to bunch together multiple statemnts and give it a name. Using this name we can recompute all the statements those are stored inside it.
name = "Rupali"
len(name) # len is a function which returns length of collection
6
ones = [1, 1, 1, 1,1, 1, 1, 1]
len(ones)
8
point = (0,0)
len(point)
2
color = (255, 0, 0)
len(color)
3
stocks['risky'][1]
{'name': 'Agillent', 'value': 160}
stocks['risky'][1]['name']
'Agillent'
stocks['risky'][1]['value']
160
stock = {'name': 'IBM', 'open': 123, 'close':125, 'low':122, 'high':125}
len(stock) # number of pairs of key-value in the dictionary
5
len(5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-173-91fed648bb37> in <module> ----> 1 len(5) TypeError: object of type 'int' has no len()
len(555)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-174-78f9f1414c1d> in <module> ----> 1 len(555) TypeError: object of type 'int' has no len()
len("555")
3
len()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-176-adf3103c7c3e> in <module> ----> 1 len() TypeError: len() takes exactly one argument (0 given)
len(stocks)
2
x
10
x() # it will try to call x as function
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-179-13d3b24b5934> in <module> ----> 1 x() # it will try to call x as function TypeError: 'int' object is not callable
28700(2+3)
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? <>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? <ipython-input-180-cfde5e607c5d>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? 28700(2+3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-180-cfde5e607c5d> in <module> ----> 1 28700(2+3) TypeError: 'int' object is not callable
print(x)
10
print(x, y, z)
10 21 31
#add(2, 3) # this comma is a seperator between two arguments/parameters of a function
name ="Vikrant"
print("Hello", name)
Hello Vikrant
name = input("Enter your name and I will say hello to you!")
print("Hello", name)
Hello Arcesium batch II
input("Enter something") # input always returns a text value
'42'
'42'
'42'
x = input("give a number for squaring")
print(x*x)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-190-96cd4035da68> in <module> 1 x = input("give a number for squaring") ----> 2 print(x*x) TypeError: can't multiply sequence by non-int of type 'str'
"hello"*"hello"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-191-40fe09b6ff19> in <module> ----> 1 "hello"*"hello" TypeError: can't multiply sequence by non-int of type 'str'
"5" - > 5
int("5")
5
str_input = input("give a number for squaring")
x = int(str_input)
print(x*x)
3136
78 -> '78'
str(78)
'78'
r = input("rate of interest:")
r
'0.07'
r
'0.07'
len(r)
4
r[0]
'0'
r[1]
'.'
r[2]
'0'
r[3]
'7'
float(r)
0.07
len -> length of a collection
input -> it takes inpur from user as text
print -> it prints to standard output
int -> it converts str/anything to int
str -> converts floats, integers or anything else to string
float -> it converts str/int/or anything else to float
sum([2, 3, 4, 5])
14
sum([1.2, 3.4, 1, 2.5])
8.1
max(numbers)
30
numbers
[10, 20, 30]
problems
int("545")
545
int("xyz")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-208-ed77017b9e49> in <module> ----> 1 int("xyz") ValueError: invalid literal for int() with base 10: 'xyz'
incomes = (12323, 232343, 232324, 43434, 12000)
total_income = sum(incomes)
print("Total income:", total_income)
Total income: 532424
max_income = max(incomes)
print("Max income", max_income)
Max income 232343
2**200
1606938044258990275541962092341162602522202993782792835301376
len(2**200)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-213-f801ab36afac> in <module> ----> 1 len(2**200) TypeError: object of type 'int' has no len()
strnum = str(2**200)
len(strnum)
61
len(str(2**200)) # nested call
61
words
['welcome', 'some', 'words', 'for', 'lists']
words[0] # this returns single item
'welcome'
words[1:4] # start at index 1 , end at index 4 (excluding 4)
['some', 'words', 'for']
words[0:5:2] # start at 1, end at 5 but alternate item
['welcome', 'words', 'lists']
list[start:end:step]
words[:2] # take first 2 items
['welcome', 'some']
words[2:] # drop first two
['words', 'for', 'lists']
words[::2] # alternate
['welcome', 'words', 'lists']
words[::-1] # reverse
['lists', 'for', 'words', 'some', 'welcome']
words
['welcome', 'some', 'words', 'for', 'lists']
words[::]
['welcome', 'some', 'words', 'for', 'lists']
words[:] #copy
['welcome', 'some', 'words', 'for', 'lists']