Dec 07-11, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch3/module1-day3.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab.pipal.in for this training. Create a notebook with name module1-day3.ipynb for today's session
text = "some text with some contents"
type(text)
text.startswith("some")
text.endswith("s")
text.isupper()
text.isalnum()
text.istitle()
text.capitalize()
text.upper()
upper_text = text.upper()
print(upper_text)
text.lower()
text.title()
text.rjust(50) # right justify with width 50, pads space chars to left side so that text moves to right
text.ljust(50)
rtext = text.rjust(50)
len(rtext)
rtext[:10] # first 10 items from the string
space = ' ' # space char
print(rtext)
rtext
len(text)
rtext[:50-28] # only space chars
rtext[:50-28+1]
ltext = text.ljust(50)
ltext
text.center(50)
text.center(10)# if width is less than the len of str then nothing happens
text.split() # splits the string into words based on spaces
words = text.split()
" ".join(words)
"_".join(words)
folders = ["", "home", "vikrant" , "training","2020", "arcesium_finop_batch3"]
"/".join(folders)
Problems:
text = "Yet-another-sentence-with-nothing-in-it"
How can you transform it such that there will be space beween two words.
sep = "\\" escapce char
Thers is list of folders from C:
folders = ["C:", "Program Files", "python3.9"]
can you make a string for complete path to python installation folder? "c:\Program Files\python3.9"
path = "/home/vikrant/training/module-day1.ipynb"
can you find out only filename?
filename = "hello.xlsx"
solutions
text = "Yet-another-sentence-with-nothing-in-it"
text.split() # without any argument, then it splits on spaces
text.split("-") # if argument is given , then string is split into words based on arg
words = text.split("-")
words
" ".join(words)
space = " "
space
words
space.join(words)
space.join(["1", "2", "3", "4"])
type(words)
numbers = [1, 2, 3, 4, 5]
mixed = [{"x":1}, {1, 2, 3}, [1, 2, 3, 4], "hello"]
mixed
" ".join([1, 2, 3])
" ".join(["text", "string"]) # will work only on list of strings/text
" ".join(['1', '2', '3'])
['1,2,3'] # looks like a list, list of str, how many string?
" ".join(['1,2,3'])
"_".join(text) #
numbers = [1, 2, 3, 4]
text = "abrakadabra"
"_".join(("hey","whats","this","?"))
"_".join((1, 2, 34))
Observation:.join(arg), here arg can be any collection that contains strings or chars
c = "a"
"Hello this is another text".replace(" ",",")
"Hello this is another text".replace(" ","-")
text
text = "Yet-another-sentence-with-nothing-in-it"
text.replace("-"," ")
winsep = "\\" # this actually one char
print(winsep)
newline = "\n" # this single char
len(newline)
len(winsep)
"""
line1
line2
line3
"""
len(winsep)
folders = ["C:", "Program Files", "python3.9"]
winsep.join(folders) # what you see on next line is repr (representative)
print(winsep.join(folders))
multi = """a
b
c
d"""
multi # repr of multi
print(multi)
tabbed = "one\ttwo\tthree\tend"
tabbed # repr
print(tabbed)
path = "/home/vikrant/training/module-day1.ipynb"
tokens = path.split("/")
tokens
tokens[len(tokens)-1]
tokens[-1] # last
filename = "hello.xlsx"
pre, ext = filename.split(".")
print(pre, ext)
download = "xyz.tar.gz"
pre, post = download.split(".") # right hand side has resulted in three items, while we are trying to store it in two!
x, y = 1, 2
1, 2
x, y = (1, 2)
print(x, y)
a, b = [1, 2]
print(a, b)
a, b = [1, 2, 3]
tokens = download.split(".")
tokens
pre, ext = tokens
download.split(".", maxsplit=1)
" hello this is a another text with some spaces at start"
text = " hello this is a another text with some spaces at start"
text.split()
multi
multi.split() # split, splits on eny white spaces... ' ', a tab, a new line
"heloo this has multi spaces".split()
header = " Name,age,email,address\n"
header.split(",")
columnnames = header.split(",")
columnnames[0]
columnnames[-1]
choped = header.strip() #remove trailing spcaes , only from start and end!
choped
choped.split(",")
header
choped
"address\n".split("\n")
header.strip()
header.split(",") #this returns a new list
header # original string is same, it has not changed
header.strip().split(",")
c = header.strip()
c
c.split(",")
header.strip().split()
header = " Name,age,email,address\n"
header.strip().split(",")[-1]
------>----->----------->----->----- mehtods will get executed in this order
`
header
header.split(",")
person = {"Name":'Alice',
"age":29,
"email":"alice@wonder.land",
"address":"Rabbit Hole"}
item_from_file = header.split(",")[0]
person[item_from_file]
item_from_file
person[' Name'] # because of additional space, we can not retrive data from dict
person['Name']
person['name']
person[item_from_file.strip()]
item_from_file
item_from_file.strip()
poem = """
this is frist line of poem
last line of poem
"""
print(poem)
print(poem)
poem
f = open("/home/vikrant/Downloads/JH How Children Learn.txt")
firstline = f.readline()
firstline
f.close()
itemname = input("Enter name of item that you want to read from dictionary:")
itemname
fullname = input("Enter your fullname (first name and second name)")
fullname = input("Enter your fullname (first name and second name)")
fullname.strip()
nums = [1, 2, 3,4, 5, -1]
nums.index(-1) # location of -1 in nums
nums.index(0) # ??
nums.index(5)
nums.count(-1) # how many times -1 comes?
nums.count(0)
nums = [1, 1, 2, 2, 3, 3, 5, 5, 5, 5, 5, 5, 5]
nums.count(5)
nums.count(1)
empty = []
empty
empty.append(1) # it adds 1 at end of empty
empty
empty.append(-1) # append adds at end
empty
text.replace(" ",",")
empty.append(0) # this method does append but does not return a value
empty # this has 2 zeros because we executed line number 218 twice
x = text.split()
x
y = empty.append(1) # this will not save anything in y
print(y)
y
empty
y = empty
y
y = [1, 2, 3]
y = empty.append(5)
y
empty.insert(0, 23)
empty
empty.insert(3, 22)
empty
empty.remove(23)
empty
empty.remove(1)
empty
empty.remove(1)
empty
empty.pop() # removes last item and returns it
empty
x = empty.pop()
x
empty
empty.pop(2)
empty
empty.clear() # delete every item
empty
nums
nums.reverse()
nums
def square(x):
s = x*x
return s
square
square(5)
def square(x)
s = x*x
return s
def square(x):
s = x*x
return s
def square(x):
s = x*x
return s
def square(x):
s = x*x
return s
def square(x):
s = x*x
return s
def square(x):
s = x*x
print(s)
# this function has no return statement
square(5)
sqr5 = square(5) # to get the result in variable , the function has to return
print(sqr5)
def square(x):
s = x*x
return s
print(square(5))
def twice(x):
print(2*x) # it is returning None!!
twice(5)
twice(10)
twice(twice(5)) # first inner fucntion call will be executed
def twice(x):
return 2*x
twice(twice(5))
twice(twice(twice(5)))
square(5)
def square(x):
return x*x
def double(x):
return 2*x
sq5 = square(5)
double(sq5)
double(square(5))
double(square(6))
def twice(x):
print(2*x) # this is not returning anything.. it will return None
2*None
twice(None)
s1 = twice(5)# None
twice(s1)
twice(twice(5))
def twice(x):
return 2*x
twice(twice(5))
Problems
>>> NAV(assets,liabilities,shares)
>>> numeric_value("(35.5)")
-35.5
>>> numeric_value("32.5")
32.5
compounded_total which takes P, n, r, and t as arguments of a function and returns total value after t years.x = 10
def foo():
x = x+1
foo()
print(x)