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

Python Interview Question

Uploaded by

jashu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Interview Question

Uploaded by

jashu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

python-interview-question

June 28, 2023

1 Rohit Verma
Question 1: Write a Python function to check if a given number is even or odd.
[1]: def is_even_or_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"

Question 2: Implement a function to find the sum of all the elements in a list
[6]: lst=[4,5,2,3,6,4]

def sum_of_list_elements(lst):
return sum(lst)

sum_of_list_elements(lst)

[6]: 24

Question: Write a Python program to calculate the factorial of a given number using
recursion.
[9]: def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

factorial(5)

[9]: 120

Question: Write a Python function to find the Fibonacci series up to a given number.
[11]: def fibonacci(n):
fib_series = [0, 1]
while fib_series[-1] + fib_series[-2] <= n:

1
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series

fibonacci(5)

[11]: [0, 1, 1, 2, 3, 5]

Question: Write a Python program to check if a given string is a palindrome ignoring


spaces and punctuation.
[13]: import re

def is_palindrome(string):
cleaned_string = re.sub('[^A-Za-z0-9]+', '', string.lower())
return cleaned_string == cleaned_string[::-1]

is_palindrome('hello')

[13]: False

Question: Write a Python program to check if a given number is prime.


[18]: def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

is_prime(45)

[18]: False

Question: Write a Python program to find the square of each element in a list.
[19]: def square_list(lst):
return [x**2 for x in lst]

square_list([4,5,2,8])

[19]: [16, 25, 4, 64]

Question: Implement a function to find the maximum difference between any two

2
numbers in a list.
[20]: def max_difference(lst):
return max(lst) - min(lst)

max_difference([7,8,2,3,5])

[20]: 6

Question: claculate the area of triangle


[23]: height=float(input("Enter the area of height: "))
base=float(input("Enter the area of base: "))
area=(1/2)*height*base
print("area of triangle:",area)

Enter the area of height: 72


Enter the area of base: 56
area of triangle: 2016.0
Question: swap two vairable
[24]: x=12
y=13
x,y=y,x
print(x,y)

13 12

[25]: # using third variable


x=13
y=12
temp=13
print(temp)
x=y
print(x)
y=temp
print(y)

13
12
13
Question: Implement a function to check if a given string is an anagram of another
string.
[28]: def is_anagram(string1, string2):
return sorted(string1) == sorted(string2)

3
is_anagram('nothing','nothing')

[28]: True

Question: Given two sorted lists, write a function to merge them into a single sorted
list.
[29]: def merge_sorted_lists(lst1, lst2):
return sorted(lst1 + lst2)

merge_sorted_lists([4,5,2,3],[5,2,3,6])

[29]: [2, 2, 3, 3, 4, 5, 5, 6]

[ ]:

You might also like