Practice

Python Made Easy: Science and Math Edition

Sep-Dec 2025 batch, Vikrant Patil

Date: 13 Dec 2025

Click here for All Notes

© Vikrant Patil

Function

def cube(n):
    return n*n    
    
cube
<function __main__.cube(n)>
x = 10
def call_of_wild(species_name):
    if species_name == "dog":  # 
        return "bark"
    elif species_name == "cat":
        return "meow"
    elif species_name == "monkey":
        return "hoop"
    else:
        return "unkown species"
call_of_wild("dog")
'bark'
words = ["one", "two", "three", "four", "five"]
for w in words:
    print(w)
one
two
three
four
five
upper_words = []
for w in words:
    upper_words.append(w.upper())

upper_words
['ONE', 'TWO', 'THREE', 'FOUR', 'FIVE']

problem - Write a function convert_upper which will take list of words as argument and return list of upper case words.

def convert_upper(words_list):
    upper_words = []
    for w in words_list:
        upper_words.append(w.upper())
    
    return upper_words
lower_words = ["hello", "some", "words", "are", "here", "in", "lower"]
convert_upper(lower_words)
['HELLO', 'SOME', 'WORDS', 'ARE', 'HERE', 'IN', 'LOWER']
def square(x):
    return x*x # the work to be done is veru small ..x*x
def convert_upper(lower_words):
    upper_words = []
    for w in lower_words:
        upper_words.append(w.upper())
    return upper_words
text = "This is some text with some data in it!"
text.split() 
['This', 'is', 'some', 'text', 'with', 'some', 'data', 'in', 'it!']

Variable names

  • variable names can not have space, +, -, *, **, ^, % special chars
  • alphabets (caps/small) and numbers
  • it can not start with a number
  • it can have underscore _
hello
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[15], line 1
----> 1 hello

NameError: name 'hello' is not defined
[hello, these, are , some , words]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[16], line 1
----> 1 [hello, these, are , some , words]

NameError: name 'hello' is not defined
w1 = "one"
w2 = "two"
w3 = "three"
[w1, w2, w3]
['one', 'two', 'three']
numbers = [3, 4, 2, 5, 6, 12, 56]
def f(x):
    return x**2 + 2*x + 1
f(2)
9
f(3)
16
f(4)
25
def squares_list(numbers):
    sqrs = []
    for n in numbers:
        sqrs.append(n*n)
    return sqr
squares_list(numbers)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[25], line 1
----> 1 squares_list(numbers)

Cell In[24], line 5, in squares_list(numbers)
      3 for n in numbers:
      4     sqrs.append(n*n)
----> 5 return sqr

NameError: name 'sqr' is not defined
def squares_list(numbers):
    sqrs = []
    for n in numbers:
        sqrs.append(n*n)
    return sqrs
squares_list(numbers)
[9, 16, 4, 25, 36, 144, 3136]
f(5)
36
f(4, 5)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[29], line 1
----> 1 f(4, 5)

TypeError: f() takes 1 positional argument but 2 were given
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 cubes_list(1,2,4,6,8)

TypeError: cubes_list() takes 1 positional argument but 5 were given
squares_list(1, 2, 3, 5, 6, 87)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[31], line 1
----> 1 squares_list(1, 2, 3, 5, 6, 87)

TypeError: squares_list() takes 1 positional argument but 6 were given
squares_list([1, 2, 3, 5, 6, 87])
[1, 4, 9, 25, 36, 7569]
def compute_f_list(numbers):
    values = []
    for n in numbers:
        values.append(f(n))
    return values
numbers
[3, 4, 2, 5, 6, 12, 56]
compute_f_list(numbers)
[16, 25, 9, 36, 49, 169, 3249]
def compute_sum(numbers):
    sumation = 0
    for n in numbers:
        sumation = sumation + n
    return sumation
compute_sum(numbers)
88
def product(numbers):
    p = 1
    for n in numbers:
        p = p * n
    return p
product(numbers)
483840
for i in range(5):
    print(i)
0
1
2
3
4
range(6)
range(0, 6)
for i in range(6):
    print(i)
0
1
2
3
4
5
for i in range(1, 6):
    print(i)
1
2
3
4
5
for i in range(1, 11):
    print(i)
1
2
3
4
5
6
7
8
9
10

problem

Write a function factorial, it takes a number, n as inputs and computes n! and returns

f(3)
16
f(  f(2) )
100
len(numbers)
7
len("test")
4

Problem

  • Write a function findlens which finds lengths of every word from a given list of words.
    >>> findlens(["one", "two", "three"])
    [3, 3, 5]
  • Write a function find_words_of_len to find words of given length from given list.::
    >>> find_words_of_len(words, 3)
    ['one', 'two', 'six']