Jan 16-20, 2023 Vikrant Patil
All notes are available online at https://notes.pipal.in/2023/arcesium_finop_jan/
Please login to https://engage.pipal.in/ and launch jupyter lab
For today create a notebook with name module1-day2
notebook names are case sensitive. Make sure you give correct name
© Pipal Academy LLP
built-in functions. These functions are already there in python installtion
print("Hello World")
Hello World
len([1, 2, 3, 4])
4
len((1, 2, 3, 4, 5))
5
len("hello this is text")
18
len("""one
two""")
7
stock = {"ticker":"IBM", "value":125, "high":126, "low": 123}
len(stock)
4
len({1, 2, 4, 3, 3, 3, 3, 3, 3})
4
sum([1, 2, 3, 4])
10
sum((1, 3, 4, 5))
13
str(11) # will convert 11 into charecter '11'
'11'
11
11
int("123")
123
x = input("Give value for x")
x # every input is always text! even if you give input as a number
'555'
int(x)
555
x * 3
'555555555'
int(x)*3
1665
x # this is string...interpreter response is showing '' around it
'555'
print(x) # print does not show quotes
555
print(555)
555
print("555")
555
int(x) # is doing not changin x ... but it will make new int by using chars from x
555
y = int(x)
type(y)
int
type(x)
str
int(x) # calling int on x does not change x
555
stock
{'ticker': 'IBM', 'value': 125, 'high': 126, 'low': 123}
stock['value'] = 126
stock
{'ticker': 'IBM', 'value': 126, 'high': 126, 'low': 123}
basic data types (these are immutable)
higher level data types
list("2323") # makes a list of chars
['2', '3', '2', '3']
list((1, 2, 3, 4))
[1, 2, 3, 4]
list(stock) # list of only keys
['ticker', 'value', 'high', 'low']
list({1, 1, 1, 2, 2, 3, 4})
[1, 2, 3, 4]
names = ["Yash", "Akash", "Alice", "Alex"]
language = ["Hindi", "Telugu", "English", "Spanish"]
list(zip(names, language)) # nested function call!
[('Yash', 'Hindi'),
('Akash', 'Telugu'),
('Alice', 'English'),
('Alex', 'Spanish')]
pairs = zip(names, language)
list(pairs)
[('Yash', 'Hindi'),
('Akash', 'Telugu'),
('Alice', 'English'),
('Alex', 'Spanish')]
list(zip(names, language)) # innermost function will be called first. Then output of that will be passed as an argument to outer function
[('Yash', 'Hindi'),
('Akash', 'Telugu'),
('Alice', 'English'),
('Alex', 'Spanish')]
print(1 + int(input("give integer")))
# input will be called first ..because it is inner most function
# then int will be called with the argument as output of input function
# 1 + output of int will be computed
# print will called with result as argument
7
dict([(1,"one"), (2,"two")])
{1: 'one', 2: 'two'}
dict([[1,"one"], [2,"two"]]) # it will take first item from pair as key and second item as value
{1: 'one', 2: 'two'}
dict(zip(names, language))
{'Yash': 'Hindi', 'Akash': 'Telugu', 'Alice': 'English', 'Alex': 'Spanish'}
We saw following built in functions
problems
what data structure (higher level data type to store this data?) 2. Find out how many digits are there in 2$^3$$^2$ 3. Find out maximum income from above sources of income in problem 1 (there is a built in function called max/min which find max/min from a list/tuple..any sequence) 4. sum(["a", "b", "c", "d"]) will this work?
sum(1, 2, 3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[54], line 1 ----> 1 sum(1, 2, 3) TypeError: sum() takes at most 2 arguments (3 given)
sum([1, 2, 3])
6
incomes = [122335, 120000, 5444560, 112000, 55000]
55 # it is integer
55
'55'
'55'
2**32
4294967296
x = 10
y = 20
x = 30
sum = 54
sum
54
sum([1, 2, 3, 4])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[66], line 1 ----> 1 sum([1, 2, 3, 4]) TypeError: 'int' object is not callable
del sum
getting back the built in
in case by mistake you named a variable same as built in function , then will not be able to the function unless you delete the variable
sum([1, 2, 3])
6
str(2**32) # this converts integer into decimal digits as string chars
'4294967296'
2**32 # internally integers/floats are stored in some binary representation. Here interpreter showing digits only for our convinience
4294967296
len(str(2**32))
10
0 + "a"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[74], line 1 ----> 1 0 + "a" TypeError: unsupported operand type(s) for +: 'int' and 'str'
["a", "b", "c", "d"]
0 + "a"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[75], line 2 1 ["a", "b", "c", "d"] ----> 2 0 + "a" TypeError: unsupported operand type(s) for +: 'int' and 'str'
["a", "b", "c", "d"]
"" + "a"
'a'
help(sum)
Help on built-in function sum in module builtins:
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
sum(["a", "b", "c", "d"], start="")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[78], line 1 ----> 1 sum(["a", "b", "c", "d"], start="") TypeError: sum() can't sum strings [use ''.join(seq) instead]
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits = list(range(10))
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
len(digits)
10
digits[0] # zeroth element
0
digits[2:6] # give elements from index 2 till index 5 (exclide 6)
[2, 3, 4, 5]
digits[:5] # take first five
[0, 1, 2, 3, 4]
digits[3:] # drop first three
[3, 4, 5, 6, 7, 8, 9]
digits[2:9:2] # start at index 2 go till 8 (exlude 9) and at step of 2
[2, 4, 6, 8]
digits[2::2]
[2, 4, 6, 8]
first_100 = list(range(100))
first_100[:5] # head
[0, 1, 2, 3, 4]
first_100[-5:] # tail
[95, 96, 97, 98, 99]
sum(first_100[2::2])
2450
most generic format of list slicing is
items[start:end:step]
anything not given is taken as default
start is not given then start is taken 0
end is not given then end is taken as end of list
step is not given then step is taken as 1
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]*20
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
"[1, 2, 3, 4, 5]" # this is not a list it is just text with some chars in it
'[1, 2, 3, 4, 5]'
list("[1, 2, 3, 4, 5]")
['[', '1', ',', ' ', '2', ',', ' ', '3', ',', ' ', '4', ',', ' ', '5', ']']
x = 10
y = 20
z = 30
m = 45
n = 5
[x, y, z, m, n]
[10, 20, 30, 45, 5]
11111
11111
digits[::] # copy
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[::-1] # reverse
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
text = "This is some text data to work with slicing"
text[5:] # drop first five
'is some text data to work with slicing'
text[:5] # take first five chars
'This '
text[-5:] # take last five char
'icing'
text[:-5] # take everyting except last 5
'This is some text data to work with sl'
text[::-1] # reverse
'gnicils htiw krow ot atad txet emos si sihT'
del x # del is a statement, after this you give space and variable name you want to delete
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[119], line 1 ----> 1 del x # del is a statement, after this you give space and variable name you want to delete NameError: name 'x' is not defined
complex_list = [[1, 2, 3], ["one", "two", "three"]]
index = 1
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
2 two
add
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[117], line 1 ----> 1 add NameError: name 'add' is not defined
def add(a, b): # don't miss this colon
return a + b # body function should be indented by 4 spaces (convention)
add
<function __main__.add(a, b)>
add(2, 3)
5
complex_list = [[1, 2, 3], ["one", "two", "three"]]
index = 0
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
1 one
complex_list = [[1, 2, 3], ["one", "two", "three"]]
index = 1
numeric, text = complex_list[0][index], complex_list[1][index]
print(numeric, text)
2 two
def get_pair_from_complex_list(pair_list, index):
return pair_list[0][index], pair_list[1][index] # it will return two items at a time as tuple
get_pair_from_complex_list(complex_list, 0)
(1, 'one')
get_pair_from_complex_list(complex_list, 1)
(2, 'two')
get_pair_from_complex_list(complex_list, 2)
(3, 'three')
def square(a):
return a*a
def sumofsquares(x, y):
x_2 = square(x)
y_2 = square(y)
return x_2 + y_2
Cell In[132], line 6 y_2 = square(y) ^ IndentationError: unexpected indent
def square(a):
return a*a
def sumofsquares(x, y):
x_2 = square(x)
y_2 = square(y)
return x_2 + y_2
Cell In[133], line 8 return x_2 + y_2 ^ SyntaxError: 'return' outside function
return 5
Cell In[134], line 1 return 5 ^ SyntaxError: 'return' outside function
def square(a):
return a*a
def sumofsquares(x, y):
x_2 = square(x)
y_2 = square(y)
return x_2 + y_2
sumofsquares(5, 6)
61
def print_double(x):
print(2*x)
print_double(3) # but this is a print not return!
6
x = print_double(5)
10
print(x)
None
s = sumofsquares(5, 7)
print(s)
74
def double1(x):
2*x
double1(5)
Function names can be just like variable names
def add(2, 3): # incorrect, it should be variable name
return 2 +3
def add("a", "b"): # incorrect, it should be variable and not a literal
return a + b
Cell In[146], line 1 def add(2, 3): # incorrect, it should be variable name ^ SyntaxError: invalid syntax
add(2, 3)
5
x = 56
y = 47
add(x, y)
103
def mysum(*args): # this means any number of arguments
return sum(args)
mysum(1, 2, 3, 4)
10
mysum(1, 2)
3
def simple_interest(P, N=5, R=5.5): # default args
return P*N*R/100
simple_interest(5000)
1375.0
simple_interest(10000)
2750.0
simple_interest(10000, N=10, R=6)
6000.0
simple_interest(10000,10,6)
6000.0
def print_double(x):
print(2*x) # is printing but not returning
# if function does not return anything it reurns None
print(print_double(5)) # None is coming from outside print statement and 10 is coming from print inside print_double function
10 None
print_double(5) # this is not returning 10! it is
10
x = print_double(5)
10
print(x)
None
def foo(x):
x**2 # if there is not return statement , function will return None on its own...
foo(4)
print(foo(4))
None