Check if a number is prime
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5)+1):
if num % i == 0:
return False
return True
print(is_prime(7))
Check if a number is even or odd
def check_even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
print(check_even_odd(7))
Find the factorial of a number
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
return result
print(factorial(5))
Reverse a string
def reverse_string(s):
return s[::-1]
print(reverse_string("hello"))
Check if a string is a palindrome
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("radar"))
Find the largest number in a list
def find_largest(numbers):
return max(numbers)
print(find_largest([1, 5, 9, 2])) def find_largest(numbers):
return max(numbers)
print(find_largest([1, 5, 9, 2]))
Count number of vowels in a string
def count_vowels(s):
vowels = "aeiouAEIOU"
return sum(1 for char in s if char in vowels)
print(count_vowels("education"))
Fibonacci sequence up to n terms
def fibonacci(n):
seq = [0, 1]
for i in range(2, n):
[Link](seq[i-1] + seq[i-2])
return seq[:n]
print(fibonacci(7))
Swap two variables
a, b = 5, 10
a, b = b, a
print(a, b)
Sum of all elements in a list
def sum_list(lst):
return sum(lst)
print(sum_list([1, 2, 3, 4]))
Remove duplicates from a list
def remove_duplicates(lst):
return list(set(lst))
print(remove_duplicates([1, 2, 2, 3, 4, 4, 5]))
Find the second largest number in a list
def second_largest(lst):
lst = list(set(lst)) # Remove duplicates
[Link]()
return lst[-2]
print(second_largest([10, 20, 4, 45, 99, 99]))
Sum of digits of a number
def sum_of_digits(num):
return sum(int(digit) for digit in str(num))
print(sum_of_digits(1234))
Check if two strings are anagrams
def are_anagrams(str1, str2):
return sorted(str1) == sorted(str2)
print(are_anagrams("listen", "silent"))
Sort a list in ascending order
def sort_list(lst):
return sorted(lst)
print(sort_list([5, 2, 9, 1]))
1. What are Python’s key features?
Python is interpreted, dynamically typed, object-oriented, has extensive libraries, and
is easy to learn.
2. Difference between list and tuple?
Lists are mutable; tuples are immutable.
3. Difference between deep copy and shallow copy?
Shallow copy copies references; deep copy copies the object and its internal objects.
4. Difference between is and ==?
is checks memory location (identity), == checks value (equality).
5. What are Python’s basic data types?
int, float, str, list, tuple, set, dict, bool.
6. What are *args and **kwargs?
*args: variable non-keyword arguments.
**kwargs: variable keyword arguments.
7. Purpose of self in Python classes?
self refers to the instance of the class, used to access attributes and methods.
8. Class method vs Static method?
Class method: has access to class (cls).
Static method: independent, no access to self or cls.
9. What is a Python decorator?
A function that modifies the behavior of another function.
10. Purpose of __init__() method?
It initializes object attributes during instance creation.
11. What is List Comprehension? Example?
Short syntax to create lists:
Example: [x**2 for x in range(5)]
12. What are lambda functions?
Small anonymous functions defined with lambda.
13. How is memory managed in Python?
Through reference counting, garbage collection, and private heap space.
14. Difference between append() and extend()?
append(): adds one item.
extend(): adds multiple items from an iterable.
15. What are modules and packages?
Module: .py file.
Package: folder with multiple modules (and __init__.py).
16. Explain GIL (Global Interpreter Lock).
Only one thread can execute Python bytecode at a time, limiting true multithreading.
17. What are exceptions and how to handle them?
Errors during execution.
Handled using try-except blocks.
18. Python 2 vs Python 3?
Python 3 has print as a function, better Unicode support, and different integer
division.
19. What is pickling and unpickling?
Pickling: serialize Python object into a byte stream.
Unpickling: convert byte stream back to Python object.
20. Difference between @staticmethod and @classmethod?
@staticmethod: no access to self or cls.
@classmethod: takes cls parameter and works with the class.