0% found this document useful (0 votes)
2 views

AL.ipynb - Colab

The document contains a series of Python functions demonstrating basic programming concepts such as arithmetic operations, area calculations, list manipulations, and seasonal checks. Each function is tested with example inputs, and the outputs are displayed. The code also includes error handling for type mismatches in function arguments.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AL.ipynb - Colab

The document contains a series of Python functions demonstrating basic programming concepts such as arithmetic operations, area calculations, list manipulations, and seasonal checks. Each function is tested with example inputs, and the outputs are displayed. The code also includes error handling for type mismatches in function arguments.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2/13/25, 2:16 PM HarshitaSaraogi_AL1_assignment1.

ipynb - Colab

#1
def add_two_numbers(a, b):
return a+b
add_two_numbers(10,15)

25

#2
def area_of_circle(r):
return 3.14*r*r
area_of_circle(3)

28.259999999999998

#3
def add_all_nums(*args):
try:
print(sum(args))
except TypeError:
print("error")

result= add_all_nums(1,2,3)
print(result)

6
None

result1= add_all_nums("apple","mango")
print(result1)

error
None

#4
def c_to_f(c):
return (c*9/5)+32
c_to_f(0)

32.0

#5
def check_season(month):

if month in ["December", "January", "February"]:


print("Winter")
elif month in ["March", "April", "May"]:
print("Spring")
elif month in ["June", "July", "August"]:
print("Summer")
elif month in ["September", "October", "November"]:
print("Autumn")
else:
print("Invalid month")

check_season("January")

Winter

#6
def calculate_slope(x1,x2,y1,y2):
return (x2-x1)/(y2-y1)
calculate_slope(1,2,3,4)

1.0

#7
def solve_quadratic_eqn(a,b,c):

https://colab.research.google.com/drive/1lW7hIKF4mFCxa9YmcJ67Bd9L-qugjbJ2#scrollTo=_AHbdUApPV0S&printMode=true 1/3
2/13/25, 2:16 PM HarshitaSaraogi_AL1_assignment1.ipynb - Colab
return (-b+(b**2-4*a*c)**0.5)/(2*a),(-b-(b**2-4*a*c)**0.5)/(2*a)
solve_quadratic_eqn(1,2,3)

((-0.9999999999999999+1.4142135623730951j), (-1-1.4142135623730951j))

#8
def print_list(list):
for i in list:
print(i)
print_list([1,2,3,4])

1
2
3
4

#9
import numpy as np
def reverse_list(list):
b=np.array(list)
a=[]
for i in b[::-1]:
a.append(i)

print(a)

reverse_list([1,2,3,4,5])

[5, 4, 3, 2, 1]

#10
def capitalize_list_items(list):
for i in list:
print(i.capitalize())
capitalize_list_items(["monday","tuesday"])

Monday
Tuesday

#11
def add_item(list,item):
list.append(item)
return list

food_staff = ['Potato', 'Tomato', 'Mango', 'Milk']


print(add_item(food_staff, 'Meat')) # ['Potato', 'Tomato', 'Mango','Milk','Meat']

['Potato', 'Tomato', 'Mango', 'Milk', 'Meat']

numbers = [2, 3, 7, 9]
print(add_item(numbers, 5)) #[2, 3, 7, 9, 5]

[2, 3, 7, 9, 5]

#12
def remove_item(list,item):
list.remove(item)
return list

food_staff = ['Potato', 'Tomato', 'Mango', 'Milk']


print(remove_item(food_staff, 'Mango')) # ['Potato', 'Tomato', 'Milk'];

['Potato', 'Tomato', 'Milk']

numbers = [2, 3, 7, 9]
print(remove_item(numbers, 3)) # [2, 7, 9]

https://colab.research.google.com/drive/1lW7hIKF4mFCxa9YmcJ67Bd9L-qugjbJ2#scrollTo=_AHbdUApPV0S&printMode=true 2/3
2/13/25, 2:16 PM HarshitaSaraogi_AL1_assignment1.ipynb - Colab

[2, 7, 9]

#13
def sum_of_numbers(n):
return n*(n+1)/2
sum_of_numbers(15)

120.0

#14
def sum_of_odd_numbers(n):
num= (n+1)//2;
return num*num

sum_of_odd_numbers(5)

#15
def sum_of_even_numbers(n):
return (n//2)*((n+2)//2)
sum_of_even_numbers(10)

30

https://colab.research.google.com/drive/1lW7hIKF4mFCxa9YmcJ67Bd9L-qugjbJ2#scrollTo=_AHbdUApPV0S&printMode=true 3/3

You might also like