Module I - Day 3

Python Made Easy: Science and Math Edition

Sep-Dec 2025 batch, Vikrant Patil

Date: 04 Oct 2025

Click here for All Notes

© Vikrant Patil

Problem 1.2

Total amount after compound interest is calculated using formula P (1 + r/n) \(^n\)\(^t\) . In this formula, P is the principal amount, r is the rate of interest (as fraction of 100) per annum, n denotes the number of times in a year the interest gets compounded, and t denotes the number of years. Use python to Total amount after compoundin interest for principle amount of 26780, rate of interest 7%, interest is compounded 4 quarterly, and amount is invested for 5 years.

Problem 1.3

Have a look at following python statements.

x = 10
y = x
x = x + 10

What will be value of y after this?

Problem 1.4

What will be value of x after executing all statements?

x = 10
y = x
y = 25
P = 26780
r = 0.07
n = 4
t = 5
P * ( 1 + r/n)**(n*t) # any execution of cell in jupyter will show result of last line 
37887.76008234032
amount = P * ( 1 + r/n)**(n*t) # this is storing
amount
37887.76008234032
amount - P # interest amount
11107.760082340319
x = 10 # this is storing the value
print(amount)
37887.76008234032
2 * 3
5 * 10
20 -5
2 ** 100 # this will show only result of last line
1267650600228229401496703205376
x
10
x = 10
P = 26780
r = 0.07
n = 4
t = 5
amount = P * ( 1 + r/n)**(n*t) # any execution of cell in jupyter will show result of last line, this will not show anything because
                               # the last line is just a variable assignment

Collections - methods

nums = [1, 2, 3, 4, 5, 6]
empty = []
empty.append(0) # this will append 0 to empty list
empty
[0]
empty.append(10)
empty
[0, 10]
empty.append(-1)
empty
[0, 10, -1]
x
10
empty.count(0)
1
empty.remove(-1)
empty
[0, 10]
ones = [1, 1, 1, 1, 1, 1, 1]
ones.remove(1) # it will remove first occurence of the 1
ones
[1, 1, 1, 1, 1, 1]

immutable objects

objects that can not be modified once created

text = "this is some sentence"
text.capitalize() # this will not change the actual data , but it will return new data as per method functioning
'This is some sentence'
text.upper()
'THIS IS SOME SENTENCE'
text
'this is some sentence'
upper_text = text.upper()
text
'this is some sentence'
upper_text
'THIS IS SOME SENTENCE'
nums
[1, 2, 3, 4, 5, 6]
nums[0] 
1
nums[3]
4
nums[0] = 5 # this will modify actual nums at location 0
nums
[5, 2, 3, 4, 5, 6]
text[0] = "T"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[47], line 1
----> 1 text[0] = "T"

TypeError: 'str' object does not support item assignment
nums.append(-1)
nums
[5, 2, 3, 4, 5, 6, -1]
nums. append(3) 
nums
[5, 2, 3, 4, 5, 6, -1, 3]

methods from text

sentence = "this has some words but in single sentence"
sentence.split() # this will split the text on the basis of spaces into small texts and make a list of those small texts
['this', 'has', 'some', 'words', 'but', 'in', 'single', 'sentence']
textdata = "one,two,three,four,five,six"
textdata.split(",") # split with ","
['one', 'two', 'three', 'four', 'five', 'six']
textdata1 = "one,two,,three,four,five,six"
textdata1.split(",")
['one', 'two', '', 'three', 'four', 'five', 'six']
hyphentext = "text-with-hyphens-in-it"
hyphentext.split("-")
['text', 'with', 'hyphens', 'in', 'it']
"this$something$wierd$but$it$works".split("$")
['this', 'something', 'wierd', 'but', 'it', 'works']
words = "this$something$wierd$but$it$works".split("$")
words
['this', 'something', 'wierd', 'but', 'it', 'works']
multiline = """first line
second line
another line
yet another
and this
is last one"""
multiline
'first line\nsecond line\nanother line\nyet another\nand this \nis last one'
multiline.split("\n")
['first line',
 'second line',
 'another line',
 'yet another',
 'and this ',
 'is last one']
lines = multiline.split("\n")
type(lines) # tell me the type of object
list
lines[0] # 0th line
'first line'
lines[-1] # last line
'is last one'
print(multiline)
first line
second line
another line
yet another
and this 
is last one
words
['this', 'something', 'wierd', 'but', 'it', 'works']
" ".join(words)
'this something wierd but it works'
",".join(words)
'this,something,wierd,but,it,works'
"-".join(words)
'this-something-wierd-but-it-works'
lines
['first line',
 'second line',
 'another line',
 'yet another',
 'and this ',
 'is last one']
multi = "\n".join(lines)
print(multi)
first line
second line
another line
yet another
and this 
is last one
" ".join(["one", "two", "three", "four"])
'one two three four'
sperator = ","
words = ["one", "two", "three", "four", "five"]
finaltext = sperator.join(words)
print(finaltext)
one,two,three,four,five
finaltext
'one,two,three,four,five'
print(finaltext)
one,two,three,four,five
multiline
'first line\nsecond line\nanother line\nyet another\nand this \nis last one'
print(multiline)
first line
second line
another line
yet another
and this 
is last one
"" # this is also text, but it empty
''
"".join(["one", "two", "three", "four"])
'onetwothreefour'
words
['one', 'two', 'three', 'four', 'five']
"?".join(words)
'one?two?three?four?five'
" and ".join(words) # necessarily you have to give a list/iterable
'one and two and three and four and five'
"statement that will be joined to another and this is next statement".split(" and ")
['statement that will be joined to another', 'this is next statement']

quick rundown of usefull methods

text.upper()
'THIS IS SOME SENTENCE'
text.lower()
'this is some sentence'
text.title()
'This Is Some Sentence'
text.isalnum() 
False
"ertehjh34".isalnum()
True
text.startswith("hello")
False
text.startswith("this")
True
text.startswith("t")
True
text.endswith("e")
True
text.count("e")
4
text.count("is")
2
text.center(50)
'              this is some sentence               '
text.rjust(50)
'                             this is some sentence'
text.ljust(50)
'this is some sentence                             '
help(text.find)
Help on built-in function find:

find(...) method of builtins.str instance
    S.find(sub[, start[, end]]) -> int
    
    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Return -1 on failure.
text
'this is some sentence'
text.find("is")
2
nums
[5, 2, 3, 4, 5, 6, -1, 3]
nums.append(-1)
nums
[5, 2, 3, 4, 5, 6, -1, 3, -1]
nums.remove(5) # remove first occerence of 5
nums
[2, 3, 4, 5, 6, -1, 3, -1]
nums.reverse()
nums
[-1, 3, -1, 6, 4, 3, 2]
nums.pop() # opposite of append. this will remove last item and return it
2
nums
[-1, 3, -1, 6, 4, 3]
x = nums.append(2) # it has not returned anything but it has appended
print(x)
None
y = nums.pop() # will return and remove last item
y
2
nums
[-1, 3, -1, 6, 4, 3]
nums.pop()
3
nums.append(3)

functions

just recollect the problem

Problem 1.1

In mathematical notation, we can write an equation like v = u + at where v is final velocity of object and u is initial velocity, a is acceleration. (usually it is understood that we are multiplying a and t when a and t are written side by side. But that does not work in Python. You need to put * operator between a and t to make python understand that it need to multiply them! Make use of python to find out velocity of an object after 5 seconds, which is thrown straight up at velocity of 100m/s. Assume acceleration due to gravity is 9.8 m/s\(^2\)

3 * 3
9
def square(x):
    s = x * x
    return s
    
square # show me what is it
<function __main__.square(x)>
nums
[-1, 3, -1, 6, 4, 3]
square(5)
25
s
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[138], line 1
----> 1 s

NameError: name 's' is not defined
sq5 = square(5)
sq5
25
add
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[141], line 1
----> 1 add

NameError: name 'add' is not defined
def add(a, b):
    return a + b
add
<function __main__.add(a, b)>
add(3, 4) # while calling add I passed litteral values as arguments
7
x = 50
y = 30
add(x, y)
80
add(50)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[147], line 1
----> 1 add(50)

TypeError: add() missing 1 required positional argument: 'b'
add()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[148], line 1
----> 1 add()

TypeError: add() missing 2 required positional arguments: 'a' and 'b'
def hello():
    return "Hello"
hello()
'Hello'
def compute_velocity(initial_velocity, acceleration, time):
    v = initial_velocity + acceleration*time
    return v
compute_velocity(100, -9.8, 5)
51.0
def compute_velocity(initial_velocity, acceleration, time):
    """Compute velocity using newton's law of motion.
    computes final velocity given initial velocity , acceleration and time
    
    initial_velocity -> velocity in m/s
    acceleratin -> acceleration in m/s**2
    time -> time in seconds
    returns -> final velocity in m/s
    """
    v = initial_velocity + acceleration*time
    return v
help(compute_velocity)
Help on function compute_velocity in module __main__:

compute_velocity(initial_velocity, acceleration, time)
    Compute velocity using newton's law of motion.
    computes final velocity given initial velocity , acceleration and time
    
    initial_velocity -> velocity in m/s
    acceleratin -> acceleration in m/s**2
    time -> time in seconds
    returns -> final velocity in m/s

problem Write a function compound_interest which takes arguments P, r, n, t where P is principle amount, r is rate of interest in %, n is componding frequency, t is time in years. compound interest is calculated using formula P (1 + r/n)\(^n\)\(^t\) - P

How does markdown work?

# header
## header2
### header

**text** bold
`code` code
$latex$

header4

text code

home work

  • Write a function say_hello which will take one argument called name and print hello greeting with that name. for example look at following example of a sample function call

    [ ]: say_hello("vikrant")
    Hello Vikrant!
  • Here is a list of some built in functions , which means you don’t have to define them, they exist with python installation (we have seen few such functions e.g. print, type). Try them out by passing appropriate arguments as suggested.

    • len : finds length of a list/text like objects. len([1, 1, 2, 2, 3, 3]) will give 6 as result. len(“hello”) will give 5.
    • sum : finds sum of numbers from a list (if has all numbers in it). So you pass a desired list as argument to sum.e.g. sum([1,2,3]) will result into 6!
    • str : converts an integer or float to text format, so that every digit in the number becomes a text character
    • int : converts a text into a number for example if you want to make “1234” as 1234 use int("1234")
  • Now write a function mean which will finds out arithmatic mean of numbers given in a list. A function mean will have an argument numbers which is assumed to be a list. your function will compute mean of numbers from that list. here is one sample run of function.

    [ ]: mean([1,2,3,4,5])
    3.0
  • Write a function count_digits which will count how many digits are there in an integer. So this function will take an argument n which is assumed to be an integer. Your function will return number of digits it has. Here are few sample runs

    [ ]: count_digits(45)
    2
    [ ]: count_digits(10000)
    5