Module I - Day 4

Python Made Easy: Science and Math Edition

Sep-Dec 2025 batch, Vikrant Patil

Date: 11 Oct 2025

Click here for All Notes

© Vikrant Patil

functions

velocity
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 1
----> 1 velocity

NameError: name 'velocity' is not defined
def velocity(u, a, t): # after colon..next line is always indented by 4 spaces
    """computes final velocity using newton's laws of motion
    u = initial velocity m/s
    a = acceleration m/s^2
    t = time in seconds"""
    final_velocity = u + a*t
    return final_velocity

print("hello") # this print statement is not part of function    
hello
velocity
<function __main__.velocity(u, a, t)>
velocity(100, -9.8, 5) # called the function with literal values as the argument
51.0
init_vel = 100 
accleration = -9.8
time = 5
velocity(init_vel, accleration, time)
51.0
help(velocity)
Help on function velocity in module __main__:

velocity(u, a, t)
    computes final velocity using newton's laws of motion
    u = initial velocity m/s
    a = acceleration m/s^2
    t = time in seconds
def vel(u, a, t):
    """some docs string
    """
    v = u + a*t
    return v
vel(100, -9.8, 5)
-49.0

some facts about functions

  1. function is over when you return
  2. function is over when you come to to original indentation level
  3. if there no return statement and you come back to original indentation level…then the function is over but it return None!
def hello(name):
    print("hello", name)  # print is built in function
hello("vikrant")
hello vikrant
velocity(0, 9.8, 5) # this is not print
49.0
x = 10
x # this is repr of x 
10
print(x) # this is actually print to screen
10
v = velocity(0, 9.8, 5) # this will not show repr
x = 10
v = velocity(0, 9.8, 5) # this statement will store value returned by fucntion call in v
def twice(x):
    2*x
twice(5)
x2 = twice(40)
print(x2)
None
hello("vikrant")
hello vikrant
hello_v = hello("vikrant")
hello vikrant
print(hello_v)
None
"text".capitalize()
'Text'
def say_hello(name):
    print("Hello", name.capitlize(), "!")
say_hello("vikrant")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[36], line 1
----> 1 say_hello("vikrant")

Cell In[35], line 2, in say_hello(name)
      1 def say_hello(name):
----> 2     print("Hello", name.capitlize(), "!")

AttributeError: 'str' object has no attribute 'capitlize'
def say_hello(name):
    print("Hello", name.capitalize(), "!")
say_hello("vikrant")
Hello Vikrant !
print(1,3,3)
1 3 3
"test" + "hello"
'testhello'
def say_hello(name):
    print("Hello", name.capitalize() + "!")
say_hello("vikrant")
Hello Vikrant!
def say_hello(name):
    formated_name = name.capitalize() + "!"
    print("Hello", formated_name)
say_hello("vikrant")
Hello Vikrant!
def add(2, 3):  # this is not correct
    return 2 + 3 
  Cell In[45], line 1
    def add(2, 3):  # this is not correct
            ^
SyntaxError: invalid syntax
def add(x, y):
    return x + y
def cube("x"): # the argument here is not a name, it is literal text "x"
    return x**3
  Cell In[47], line 1
    def cube("x"): # the argument here is not a name, it is literal text "x"
             ^
SyntaxError: invalid syntax
def cube(x):
    return x**3
cube(5)
125
cube(5)
125
cube(x)
1000

some built ins

print(1, 2, "hello")
1 2 hello
print(x, "hello")
10 hello
print("hello")
hello
cube(5)
125
cube(5, 6)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[57], line 1
----> 1 cube(5, 6)

TypeError: cube() takes 1 positional argument but 2 were given
len("text")
4
len([1, 2, 3, 4, 5, 6])
6
point = (0, 0, 10) # instead of square bracket you give round bracket
point
(0, 0, 10)

tuple is exactly same as list execpt that it is immutable

len(point)
3

dictionary

student = {"name": "siddharth",
           "profession": "naturalist"}
student['name']
'siddharth'
student['profession']
'naturalist'
student_list = ["siddharth", "naturalist"]
student_list[0]
'siddharth'
student_list[1]
'naturalist'
student1 = {"name": "Pallavi",
            "profession": "chemist"}

student2 = {"name": "Anil", "profession":"Software"}
classroom = [student, student1, student2]
classroom
[{'name': 'siddharth', 'profession': 'naturalist'},
 {'name': 'Pallavi', 'profession': 'chemist'},
 {'name': 'Anil', 'profession': 'Software'}]
type(classroom)
list
  • use len and find out how many students are there in classroom?
  • how many items are there stored for each student? use len
birds = ["species1", # it is good idea to give items in list/dictcionary on separate lines some times
         "species2",
         "species3"]
birds
['species1', 'species2', 'species3']
student2 = {"name": "Anil", 
            "profession":"Software"}
stock = {"ticker": "INFY",
         "start": 2000.5, 
         "end" : 2005.6,
         "volume": 500}
stock['ticker']
'INFY'
stock['start']
2000.5
len(classroom) # length if list
3
len(student) # length of dictionary
2
len(stock) # lenght of dictionary
4
len("vikrant") # length of text/str
7
nums
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[85], line 1
----> 1 nums

NameError: name 'nums' is not defined
sum([1, 2, 3, 4, 5])
15
classroom[0]
{'name': 'siddharth', 'profession': 'naturalist'}
len(classroom[0])
2
another_class = [
    {'name': 'siddharth', 'profession': 'naturalist'},
    {'name': 'Pallavi', 'profession': 'chemist'},
    {'name': 'Anil', 'profession': 'Software'}]
another_class
[{'name': 'siddharth', 'profession': 'naturalist'},
 {'name': 'Pallavi', 'profession': 'chemist'},
 {'name': 'Anil', 'profession': 'Software'}]
another_class[1]
{'name': 'Pallavi', 'profession': 'chemist'}
another_class[1]['name']
'Pallavi'

problem - consider data from your field which you use everyday. Try to fit that data into some nice structure using combination of lists and dictionaries

45
45
x = 45
str(x) # this will make text
'45'
x
45
strx = str(x)
strx
'45'
len(45)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[101], line 1
----> 1 len(45)

TypeError: object of type 'int' has no len()
int("45") 
45
int("45    ")
45
int("4 5")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[104], line 1
----> 1 int("4 5")

ValueError: invalid literal for int() with base 10: '4 5'
int("45 years")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[105], line 1
----> 1 int("45 years")

ValueError: invalid literal for int() with base 10: '45 years'
"45 years".split(" ")
['45', 'years']
"45 years".split(" ")[0]
'45'
int("45 years".split(" ")[0])
45
d = "45 years".split(" ")[0]
int(d)
45

conditions

"text".startswith("t")
True
nums
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[112], line 1
----> 1 nums

NameError: name 'nums' is not defined
nums = [1, 2, 3, 4]
1 in nums
True
statement = "this is a statement for checking if"
"for" in statement
True
"hello" in statement
False
x
45
y = 50
x > y
False
x < y
True
x >= y
False
x <= y
True
x == y
False
nums
[1, 2, 3, 4]
nums == [1, 2, 3, 4]
True
another_nums = [1, 2, 3, 4]
nums == another_nums
True
nums.append(10)
nums
[1, 2, 3, 4, 10]
nums == another_nums
False
def say_hello(name, language):
    if language == "english":
        print("Hello", name)
    elif language == "marathi":
        print("नमस्कार" , name)
    else:
        print(language, "is not supported, try english or marathi!")
say_hello("vikrant", "marathi")
नमस्कार vikrant
say_hello("vikrant", "english")
Hello vikrant
say_hello("vikrant", "tamil")
tamil is not supported, try english or marathi!

problem

Write a function max2 to find maximum from given two numbers. Then make use max2 to find maximum from three numbers and write another function max3