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

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

Functions¶

In [1]:
int("42")
Out[1]:
42
In [2]:
str(56565)
Out[2]:
'56565'
In [3]:
str([1,2,3,4])
Out[3]:
'[1, 2, 3, 4]'
In [4]:
[1,2,3,4]
Out[4]:
[1, 2, 3, 4]
In [5]:
float("23.5")
Out[5]:
23.5
In [6]:
float(42)
Out[6]:
42.0
In [49]:
x = 10
In [8]:
type(x) # this will tell what is type (data) of value stored at x
Out[8]:
int
In [9]:
type(42.3)
Out[9]:
float
In [10]:
type([1, 2, 3, 4])
Out[10]:
list
In [11]:
type((1,23,3))
Out[11]:
tuple
In [12]:
stock = {"name":"IBM", "open":123, "high":125, "low": 120}
In [13]:
type(stock)
Out[13]:
dict
In [14]:
type("text")
Out[14]:
str
In [15]:
list("text")
Out[15]:
['t', 'e', 'x', 't']
In [19]:
range(5) # numbers starting from 0 till less than 5
Out[19]:
range(0, 5)
In [20]:
range5 = range(5)
In [21]:
list(range5)
Out[21]:
[0, 1, 2, 3, 4]
In [23]:
max([23, 25, 50, 8734,1,2,3]) # finds out maximum number from given list
Out[23]:
8734
In [24]:
max((1, 2, 3, 45))
Out[24]:
45
In [25]:
d = {1:"one", 2:"two", 3:"three"}
In [26]:
d
Out[26]:
{1: 'one', 2: 'two', 3: 'three'}
In [28]:
max(d) # it will find maximum from only keys of a dictionary
Out[28]:
3
In [29]:
nums = [34, 67, -1, 45, 23, 2, 3]
In [30]:
min(nums)
Out[30]:
-1
In [32]:
sum(nums) # finds sum of all numbers form the list
Out[32]:
173
In [33]:
print("hello")
hello
In [34]:
print("Hello world!")
Hello world!
In [47]:
name = input("What is your name?")
In [37]:
print(name)
vikrant
In [38]:
sorted(nums) 
Out[38]:
[-1, 2, 3, 23, 34, 45, 67]
In [41]:
sorted_nums = sorted(nums) # here I am storing output returned by function sorted into a variable called sorted_nums
In [42]:
nums
Out[42]:
[34, 67, -1, 45, 23, 2, 3]
In [43]:
sorted_nums 
Out[43]:
[-1, 2, 3, 23, 34, 45, 67]
In [48]:
numsx = [1, 2, 3, 4]
In [46]:
sorted(numsx)
Out[46]:
[1, 2, 3, 4]

problems

  • Use python to find total income if the person has five income sources giving income of 123330, 250000, 45555, 232130, 11123
  • Find out how many digits are there in 2 raised to power 42
  • Using python find highest income from example above
  • Will this work? sum(["a","b","c","d"])
In [51]:
"a" + "b"
Out[51]:
'ab'
In [52]:
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.

In [54]:
strdigits = str(2**42)
In [55]:
strdigits
Out[55]:
'4398046511104'
In [56]:
len(strdigits)
Out[56]:
13
In [57]:
strdigits
Out[57]:
'4398046511104'
In [58]:
a = 2**42
In [59]:
incomes = [123330, 250000, 45555, 232130, 11123]
In [61]:
sum(incomes) # total income
Out[61]:
662138
In [62]:
max(incomes)
Out[62]:
250000
In [63]:
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'
In [64]:
[1, 2, 3, 4, 5]
Out[64]:
[1, 2, 3, 4, 5]
In [67]:
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]
In [66]:
"" + "a" + "b" + "c"
Out[66]:
'abc'

List Slicing (string/tuple)¶

In [72]:
digits = [0,1,2,3,4,5,6,7,8,9]
In [71]:
digits_ = list(range(10))
In [73]:
digits
Out[73]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [74]:
digits_
Out[74]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [75]:
x = range(10)
In [76]:
x
Out[76]:
range(0, 10)
In [77]:
x[0]
Out[77]:
0
In [79]:
list((1, 2, 3))
Out[79]:
[1, 2, 3]
In [80]:
digits[0]
Out[80]:
0
In [81]:
digits[-1]
Out[81]:
9
In [82]:
digits
Out[82]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [86]:
digits[1:8:2] # start:end:step
Out[86]:
[1, 3, 5, 7]
In [87]:
digits[0:10:1]
Out[87]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [88]:
digits[0:10:2]
Out[88]:
[0, 2, 4, 6, 8]
In [89]:
digits[1:10:2]
Out[89]:
[1, 3, 5, 7, 9]
In [91]:
digits[1::2] # anything that you don't give it will take default
Out[91]:
[1, 3, 5, 7, 9]
In [92]:
digits[1::] # end is taken as end of list by default and step is taken as one by default
Out[92]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [93]:
digits[2:5] # this means step in not given
Out[93]:
[2, 3, 4]
In [94]:
digits
Out[94]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [95]:
digits[2:] # it means drop first two item
Out[95]:
[2, 3, 4, 5, 6, 7, 8, 9]
In [96]:
digits[3:] # drop first 3 items
Out[96]:
[3, 4, 5, 6, 7, 8, 9]
In [97]:
digits[:3] # take first three items
Out[97]:
[0, 1, 2]
In [98]:
digits[1:5] ## start at location 1 and end before location 5
Out[98]:
[1, 2, 3, 4]
In [99]:
digits[1]
Out[99]:
1
In [100]:
digits[5]
Out[100]:
5
In [101]:
digits[::1] # complete list
Out[101]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [102]:
digits[:] # complete list ..copy
Out[102]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [105]:
y = digits # this is pointing to same memeory as digits
In [106]:
z = digits[:] # this is new copy
In [108]:
digits[::-1] # what will this give? this will give a revrsed list!
Out[108]:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In [110]:
d
Out[110]:
{1: 'one', 2: 'two', 3: 'three'}
In [111]:
text = "this works with any collection"
In [113]:
text[3:10]
Out[113]:
's works'
In [114]:
text[::-1]
Out[114]:
'noitcelloc yna htiw skrow siht'
In [115]:
x
Out[115]:
range(0, 10)
In [120]:
#range(start, end , step) start numbers at start with steps as step and end before end
In [118]:
list(range(2, 20, 3))
Out[118]:
[2, 5, 8, 11, 14, 17]
In [119]:
list(range(1, 6))
Out[119]:
[1, 2, 3, 4, 5]
In [121]:
x[::-1]
Out[121]:
range(9, -1, -1)
In [122]:
list(range(9, -1, -1))# not reco
Out[122]:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In [123]:
list = [2, 3, 5, 6]
In [124]:
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
In [125]:
list
Out[125]:
[2, 3, 5, 6]
In [126]:
del list
In [127]:
list
Out[127]:
list
In [128]:
list(range(5))
Out[128]:
[0, 1, 2, 3, 4]

Important Note You should not use variable names which are same as built in function names, and also keywords!

In [129]:
range(1,5) # you have no way to skip end in range function
Out[129]:
range(1, 5)
In [131]:
range(,,5) # this is not allowed 
  Input In [131]
    range(,,5) # this is not allowed
          ^
SyntaxError: invalid syntax
In [133]:
del = 45 
  Input In [133]
    del = 45
        ^
SyntaxError: invalid syntax
In [ ]:
 

Custom Functions¶

In [144]:
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

In [137]:
say_hello # it is just referring to function like a variable
Out[137]:
<function __main__.say_hello(name)>
In [139]:
say_hello("vikrant")
Hello vikrant
In [142]:
def say_hello(name)   # : at end of this line is necessary
    print("Hello",name) 
  Input In [142]
    def say_hello(name)
                       ^
SyntaxError: expected ':'
In [145]:
def square(x):
    return x*x
In [146]:
square
Out[146]:
<function __main__.square(x)>
In [147]:
square(5)
Out[147]:
25
In [148]:
a = 56
In [149]:
square(a)
Out[149]:
3136
In [150]:
def square(b):
    return b*b
In [153]:
def square(num):
    return num*num # this has return statement!
In [152]:
square(56) # behaves like black box! you give input and it will give back output
Out[152]:
3136
In [155]:
sqr23 = square(23) # with a function that returns I can assign the results to a variable
In [156]:
print("hello")
hello
In [157]:
x = print("hello") # it did not return "hello" ..it printed ... 
hello
In [159]:
print(x) # nothing!
None
In [161]:
s = sum([1, 2, 3, 4, 5]) # there is no print here...
In [162]:
print(s)
15
In [166]:
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
Out[166]:
15
In [167]:
print(s)  # we tell explicitly that... print this value to standard output!
15
In [170]:
say_hello("vikrant") # this is printing! but not returning
Hello vikrant
In [171]:
greeting = say_hello("vikrant")
Hello vikrant
In [172]:
print(greeting)
None
In [173]:
def say_hello(name): 
    print("Hello", name)
In [174]:
def square(b):
    print(b*b)
In [175]:
square(25)
625
In [176]:
sqr25  = square(25)
625
In [177]:
print(sqr25)
None
In [178]:
def print_square(b):
    print(b*b)
In [179]:
print_square(56)
3136
In [180]:
def square(b):
    return b*b
In [181]:
sqr23 = square(23)
In [183]:
square(23) # this is not print ... but interpreter is showing me the returned result for debugging purpose
Out[183]:
529

Calling Functions Vs Functions¶

Some common mistakes by new comers in python

  1. You can not use literals as arguments
In [186]:
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
In [189]:
def add(x, y): # numeric computation
    return x+y

def concat(a, b):# text data
    return a + b
In [190]:
add(2, 3)
Out[190]:
5
In [192]:
a
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [192], in <cell line: 1>()
----> 1 a

NameError: name 'a' is not defined
In [193]:
b
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [193], in <cell line: 1>()
----> 1 b

NameError: name 'b' is not defined
In [194]:
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
In [195]:
concat("sds", "sds")
Out[195]:
'sdssds'
In [196]:
str1 = "hello"
str2 = "world"
concat(str1, str2)
Out[196]:
'helloworld'
In [199]:
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
In [198]:
sumofsquares(2, 3, 4)
Out[198]:
29
In [200]:
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
In [ ]: