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
© Pipal Academy LLP
3 + 4
7
Just like any programming language python supports basic data types to work with
42
42
42 + 56
98
42 * 2
84
42 ** 3 # this is comment ... it is power
74088
5 ** 2
25
5 // 2 # integer division
2
5 / 2 # division
2.5
2.5 # float , to represent real numbers
2.5
5.3 + 4.5
9.8
5.3 * 3.3
17.49
5.5 / 2.0
2.75
4 + 5 + 8.0 # anywhere if there is float in the expression it results into float
17.0
10%2 # remainder
0
11%2
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
2 + 3**2 # priority decides which operation is to be done first
11
4*(4+5)**2
324
"Hello this is text data"
'Hello this is text data'
2 + 2
4
2
2
"text"
'text'
'text'
'text'
'This is also text data'
'This is also text data'
Python also supports multiline strings
"""This is my first line of poem
this is second line
this is third line"""
'This is my first line of poem\nthis is second line\nthis is third line'
'''you can also
give single tripple
quote ..and that
also makes a
multiline string'''
'you can also \ngive single tripple\nquote ..and that\nalso makes a \nmultiline string'
'just like this text..multiline string is stored internally!'
'just like this text..multiline string is stored internally!'
'just like this text.\nmultiline string is stored internally!'
'just like this text.\nmultiline string is stored internally!'
some special chars
\n new line
\t means tab
\\
"c:\\program files\\python\\bin\\python.exe"
'c:\\program files\\python\\bin\\python.exe'
"\\n"
'\\n'
print("Hello World!") # this is function to print something on screen
Hello World!
print("""line1
line2
line3""")
line1 line2 line3
print(4 + 2)
6
4 + 2 # this is interpreter's response is given only last line of code
6
4 * 1
4 * 2
4 * 3
4 * 4 # response of interpreter is only for last line
16
print(4 * 1)
print(4 * 2)
print(4 * 3)
print(4 * 4)
4 8 12 16
"""line1
line2
line3""" # interpreter responds back with the object
'line1\nline2\nline3'
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
4 +
Cell In[48], line 1 4 + ^ SyntaxError: invalid syntax
4 + 5
9
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
print() # calling function print
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
26780*(1 + 0.007/4)**(4*5)
27733.047453680432
We talked about mathematical operators.. but we did not talk about =
x = 10 # it means save 10 (integer!) and whenever I recall with x ..I should get back 10
x
10
x + 2
12
x is called as variable
10 is called as literal
10
10
name = "python"
name # variable
'python'
"name" # this is not a variable, it is literal which
'name'
x = 10
"x" # this literal for text data 'x'
'x'
x
10
vikrant = 20
vikrant # without any quotes...
20
"vikrant" # it is text data... it is literal
'vikrant'
square2 = 2*2
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
square_2 = 2**2
square2 = 2**2
file_path = "c:\\program files"
a, b = 2, 3
print(a)
2
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?
x = 10
y = x
x = x + 10
y
10
x
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
s = "hello"
s * 2# in python you can multiply by ints .. it will repeat the text data those many times
'hellohello'
"*"*5
'*****'
first = "pyhton"
last = "programmer"
first + last # it concatenates the string
'pyhtonprogrammer'
s
'hello'
first
'pyhton'
last
'programmer'
first + last
'pyhtonprogrammer'
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
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'
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'
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'
s
'hello'
s[0] # in python this where it starts
'h'
s[1]
'e'
s[3]
'l'
s[4]
'o'
s[5]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[105], line 1 ----> 1 s[5] IndexError: string index out of range
s[-1] # last char
'o'
s[-2] # second last char
'l'
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 <-
Other than basic data types python also supports some collections
ones = [1, 1, 1, 1]
ones # this is a list
[1, 1, 1, 1]
ones[0] # zeroth item from ones
1
numbers = [2, 1, 4, 3, 6]
numbers[0]
2
numbers[1]
1
numbers[3]
3
numbers[-1]
6
names = ['Aakash', 'Yash', 'Saketh', 'Avinash'] # list containing words
names[0] # zeroth item from the list
'Aakash'
names[-1]
'Avinash'
mixed_list = [1, "one", 2 , "two", 3, "three"]
mixed_list[0]
1
mixed_list[1]
'one'
mixed_list = [1, "one", 2 , "two", 3, "three",]
mixed_list
[1, 'one', 2, 'two', 3, 'three']
mixed_list = [1, "one", 2 , "two", 3, "three",,]
Cell In[131], line 1 mixed_list = [1, "one", 2 , "two", 3, "three",,] ^ SyntaxError: invalid syntax
empty = ""
None # special data .. just like null ..nothing
[1, 2, 3, None, 4, 5]
[1, 2, 3, None, 4, 5]
items = [1, "one", 2, "two", None, 5, "five"]
items
[1, 'one', 2, 'two', None, 5, 'five']
items[-3] # interprenter will not respond for None
print(items[-3])
None
empty_text = ""
empty_list = []
complex_list = [[1, 2, 3], ["one", "two", "three"]]
complex_list[0] # what will this be?
[1, 2, 3]
complex_list
[[1, 2, 3], ['one', 'two', 'three']]
complex_list[0][0] # what will this give
1
complex_list[0][-1]
3
numeric, text = complex_list[0][0], complex_list[1][0]
print(numeric, text)
1 one
index = 0
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
1 one
index = 1
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
2 two
index = 2
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
3 three
numbers, text # this is something tuple
([2, 1, 4, 3, 6], 'three')
1, 2, 3, 4 # this is not a function call .. it actually creates a tuple
(1, 2, 3, 4)
print(1, 2, 3, 4) # this is function call
1 2 3 4
print(1, 2, 3, 4, sep=",")
1,2,3,4
t = (1, 2, 3)
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
nums = [1, 2, 3]
nums[0] = 0 # a list can be modified
nums
[0, 2, 3]
name
'python'
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
(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
(1, 2, 3) + (2, 3) # two tuples can be added, concatenation
(1, 2, 3, 2, 3)
[1, 2, 3] + [5, 6, 7] # two lists can be added
[1, 2, 3, 5, 6, 7]
t
(1, 2, 3)
t[0]
1
tuple and list addition does not do vector addition... it concatenates
t * 2 #
(1, 2, 3, 1, 2, 3)
nums * 2
[0, 2, 3, 0, 2, 3]
ones = [1]*5
text_ones = ["one"]*5
ones
[1, 1, 1, 1, 1]
text_ones
['one', 'one', 'one', 'one', 'one']
Dictionary is also like a list, but in a list you access by location, In dictionary you can access by name
scores_list = [20, 19, 18, 16]
names = ["Rupali", "Rupak", "Alice", "Kavya"]
index = 0
print(names[index], scores_list[index])
Rupali 20
scores = {"Rupali": 20, "Rupak": 19, "Alice": 18, "Kavya":16}
scores["Rupak"]
19
scores['Alice"]
Cell In[190], line 1 scores['Alice"] ^ SyntaxError: unterminated string literal (detected at line 1)
scores['Alice']
18
scores['Yash']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[192], line 1 ----> 1 scores['Yash'] KeyError: 'Yash'
aadhar = {"1200 1233 1213": "Rupali",
"2343 2323 2323" : "Rupak"}
aadhar['1200 1233 1213']
'Rupali'
{ key1 : value1,
key2 : value2,
}
Keys can be (anything that can not be changed once created)
Keys can not be anything that can be edited
and values can be anything
aadhar = {"1200 1233 1213": ["Rupali", "Address1"],
"2343 2323 2323" : ["Rupak", "Address2"]}
aadhar["1200 1233 1213"]
['Rupali', 'Address1']
digits_words = {1: "one",
2: "two",
3: "three",
4: "four"}
digits_words[1]
'one'
nums = ["zero", "one", "two", "three", "four"]
nums[0] # nums is a dict or a list or tuple or a string?
'zero'
digits = {"one" : 1,
"two" : 2,
"three" : 3,
"four": 4}
digits["one"]
1
scores
{'Rupali': 20, 'Rupak': 19, 'Alice': 18, 'Kavya': 16}
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
scores
{'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
{1, 2, 3, 4, 5, 6, 7,4, 5, 6, 2, 1, 3}
{1, 2, 3, 4, 5, 6, 7}
{3, 4, 5, 1} # order is not gauranteed... not to be used for sorting
{1, 3, 4, 5}
s = {1, 2, 3, 4}
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
print(1, 2, 3)
1 2 3
print(1, 2, 3, 4, 5, 55, 5, 6, "Done")
1 2 3 4 5 55 5 6 Done
name
'python'
names
['Rupali', 'Rupak', 'Alice', 'Kavya']
scores
{'Rupali': 20, 'Rupak': 19, 'Alice': 18, 'Kavya': 16}
t
(1, 2, 3)
s
{1, 2, 3, 4}
len(name) # len(string) returns number of chars in string
6
size = len(name) # it is possible to save the return value of a function in a variable
size
6
print(2, 3, 4, 5)
2 3 4 5
x = print(2, 3, 4, 5)
2 3 4 5
x
print(x)
None
len(s) # length of a set
4
len(scores) # length of a dictionary
4
len(t) # length of a tuple
3
{1, 1, 1, 2, 3}
{1, 2, 3}
scores
{'Rupali': 20, 'Rupak': 19, 'Alice': 18, 'Kavya': 16}
len(scores)
4
sum([1, 2, 3, 4])
10