0% found this document useful (0 votes)
2 views7 pages

50 Real Python Coding Questions

The document contains 50 Python coding questions along with their answers, covering a variety of topics such as basic arithmetic, string manipulation, list operations, and control structures. Each question is presented with a code snippet demonstrating the solution. This resource serves as a quick reference for common Python programming tasks and concepts.

Uploaded by

zdtyg
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)
2 views7 pages

50 Real Python Coding Questions

The document contains 50 Python coding questions along with their answers, covering a variety of topics such as basic arithmetic, string manipulation, list operations, and control structures. Each question is presented with a code snippet demonstrating the solution. This resource serves as a quick reference for common Python programming tasks and concepts.

Uploaded by

zdtyg
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/ 7

50 Python Coding Questions with Answers

1. Add two numbers.

a = 10

b=5

print('Sum:', a + b)

2. Check if a number is even or odd.

n=7

print('Even' if n % 2 == 0 else 'Odd')

3. Find the factorial of a number.

def factorial(n):

return 1 if n == 0 else n * factorial(n-1)

print(factorial(5))

4. Check if a number is prime.

n=7

if n > 1:

for i in range(2, n):

if n % i == 0:

print('Not Prime')

break

else:

print('Prime')

else:

print('Not Prime')

5. Print Fibonacci series up to n terms.

n = 10

a, b = 0, 1
for _ in range(n):

print(a, end=' ')

a, b = b, a + b

6. Check if a string is palindrome.

s = 'madam'

print('Palindrome' if s == s[::-1] else 'Not Palindrome')

7. Count vowels in a string.

s = 'hello world'

vowels = 'aeiou'

print('Vowels:', sum(1 for c in s if c in vowels))

8. Find the largest number in a list.

lst = [1, 4, 2, 10]

print('Max:', max(lst))

9. Sort a list.

lst = [4, 1, 3]

lst.sort()

print(lst)

10. Reverse a list.

lst = [1, 2, 3]

print(lst[::-1])

11. Check positive, negative, or zero.

n=0

print('Positive' if n > 0 else 'Negative' if n < 0 else 'Zero')

12. Sum of digits.

n = 123

print(sum(int(d) for d in str(n)))

13. Find GCD of two numbers.


import math

print(math.gcd(12, 15))

14. Find LCM of two numbers.

import math

a, b = 12, 15

print(abs(a * b) // math.gcd(a, b))

15. Swap two variables.

a, b = 5, 10

a, b = b, a

print(a, b)

16. Print even numbers from 1 to 10.

print([i for i in range(1, 11) if i % 2 == 0])

17. Find second largest in list.

lst = [4, 1, 5, 2]

lst.sort()

print(lst[-2])

18. Remove duplicates from list.

lst = [1, 2, 2, 3]

print(list(set(lst)))

19. Armstrong number check.

n = 153

print('Armstrong' if n == sum(int(d)**3 for d in str(n)) else 'Not Armstrong')

20. Length of string without len().

s = 'hello'

count = 0

for _ in s:

count += 1
print(count)

21. Print multiplication table.

n=5

for i in range(1, 11):

print(n, 'x', i, '=', n*i)

22. Reverse a string.

s = 'hello'

print(s[::-1])

23. Convert Celsius to Fahrenheit.

c = 37

f = (c * 9/5) + 32

print(f)

24. Calculate square root.

import math

print(math.sqrt(16))

25. Check leap year.

year = 2024

print('Leap' if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) else 'Not Leap')

26. Count words in string.

s = 'hello world again'

print(len(s.split()))

27. Check if element exists in list.

lst = [1, 2, 3]

print(2 in lst)

28. Merge two lists.

a = [1, 2]

b = [3, 4]
print(a + b)

29. Get unique values from list.

lst = [1, 1, 2, 3]

print(list(set(lst)))

30. Find min and max in list.

lst = [3, 1, 4]

print(min(lst), max(lst))

31. Replace item in list.

lst = [1, 2, 3]

lst[1] = 5

print(lst)

32. Find common elements in lists.

a = [1, 2, 3]

b = [2, 3, 4]

print(list(set(a) & set(b)))

33. Sum of elements in list.

lst = [1, 2, 3]

print(sum(lst))

34. Create a list of squares.

print([i**2 for i in range(1, 6)])

35. Find index of element.

lst = [10, 20, 30]

print(lst.index(20))

36. Convert list to string.

lst = ['a', 'b']

print(''.join(lst))

37. Count frequency of element.


lst = [1, 2, 2, 3]

print(lst.count(2))

38. Print elements using loop.

lst = [1, 2, 3]

for i in lst:

print(i)

39. Square of number using lambda.

sq = lambda x: x**2

print(sq(5))

40. Filter even numbers.

lst = [1, 2, 3, 4]

print(list(filter(lambda x: x % 2 == 0, lst)))

41. Find ASCII value of character.

print(ord('A'))

42. Check if string is digit.

s = '123'

print(s.isdigit())

43. Convert string to int.

s = '10'

print(int(s))

44. Create dictionary from list.

lst = ['a', 'b']

print({i: ord(i) for i in lst})

45. Iterate dictionary.

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

for k, v in d.items():

print(k, v)
46. Get list length.

lst = [1, 2, 3]

print(len(lst))

47. Convert int to binary.

n=5

print(bin(n))

48. Print current date.

import datetime

print(datetime.date.today())

49. Use try-except block.

try:

x=1/0

except ZeroDivisionError:

print('Error')

50. Define and call a function.

def greet():

print('Hello')

greet()

You might also like