[go: up one dir, main page]

0% found this document useful (0 votes)
9 views10 pages

Python 1755783164

The document contains a list of Python interview questions and their corresponding answers, covering various topics such as string manipulation, number theory, and data structures. Each question is presented with a brief code snippet demonstrating the solution. The document serves as a study material for individuals preparing for Python programming interviews.

Uploaded by

Rakshith G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Python 1755783164

The document contains a list of Python interview questions and their corresponding answers, covering various topics such as string manipulation, number theory, and data structures. Each question is presented with a brief code snippet demonstrating the solution. The document serves as a study material for individuals preparing for Python programming interviews.

Uploaded by

Rakshith G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

​​ ​ ​ ​

Python Interview Questions & Answers

1. Reverse a string in Python.

s = "hello"
print(s[::-1]) # Output: 'olleh'

2. Check if a number is prime.

def is_prime(n):

til
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:

return True
return False Pa
3. Find the factorial of a number.
g
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
ra

4. Check if a string is a palindrome.

def is_palindrome(s):
Pa

return s == s[::-1]

5. Fibonacci series up to N terms.

def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

6. Find duplicates in a list.

def find_duplicates(lst):
return list(set([x for x in lst if lst.count(x) > 1]))

7. Find the second largest number in a list.

def second_largest(nums):
return sorted(set(nums))[-2]

til
8. Swap two variables without using a third variable.

a, b = 5, 10
a, b = b, a

9. Find the GCD of two numbers.


Pa
import math
g
gcd = math.gcd(8, 12)
ra

10. Check if a list is sorted.

def is_sorted(lst):
return lst == sorted(lst)
Pa

11. Sum of digits in a number.

def sum_digits(n):
return sum(int(d) for d in str(n))

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

12. Count vowels in a string.

def count_vowels(s):
return sum(1 for char in s.lower() if char in "aeiou")

13. Remove punctuation from a string.

import string

til
def clean_text(text):
return text.translate(str.maketrans('', '',
string.punctuation))

14. Check Armstrong number.


Pa
def is_armstrong(n):
g
return n == sum(int(d)**len(str(n)) for d in str(n))
ra

15. Flatten a nested list.


Pa

def flatten(lst):
return [item for sublist in lst for item in sublist]

16. Count frequency of elements in a list.

from collections import Counter


Counter([1,2,2,3,3,3])

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

17. Check anagram strings.

def is_anagram(a, b):


return sorted(a) == sorted(b)

18. Merge two dictionaries.

a = {'x': 1}

til
b = {'y': 2}
a.update(b)

19. Find all even numbers in a list.


Pa
evens = [x for x in [1, 2, 3, 4] if x % 2 == 0]
g
20. Reverse a list.
ra

lst = [1, 2, 3]
lst.reverse()
Pa

21. Remove duplicates from a list.

unique = list(set([1, 2, 2, 3]))

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

22. Sort a dictionary by values.

d = {'a': 3, 'b': 1, 'c': 2}


sorted_d = dict(sorted(d.items(), key=lambda x: x[1]))

23. Find the longest word in a sentence.

til
def longest_word(sentence):
return max(sentence.split(), key=len)

24. Check leap year.


Pa
def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 ==
g
0)
ra

25. Print pattern (triangle of stars).


Pa

for i in range(1, 6):


print('*' * i)

26. Convert decimal to binary.

print(bin(10)) # Output: 0b1010

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

27. Check perfect number.

def is_perfect(n):
return sum(i for i in range(1, n) if n % i == 0) == n

28. List comprehension for squares.

squares = [x**2 for x in range(1, 6)]

til
29. Check substring.

"hello" in "hello world"


Pa
# Output: True

30. Factorize a number.


g
def factors(n):
ra

return [i for i in range(1, n+1) if n % i == 0]


Pa

31. Get common elements from two lists.

list(set([1,2,3]) & set([2,3,4]))

32. Get list of unique values.

list(set([1, 1, 2, 2, 3]))

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

33. Remove empty strings from list.

[x for x in ["a", "", "b"] if x]

34. Zip two lists into dict.

dict(zip(["a", "b"], [1, 2]))

til
35. Check if string contains only digits.

"12345".isdigit() Pa
# Output: True

36. Find minimum and maximum from list.


g
min([1, 2, 3]), max([1, 2, 3])
ra

37. Find square root.


Pa

import math
math.sqrt(16)

38. Check if list has all unique elements.

def is_unique(lst):
return len(lst) == len(set(lst))

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​
39. Lambda to check even number.

even = lambda x: x % 2 == 0

40. Find ASCII value of character.

ord('A') # Output: 65

til
41. Count character frequency in string.

from collections import Counter


Counter("banana")

42. Convert list of strings to int.


Pa
g
list(map(int, ['1', '2', '3']))
ra

43. Check if year is century year.


Pa

def is_century(year):
return year % 100 == 0

44. Sum of list elements using loop.

total = 0
for i in [1,2,3]: total += i

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

45. Check positive/negative number.

def check(n):
return "Positive" if n > 0 else "Negative" if n < 0 else
"Zero"

46. Capitalize first letter of each word.

til
"hello world".title() # Output: 'Hello World'

47. Sort list in descending.

sorted([3,1,2], reverse=True)
Pa
g
48. Check for palindrome number.
ra

def is_palindrome_number(n):
return str(n) == str(n)[::-1]
Pa

49. Reverse words in a sentence.

def reverse_words(sentence):
return ' '.join(sentence.split()[::-1])

50. Check if two lists are equal.

[1, 2, 3] == [1, 2, 3] # Output: True

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment


​​ ​ ​ ​

til
Pa
g
ra
Pa

Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | Study Material/ Book an Appointment

You might also like