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
© Pipal Academy LLP
int("42")
42
str(56565)
'56565'
str([1,2,3,4])
'[1, 2, 3, 4]'
[1,2,3,4]
[1, 2, 3, 4]
float("23.5")
23.5
float(42)
42.0
x = 10
type(x) # this will tell what is type (data) of value stored at x
int
type(42.3)
float
type([1, 2, 3, 4])
list
type((1,23,3))
tuple
stock = {"name":"IBM", "open":123, "high":125, "low": 120}
type(stock)
dict
type("text")
str
list("text")
['t', 'e', 'x', 't']
range(5) # numbers starting from 0 till less than 5
range(0, 5)
range5 = range(5)
list(range5)
[0, 1, 2, 3, 4]
max([23, 25, 50, 8734,1,2,3]) # finds out maximum number from given list
8734
max((1, 2, 3, 45))
45
d = {1:"one", 2:"two", 3:"three"}
d
{1: 'one', 2: 'two', 3: 'three'}
max(d) # it will find maximum from only keys of a dictionary
3
nums = [34, 67, -1, 45, 23, 2, 3]
min(nums)
-1
sum(nums) # finds sum of all numbers form the list
173
print("hello")
hello
print("Hello world!")
Hello world!
name = input("What is your name?")
print(name)
vikrant
sorted(nums)
[-1, 2, 3, 23, 34, 45, 67]
sorted_nums = sorted(nums) # here I am storing output returned by function sorted into a variable called sorted_nums
nums
[34, 67, -1, 45, 23, 2, 3]
sorted_nums
[-1, 2, 3, 23, 34, 45, 67]
numsx = [1, 2, 3, 4]
sorted(numsx)
[1, 2, 3, 4]
problems
sum(["a","b","c","d"])"a" + "b"
'ab'
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.
strdigits = str(2**42)
strdigits
'4398046511104'
len(strdigits)
13
strdigits
'4398046511104'
a = 2**42
incomes = [123330, 250000, 45555, 232130, 11123]
sum(incomes) # total income
662138
max(incomes)
250000
sum(["a","b","c","d"])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [63], in <cell line: 1>() ----> 1 sum(["a","b","c","d"]) TypeError: unsupported operand type(s) for +: 'int' and 'str'
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
sum(["a","b","c","d"], start="")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [67], in <cell line: 1>() ----> 1 sum(["a","b","c","d"], start="") TypeError: sum() can't sum strings [use ''.join(seq) instead]
"" + "a" + "b" + "c"
'abc'
digits = [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]
digits_
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x = range(10)
x
range(0, 10)
x[0]
0
list((1, 2, 3))
[1, 2, 3]
digits[0]
0
digits[-1]
9
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[1:8:2] # start:end:step
[1, 3, 5, 7]
digits[0:10:1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[0:10:2]
[0, 2, 4, 6, 8]
digits[1:10:2]
[1, 3, 5, 7, 9]
digits[1::2] # anything that you don't give it will take default
[1, 3, 5, 7, 9]
digits[1::] # end is taken as end of list by default and step is taken as one by default
[1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[2:5] # this means step in not given
[2, 3, 4]
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[2:] # it means drop first two item
[2, 3, 4, 5, 6, 7, 8, 9]
digits[3:] # drop first 3 items
[3, 4, 5, 6, 7, 8, 9]
digits[:3] # take first three items
[0, 1, 2]
digits[1:5] ## start at location 1 and end before location 5
[1, 2, 3, 4]
digits[1]
1
digits[5]
5
digits[::1] # complete list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[:] # complete list ..copy
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = digits # this is pointing to same memeory as digits
z = digits[:] # this is new copy
digits[::-1] # what will this give? this will give a revrsed list!
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
d
{1: 'one', 2: 'two', 3: 'three'}
text = "this works with any collection"
text[3:10]
's works'
text[::-1]
'noitcelloc yna htiw skrow siht'
x
range(0, 10)
#range(start, end , step) start numbers at start with steps as step and end before end
list(range(2, 20, 3))
[2, 5, 8, 11, 14, 17]
list(range(1, 6))
[1, 2, 3, 4, 5]
x[::-1]
range(9, -1, -1)
list(range(9, -1, -1))# not reco
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
list = [2, 3, 5, 6]
list(range(10))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [124], in <cell line: 1>() ----> 1 list(range(10)) TypeError: 'list' object is not callable
list
[2, 3, 5, 6]
del list
list
list
list(range(5))
[0, 1, 2, 3, 4]
Important Note You should not use variable names which are same as built in function names, and also keywords!
range(1,5) # you have no way to skip end in range function
range(1, 5)
range(,,5) # this is not allowed
Input In [131] range(,,5) # this is not allowed ^ SyntaxError: invalid syntax
del = 45
Input In [133] del = 45 ^ SyntaxError: invalid syntax
def say_hello(name): # : is required only at end of defination line
print("Hello", name) # print can take any number of arguments
# every line should be indented by constant spaces.. we take 4
The moment above code is executed, a variable with name of function is created ..and it points to function
say_hello # it is just referring to function like a variable
<function __main__.say_hello(name)>
say_hello("vikrant")
Hello vikrant
def say_hello(name) # : at end of this line is necessary
print("Hello",name)
Input In [142] def say_hello(name) ^ SyntaxError: expected ':'
def square(x):
return x*x
square
<function __main__.square(x)>
square(5)
25
a = 56
square(a)
3136
def square(b):
return b*b
def square(num):
return num*num # this has return statement!
square(56) # behaves like black box! you give input and it will give back output
3136
sqr23 = square(23) # with a function that returns I can assign the results to a variable
print("hello")
hello
x = print("hello") # it did not return "hello" ..it printed ...
hello
print(x) # nothing!
None
s = sum([1, 2, 3, 4, 5]) # there is no print here...
print(s)
15
s # this is interpreter response .... it is not print .. it is additional functionality of live interepreter for debugging purpose
# what you see here lloks like print ... but is just a side effect
15
print(s) # we tell explicitly that... print this value to standard output!
15
say_hello("vikrant") # this is printing! but not returning
Hello vikrant
greeting = say_hello("vikrant")
Hello vikrant
print(greeting)
None
def say_hello(name):
print("Hello", name)
def square(b):
print(b*b)
square(25)
625
sqr25 = square(25)
625
print(sqr25)
None
def print_square(b):
print(b*b)
print_square(56)
3136
def square(b):
return b*b
sqr23 = square(23)
square(23) # this is not print ... but interpreter is showing me the returned result for debugging purpose
529
Some common mistakes by new comers in python
def add(2,3): # incorrect!! ..
return 2+3
def concat("a","b"): # literal not allowed as parameter
return "a"+"b"
Input In [186] def add(2,3): # incorrect!! .. ^ SyntaxError: invalid syntax
def add(x, y): # numeric computation
return x+y
def concat(a, b):# text data
return a + b
add(2, 3)
5
a
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [192], in <cell line: 1>() ----> 1 a NameError: name 'a' is not defined
b
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [193], in <cell line: 1>() ----> 1 b NameError: name 'b' is not defined
concat(a,b) # incorrect ..beacause a and b are not predefined!
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [194], in <cell line: 1>() ----> 1 concat(a,b) NameError: name 'a' is not defined
concat("sds", "sds")
'sdssds'
str1 = "hello"
str2 = "world"
concat(str1, str2)
'helloworld'
def sumofsquares(a, b, c):
sqra = a*a
sqrb = b*b
sqrc = c*c
# this empty space is immateraial as long as indentation in lines below is preserved
return sqra + sqrb + sqrc
sumofsquares(2, 3, 4)
29
def sumofsquares_(a, b, c):
sqra = a*a
sqrb = b*b
sqrc = c*c
return sqra + sqrb + sqrc # this line is not part of function...because the indentation has come back to zero!
Input In [200] return sqra + sqrb + sqrc # this line is not part of function...because the indentation has come back to zero! ^ SyntaxError: 'return' outside function