Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Day 1 - (21/12/2022)

➣ Basic Operations
x = 2
y = 3
s = x+y
subtraction = x-y
division = x/y
multiplication = x*y
po = x**y
square_root = x**(1/2)
print(s)
print(subtraction)
print(division)
print(multiplication)
print(po)
print(square_root)

➣ Fibonacci Series
def fibonacci(num: int) -> int:
"""
Function takes in a number and returns the fibonacci series
of that number
"""
if(num==1):
return 0
elif(num==2):
return 1
else:
fa, fb = 0, 1
for i in range(2, num + 1):
fa, fb = fb, fa + fb
return fb
storage = []
for i in range(1, 11):
storage.append(str(fibonacci(i)))

print(", ".join(storage))

➣ Prime number of 1st 100000 numbers in best time


import time
def findPrime(n):
prime = [True for i in range(n+1)]
p = 2
while(p * p <= n):
if (prime[p] == True):
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
c = 0

for p in range(2, n):


if prime[p]:
c += 1
return c

t0 = time.time()
c = findPrime(100000)
print("Total prime numbers in range:", c)

t1 = time.time()
print("Time required:", t1 - t0)

➣ Kaprekar Number
number = 9
sum = 0
temp = number*number
while temp>0:
sum += temp%10
temp = temp//10

if sum == number:
print(f"{number} is a Kaprekar number")
else:
print(f"{number} is not a Kaprekar number")
Day 2 - (23/12/2022)
➣ Alphabetical Order (Reverse Sorting) [Remove duplicate]
data = ['a', 'a', 'b', 'c', 'd', 'e', 'e', 'a']
data = list(set(data))
data.sort(reverse=True)
print(data)

➣ Square all the value of the list using lambda function


data = [2, 3, 4, 5]
f = lambda x: x*x
d = [f(i) for i in data]
print(d)

➣ Find the highest marks in a single semester and highest marks overall.
def highest(kwargs: dict) -> (str, str, int):
high = 0
marks = ""
sem = ""
for key, value in kwargs.items():
TempMarks, TempValue = highest_sem(value, key)
if TempValue>high:
high = TempValue
marks = TempMarks
sem = key
return (sem, marks, high)

def highest_sem(kwargs: dict, args: str) -> (str, int):


high = 0
marks = ""
for key, value in kwargs.items():
if value>high:
high = value
marks = key
return (marks, high)

marks = {
"sem1": {
"DSA": 10,
"CO": 11,
"IT": 10,
"MATHS": 4
},
"sem2": {
"DSA": 20,
"CO": 21,
"IT": 20,
"MATHS": 10
},
"sem3": {
"DSA": 15,
"CO": 17,
"IT": 16,
"MATHS": 20
}
}

sem, sub, marks = highest(marks)


print(f"Highest marks obtained in {sub} during {sem} is
{marks}")
➣ Print the total number of vowels and consonants in a string
name = "Bartick"
vowels = [each for each in name if each in "aeiouAEIOU"]
print('Number of vowels:', len(vowels), vowels)

consonants = [each for each in name if each not in "aeiouAEIOU


"]
print('Number of consonants:', len(consonants), consonants)

➣ Put double quote to all the strings of a array.


country = ['Arg', 'Nij', 'Bra' 'Fra']
double_country = [f'"{ele}"' for ele in country]
print(double_country)
Day 3 - (03/01/2023)
➣ Print the following pattern

n = 3
for i in range(1, n+1):
print(" "*(n-i), "*"*(i*2 - 1))
for i in range(n-1, 0, -1):
print(" "*(n-i), "*"*(i*2 - 1))

➣ Create a custom error and raise the error in a factorial program.

class FactorialError(Exception):
def __init__(self, message: str):
self.message = message
super().__init__(self.message)

def factorial(x) -> int:


if x < 0:
raise FactorialError("Sorry, no numbers below zero")
if type(x) in [dict, float, str, list, tuple, set, bool]:
raise FactorialError("Only int is valid")
prod = 1
for i in range(1, x+1):
prod *= i
return prod

n = -1
try:
fact = factorial(n)
print(fact)
except FactorialError as err:
print(err)

➣ Use pandas to find bmi of a certain animal from a sample dataset.

import pandas
from seaborn import load_dataset

df = load_dataset('penguins')

df['bill_length_cm'] = [ i/100 for i in df['bill_length_mm'] ]


df['bill_depth_cm'] = [ i/100 for i in df['bill_depth_mm'] ]
df['flipper_length_cm'] = [ i/100 for i in
df['flipper_length_mm'] ]
df['bmi'] = [

(df['body_mass_g'][i]/1000)/(df['bill_depth_cm'][i]*df['bill_dep
th_cm'][i]) for i in range(len(df['bill_depth_cm']))
]
print(df.head())
Day 4 - (04/01/2023)
➣ Take the input of name of object and it method and show the help of the
function closely related to the imputed method name.

obj = input("Enter the name of the object to search from = ")


totalMethods = dir(eval(obj))
function = input("Enter function name = ")
j = 0
for method in totalMethods:
if method.find(function) == -1:
continue
print(help(eval(f'{obj}.{method}')))
j += 1
if not j:
print("No method found")

You might also like