Sep 13-17, 2021 Vikrant Patil
These notes are available online at https://notes.pipal.in/2021/arcesium_finop_batch1/module1-day2.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from https://lab.pipal.in for this training.
login to hub and create a notebook with name module1-day2
built in functions
name = "rupali"
len(name)
6
numbers = [1, 2, 3, 4, 5, 6]
len(numbers) # with tab you can auto-complete the variable name, function names or build ins
6
len(numbers)
6
point = (0, 0, 0)
len(point)
3
stock = {"name":"IBM", "open":124, "close":125}
len(stock)
3
type conversion
p = input("Enter value of principle amount:")
p
'20000'
p + 200
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-13-3698dda22622> in <module> ----> 1 p + 200 TypeError: can only concatenate str (not "int") to str
len(p)
5
p**2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-15-ab546129d7fc> in <module> ----> 1 p**2 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
int(p)
20000
p
'20000'
principle = int(p)
principle + 200
20200
principle**0.05
1.6407843301899723
float("20.5")
20.5
float(5)
5.0
str(principle)
'20000'
list("hello")
['h', 'e', 'l', 'l', 'o']
some_numbers = input("Enter list of numbers:")
some_numbers
'[1, 2, 3, 4, 5]'
some_numbers[0]
'['
some_numbers[-1]
']'
list(some_numbers)
['[', '1', ',', ' ', '2', ',', ' ', '3', ',', ' ', '4', ',', ' ', '5', ']']
stock
{'name': 'IBM', 'open': 124, 'close': 125}
list(stock) # works only on collections , where it creates a list from items in given
['name', 'open', 'close']
list(point)
[0, 0, 0]
point
(0, 0, 0)
max([42, 45, 343, 67, 34])
343
min(numbers)
1
sum(numbers)
21
sum(["a","b","c"])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-37-839ac8434816> in <module> ----> 1 sum(["a","b","c"]) TypeError: unsupported operand type(s) for +: 'int' and 'str'
"a" + "b"
'ab'
problem
incomes = [32344, 435454, 5454, 43545]
total_income = sum(incomes)
print(total_income)
516797
max_income = max(incomes)
x = 10
y = x
x = 30
max = max(incomes) # by mistake overwrote python function! variable name should be from built in functions
max
435454
max(incomes)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-45-9070a17d57e1> in <module> ----> 1 max(incomes) TypeError: 'int' object is not callable
del max
max_ = max(incomes)
max_
435454
max_income = max(incomes)
2**100
1267650600228229401496703205376
power_2_100 = 2**100
print(power_2_100)
1267650600228229401496703205376
len(power_2_100)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-56-c26ee8a0acd7> in <module> ----> 1 len(power_2_100) TypeError: object of type 'int' has no len()
len(str(power_2_100)) # nested calling!
31
digits_text = str(power_2_100)
len(digits_text)
31
2^100 # this is bitwise operator
102
max(3, 5, 6)
6
sum(2, 3, 4)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-62-fc2d57a9194a> in <module> ----> 1 sum(2, 3, 4) TypeError: sum() takes at most 2 arguments (3 given)
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.
help(max)
Help on built-in function max in module builtins:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
p = {1, 2, 2, 3} # this set, it makes a unique collection
p
{1, 2, 3}
len(p)
3
incomes = {"shares": 50000, "salary":300000, "rent":100000}
max(incomes) # this will order the keys in dictionary order and give me last item
'shares'
len(incomes)
3
list(incomes)
['shares', 'salary', 'rent']
max(["one", "two", "three"])
'two'
[3, 878, 2, 1, 7]-> [1, 2, 3, 7, 878]
["a", "ab", "abc", "b", "boo", "bool"] # dictionary order
max(["one", "two", "three"], key=len)
'three'
numbers
[1, 2, 3, 4, 5, 6]
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[2:8:2] # give me items from position 2 till 8 (exludes this) , every second item
[2, 4, 6]
list[start:end:step]
digits[2:8] # all items from index 2 till index 8 (exluding)
[2, 3, 4, 5, 6, 7]
digits[:5] # take first five items
[0, 1, 2, 3, 4]
digits[5:] # drop first five items
[5, 6, 7, 8, 9]
digits[:] # take all..copy
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[::-1] # reverse order
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
len(numbers) # function
6
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
len(digits)
10
sorted(digits)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sorted(digits, reverse=True)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[0] = -1
digits
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sentence = "These are some wise words"
sentence.startswith("This")
False
sentence.startswith("These")
True
sentence.endswith("false")
False
sentence.endswith("words") # argument to this method is litteral string
True
sentence = input("enter your statement")
word = input("enter the word whose existence you want check at end of statement")
print(sentence.endswith(word))
True
2 + 3
5+6
2**15
4*5
20
print(2 + 3)
print(5+6)
print(2**15)
print(4*5)
5 11 32768 20
sentence.isupper()
False
"HELLO".isupper()
True
sentence.istitle()
False
sentence.isalnum()
False
sentence.isprintable()
True
"\n".isprintable()
False
sentence.upper()
'HELLO THIS IS THIRD TIME!'
sentence
'hello this is third time!'
sentence.upper()
'HELLO THIS IS THIRD TIME!'
sentence.capitalize()
'Hello this is third time!'
sentence.title()
'Hello This Is Third Time!'
sentence.center(50)
' hello this is third time! '
sentence.rjust(50)
' hello this is third time!'
sentence.ljust(50)
'hello this is third time! '
sentence.replace("third", "quite a many")
'hello this is quite a many time!'
print(sentence.replace("third", "quite a many")) # this will print only items from the string..
hello this is quite a many time!
"hello 234".isalnum()
False
"khfkjdshf".isalnum()
True
"dfhkjdshfkjs123".isalnum()
True
"fdshf 76786dsad".isalnum() # this text does have at least one char which is neither alphabet not a numeric digit
False
" ".isspace()
True
" \n \t".isspace()
True
" " in sentence # this checkes whether " " is there in sentence
True
sentence
'hello this is third time!'
sentence.split() # split the sentence using whitespace (space, \t \n)
['hello', 'this', 'is', 'third', 'time!']
path = "c:\\Program Files\\Python"
path.split("\\")
['c:', 'Program Files', 'Python']
path = r"c:\Program Files\Python" # raw string
sep = "\\"
path.split(sep)
['c:', 'Program Files', 'Python']
"commas,are,there,between,these,words".split(",")
['commas', 'are', 'there', 'between', 'these', 'words']
words = ['commas', 'are', 'there', 'between', 'these', 'words']
words
['commas', 'are', 'there', 'between', 'these', 'words']
",".join(words)
'commas,are,there,between,these,words'
" ".join(words)
'commas are there between these words'
folders = ["/","home","vikrant","training","arcesium"]
"/".join(folders)
'//home/vikrant/training/arcesium'
problem
filename="hello.xlsx"
filename.split(".")
['hello', 'xlsx']
filename.split(".")[-1]
'xlsx'
numbers
[1, 2, 3, 4, 5, 6]
numbers.index(3) # what is the location of this value , 3
2
[1, 2, 3, 4, 5, 6]
0 1 2 3 4 5 <---- index
['a', 'b', 'c', 'd']
0 1 2 3
nums = [1, 2, 4, 2, 3, 4] # what is the item comes many times in the list?
nums.index(2)
1
nums.count(2)
2
nums.count(3)
1
items = ['a', 'b', 'c', 'd']
items.index("a")
0
items.index("b")
1
empty = []
empty.append(1) ## adds item at last
empty.append(3)
empty
[1, 3]
empty.append(4)
empty # there will be two 4s because I executed above statement twice in my interpreter
[1, 3, 4, 4]
value = 24
loc = 2
empty.insert(loc, value) # note that assignment is not needed
empty
[1, 3, 24, 4, 4]
numbers = (1, 2, 3, 4)
list(numbers)
[1, 2, 3, 4]
numbers
(1, 2, 3, 4)
numbers = list(numbers)
numbers
[1, 2, 3, 4]
numbers.extend([2,3,4]) # again no assignment needed
numbers
[1, 2, 3, 4, 2, 3, 4]
items
['a', 'b', 'c', 'd']
items.remove('a') # no assignment required, list is modified directly
items
['b', 'c', 'd']
items = ['a','b','c','a']
items.remove('a') # will remov whatever occurs first
items = ['a','b','c','d']
items.pop()
print(items)
['a', 'b', 'c']
items = ['a','b','c','d']
last = items.pop()
print("items after pop" , items)
print("popped item", last)
items after pop ['a', 'b', 'c'] popped item d
item
['a', 'b', 'c']
items = ['a','b','c','d']
second = items.pop(2)
print("items after pop" , items)
print("popped item", second)
items after pop ['a', 'b', 'd'] popped item c
numbers
[1, 2, 3, 4, 2, 3, 4]
numbers.sort() #it sorts in place
numbers
[1, 2, 2, 3, 3, 4, 4]
numbers.reverse() # will reverse the lisy in place!
def square(x):
sqr = x*x
return sqr
square(34)
1156
def add(x, y):
return x+y
add(3, 4)
7
def sum_2(a, b):
return a+b
def sumofsquares(a, b):
return square(a) + square(b)
sumofsquares(3, 4)
25
v = 2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-207-047e21bebf5f> in <module> ----> 1 v.to_bytes() TypeError: to_bytes() missing required argument 'length' (pos 1)