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-day3
notebook names are case sensitive. Make sure you give correct name
© Pipal Academy LLP
def simple_interest(principal, time, rate):# if name does not match with the problem defination then tests will fail
return principal*time*rate
Common systax rules for python
def mean(a, b, c, d, e):
return (a+b+c+d+e)/5 # this computes mean of only five numbers
def mean(numbers):
return sum(numbers)/len(numbers)
mean([1, 2, 3, 4, 2, 3, 1, 3, 5, 6])
3.0
nums = list(range(100))
mean(nums)
49.5
input("Enter comma seperated numbers")
'1, 2, 3, 4'
X= 5
x # small case
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[12], line 1 ----> 1 x # small case NameError: name 'x' is not defined
x = 5
x
5
sentence = "There are some unwise words to experiment"
type(sentence) # str-> string->text data
str
sentence.startswith("T")
True
sentence.startswith("THERE") # case sensitive
False
sentence.startswith("There")
True
sentence.endswith("experiment")
True
X = "X"
X.isupper()
True
sentence.isupper()
False
sentence.islower()
False
sentence.isalnum()
False
sentence
'There are some unwise words to experiment'
"there".isalnum()
True
"ther2323".isalnum()
True
"2323".isalpha()
False
int("one")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[31], line 1 ----> 1 int("one") ValueError: invalid literal for int() with base 10: 'one'
"one".isnumeric()
False
"232323".isnumeric()
True
Methods to modify data. modified but not original data, new data is created and that is returned
sentence
'There are some unwise words to experiment'
sentence.upper()
'THERE ARE SOME UNWISE WORDS TO EXPERIMENT'
"vikrant".capitalize()
'Vikrant'
sentence
'There are some unwise words to experiment'
sentence.upper()
'THERE ARE SOME UNWISE WORDS TO EXPERIMENT'
sentence
'There are some unwise words to experiment'
sentence.ljust(50)
'There are some unwise words to experiment '
sentence.rjust(80)
' There are some unwise words to experiment'
sentence.center(100) # additional input and worked on internal data
' There are some unwise words to experiment '
sentence
'There are some unwise words to experiment'
sentence.rjust(10) # 10 is less than length of the data stored inside
'There are some unwise words to experiment'
len(sentence)
41
sentence.rjust(45)
' There are some unwise words to experiment'
centered_sentence = sentence.center(100)
len(centered_sentence)
100
centered_sentence
' There are some unwise words to experiment '
mehods used widely during text processing
sentence[:3].upper()
'THE'
sentence
'There are some unwise words to experiment'
sentence.split() # this will seperate words by taking whitespace as word boundary
['There', 'are', 'some', 'unwise', 'words', 'to', 'experiment']
words = sentence.split()
words
['There', 'are', 'some', 'unwise', 'words', 'to', 'experiment']
words[0].upper()
'THERE'
def upper_first_three_words(sentence):
words = sentence.split()
return words[0].upper(), words[1].upper(), words[2].upper()
upper_first_three_words(sentence)
('THERE', 'ARE', 'SOME')
upper_first_three_words("THis is another statement")
('THIS', 'IS', 'ANOTHER')
"THIS IS ANOTHER"
'THIS IS ANOTHER'
three_words = upper_first_three_words(sentence)
three_words
('THERE', 'ARE', 'SOME')
" ".join(three_words) # given words I can stitch them together to form a sentence
'THERE ARE SOME'
",".join(three_words)
'THERE,ARE,SOME'
"\\".join(three_words)
'THERE\\ARE\\SOME'
"\\".join(["c:", "program files", "python"])
'c:\\program files\\python'
"".join(three_words)
'THEREARESOME'
words_input = input("Eneter words seperated by comma")
words_input
'one,two,three,four'
words_input.split(",") # you can also specify word boundary for spliting
['one', 'two', 'three', 'four']
" this has trailing whitespace at start".strip()
'this has trailing whitespace at start'
" this has trailing whitespace at start".split() # split can take one more continious white spaces as word boundary
['this', 'has', 'trailing', 'whitespace', 'at', 'start']
" dsfghds,sdsd,dsad, sadsa, sdsad ".strip()
'dsfghds,sdsd,dsad, sadsa, sdsad'
" dsfghds,sdsd,dsad, sadsa, sdsad ".lstrip()
'dsfghds,sdsd,dsad, sadsa, sdsad '
" dsfghds,sdsd,dsad, sadsa, sdsad ".rstrip()
' dsfghds,sdsd,dsad, sadsa, sdsad'
"This is a statement".replace("This", "That").split()
['That', 'is', 'a', 'statement']
"This is a statement".replace("This", "That").replace("is", "was")
'That was a statement'
int("-12121".replace("-", "")) # just another simple way to make absolute!
12121
problems
username is as per rules?>>> sentence = "Yet-another-sentence-with-nothing-in-it"
>>> path = "/home/vikrant/trainig/day1.html"
filename = "hello.xlsx"def check_unsername(username):
return username.isalnum()
def filename_unix(path):
tokens = path.split("/")
return tokens[-1]
def extension(filename):
tokens = filename.split(".")
return "." + tokens[-1]
extension("hello.py")
'.py'
filename_unix("/home/vikrant/day.html")
'day.html'
evens = range(2, 20, 2)
list(evens)
[2, 4, 6, 8, 10, 12, 14, 16, 18]
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nums = list(range(5))
nums
[0, 1, 2, 3, 4]
nums.index(1)
1
odds = list(range(1, 20, 2))
odds
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
odds.index(5) # returns index of 5
2
odds[2]
5
odds.index(2)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[117], line 1 ----> 1 odds.index(2) ValueError: 2 is not in list
odds.count(2)
0
odds.count(5) # returns count
1
ones = [1, 1, 1, 1, 1, 1]
ones.count(1)
6
nums = [1, 1, 1, 1, 2, 2, 2, 3, 3]
nums.count(1)
4
nums.count(3)
2
empty = []
empty.append(1) # does not return anything , but appends given item at end
empty
[1]
empty.append(3)
empty
[1, 3]
empty.insert(0, 23) # does not return anything, but inserts item at given location
empty
[23, 1, 3]
empty.remove(23) # does not return anything, but removes first occurence of given item
empty
[1, 3]
nums
[1, 1, 1, 1, 2, 2, 2, 3, 3]
nums.remove(1)
nums
[1, 1, 1, 2, 2, 2, 3, 3]
nums.clear() # remove everything
nums
[]
odds
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
sorted(odds)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
nums = [4, 2, 2, 6, 3, 9, 3, 8, 4, 2]
nums
[4, 2, 2, 6, 3, 9, 3, 8, 4, 2]
sorted(nums) # it will return new sorted list. original list remains as it is
[2, 2, 2, 3, 3, 4, 4, 6, 8, 9]
nums
[4, 2, 2, 6, 3, 9, 3, 8, 4, 2]
nums.sort() # this does not return anything. this will sort in place. original data order is lost
nums
[2, 2, 2, 3, 3, 4, 4, 6, 8, 9]
nums.reverse() # does not return anything. but reverses the list in place
nums
[9, 8, 6, 4, 4, 3, 3, 2, 2, 2]
nums.sort()
nums
[2, 2, 2, 3, 3, 4, 4, 6, 8, 9]
nums.sort(reverse=True) # descending order, in place
nums
[9, 8, 6, 4, 4, 3, 3, 2, 2, 2]
sorted(nums, reverse=True)
[9, 8, 6, 4, 4, 3, 3, 2, 2, 2]
s = "hello"
s.upper()
'HELLO'
s
'hello'
s_upper = s.upper()
s_upper
'HELLO'
s
'hello'
quiz
What will be output
x = 10
def foo():
x = 20
foo()
print(x)
output is 10
What happens if x is neither defined nor passed as an argument
x = 10
def foo():
print(x)
foo()
x = 10
def foo():
print(x) # because is not available local namespace, it will take it from global
foo()
10
x = 10
def foo():
x = x + 1
foo()
print(x)
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) Cell In[164], line 6 3 def foo(): 4 x = x + 1 ----> 6 foo() 7 print(x) Cell In[164], line 4, in foo() 3 def foo(): ----> 4 x = x + 1 UnboundLocalError: local variable 'x' referenced before assignment
x = 10
def foo():
x = 20 # local
x = x + 1
foo()
print(x)
10
x = [1, 1, 1]
def appendzero(y):
y.append(0) # a method which changes in place is called..so original will get modified
appendzero(x)
print(x)
[1, 1, 1, 0]
x = [1, 1, 1]
def appendzero(y):
y = y + [0] # this is not append. also defining a local variable
appendzero(x)
print(x)
[1, 1, 1]
underlying execution mechanism for this is
y = y + [0]
y = x + [0]
l = [1, 1, 1]
l + [0]
[1, 1, 1, 0]
l
[1, 1, 1]
x = [1, 1, 1]
def appendzero(y):
return y.append(0) # a method which changes in place is called..so original will get modified
appendzero(x)
print(x)
[1, 1, 1, 0]
x = [1, 1, 1]
def appendzero(y):
return y.append(0) # a method which changes in place is called..so original will get modified
print(appendzero(x))
print(x)
None [1, 1, 1, 0]
nums
[9, 8, 6, 4, 4, 3, 3, 2, 2, 2]
print(nums.index(8)) # this returns
1
print(nums.append(-1)) # this does not return
None
nums
[9, 8, 6, 4, 4, 3, 3, 2, 2, 2, -1]