Categories:Viewed: 5 - Published at: a few seconds ago

Write a python program to add two list of same length.

def add_two_list_items():
    num1 = [1,2,3]
    num2 = [4,5,6]
    sum = num1 + num2
    print(f'Sum: {sum}')

Write a python program to add numbers from two list if first list item is even and second list item is odd.

def add_two_lists_even_odd(l1, l2):
    new = []
    for x, y in zip(l1, l2):
        if l1%2 == 0 and l2%2 != 0:
            new.append(x+y)
    return new

Write a python program Convert KM/H to MPH

kmh = 50
mph =  0.6214 * kmh
print("Speed:", kmh, "KM/H = ", mph, "MPH")

Write a program to find and print the smallest among three numbers

num1 = 100
num2 = 200
num3 = 300
if (num1 <= num2) and (num1 <= num3):
    smallest = num1
elif (num2 <= num1) and (num2 <= num3):
    smallest = num2
else:
    smallest = num3
print(f'smallest:{smallest}')

Write a function to sort a list

raw_list = [-5, -23, 5, 0, 23, -6, 23, 67]
sorted_list = []
while raw_list:
    minimum = raw_list[0]   
    for x in raw_list: 
        if x < minimum:
            minimum = x
    sorted_list.append(minimum)
    raw_list.remove(minimum)    

print(soreted_list)

Write a function to print the time it takes to run a function

import time
def time_it(fn, *args, repetitons= 1, **kwargs):
    start = time.perf_counter()
    if (repetitons <= 0):
        raise ValueError("repetitions should be greater that 0")
    if (not(isinstance(repetitons,int))):
        raise ValueError("Repetions must be of type Integer")
    for _ in range(repetitons):
        fn(*args, **kwargs)
    stop = time.perf_counter()
    return ((stop - start)/repetitons)

Write a python function to calculate simple Interest

def simple_interest(p,t,r): 

    si = (p * t * r)/100
    return si 

Write a python program to print all Prime numbers in an Interval

start = 11
end = 25

for i in range(start,end):
  if i>1:
    for j in range(2,i):
        if(i % j==0):
            break
    else:
        print(i)

Write a python funtion to implement a counter to record how many time the word has been repeated using closure concept

def word_counter():
    counter = {}
    def count(word):
        counter[word] = counter.get(word, 0) + 1
        return counter[word]
    return count

Write a python program to check and print if a string is palindrome or not

st = 'malayalam'
j = -1
flag = 0
for i in st:
    if i != st[j]:
      j = j - 1
      flag = 1
      break
    j = j - 1
if flag == 1:
    print("Not a palindrome")
else:
    print("It is a palindrome")

Write a python function to find the URL from an input string using the regular expression

import re 
def Find(string): 
    regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
    url = re.findall(regex,string)       
    return [x[0] for x in url] 

Write a python program to find N largest elements from a list

l = [1000,298,3579,100,200,-45,900] 
n = 4
l.sort() 
print(l[-n:])

Write a python program to add two lists using map and lambda

nums1 = [1, 2, 3]
nums2 = [4, 5, 6]
result = map(lambda x, y: x + y, nums1, nums2)
print(list(result))

Write a python functionto test the equality of the float numbers

def float_equality_testing(a, b):

    rel_tol = 1e-12
    abs_tol = 1e-05
    return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

Write a python function to caclucate the polygon_area

def polygon_area( side_length, sides = 3):
    if(sides < 3 or sides > 6 ):
        raise ValueError("number of sides must be greater than 2 and less than 7")
    if(side_length < 0 ):
        raise ValueError("side length must be positive")

    return sides * (side_length ** 2) / (4 * tan(pi / sides))

Write a python program to get positive elements from given list of lists

Input = [[10, -11, 222], [42, -222, -412, 99, -87]] 
temp = map(lambda elem: filter(lambda a: a>0, elem), Input) 
Output = [[a for a in elem if a>0] for elem in temp]