0% found this document useful (0 votes)
40 views4 pages

Practice Question - 1

The document contains a series of programming exercises covering user-defined functions, control flow, built-in functions, data structures, and basic object-oriented programming concepts in Python. Each topic includes multiple questions with example code and outputs demonstrating various functionalities such as calculating factorials, checking for prime numbers, manipulating lists and dictionaries, and defining classes. The exercises aim to reinforce foundational programming skills and concepts.

Uploaded by

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

Practice Question - 1

The document contains a series of programming exercises covering user-defined functions, control flow, built-in functions, data structures, and basic object-oriented programming concepts in Python. Each topic includes multiple questions with example code and outputs demonstrating various functionalities such as calculating factorials, checking for prime numbers, manipulating lists and dictionaries, and defining classes. The exercises aim to reinforce foundational programming skills and concepts.

Uploaded by

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

# Topic 1: User-Defined Functions

# Q1. Write a function to calculate the factorial of a number.


def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)

print(factorial(5)) # Output: 120

# Q2. Write a function to check if a number is prime.


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

print(is_prime(7)) # Output: True

# Q3. Write a function to find the maximum of three numbers.


def find_max(a, b, c):
return max(a, b, c)

print(find_max(10, 20, 15)) # Output: 20

# Q4. Write a function to reverse a string.


def reverse_string(s):
return s[::-1]

print(reverse_string("hello")) # Output: "olleh"

# Q5. Write a function to sum all numbers in a list.


def sum_list(lst):
return sum(lst)

print(sum_list([1, 2, 3, 4])) # Output: 10

# Topic 2: If, For, While, Range

# Q6. Write a program that prints "Even" if a number is even, "Odd" otherwise.
num = 4
if num % 2 == 0:
print("Even") # Output: Even
else:
print("Odd")

# Q7. Write a for loop to print numbers from 1 to 10.


for i in range(1, 11):
print(i) # Output: 1 2 3 ... 10

# Q8. Write a while loop to find the sum of the first 10 natural numbers.
n = 10
total = 0
i = 1
while i <= n:
total += i
i += 1
print(total) # Output: 55

# Q9. Write a for loop to iterate over a list of names and print each one.
names = ["Alice", "Bob", "Charlie"]
for name in names:
print(name)
# Output: Alice Bob Charlie

# Q10. Use range() to generate a list of even numbers from 2 to 20.


even_numbers = list(range(2, 21, 2))
print(even_numbers) # Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

# Topic 3: Built-in Functions

# Q11. Use the `len()` function to find the length of a list.


lst = [1, 2, 3, 4, 5]
print(len(lst)) # Output: 5

# Q12. Use the `sorted()` function to sort a list in ascending order.


numbers = [5, 1, 4, 2, 3]
print(sorted(numbers)) # Output: [1, 2, 3, 4, 5]

# Q13. Use `min()` and `max()` to find the smallest and largest elements in a list.
print(min(numbers)) # Output: 1
print(max(numbers)) # Output: 5

# Q14. Use `sum()` to calculate the total sum of a list.


print(sum(numbers)) # Output: 15

# Q15. Use `map()` to square each element in a list.


squares = list(map(lambda x: x**2, numbers))
print(squares) # Output: [25, 1, 16, 4, 9]

# Topic 4: List, Set, Dictionary

# Q16. Write a program to find the length of a list.


fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # Output: 3

# Q17. Write a program to add an element to a set.


my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}

# Q18. Write a program to remove duplicates from a list using a set.


dup_list = [1, 2, 2, 3, 4, 4]
unique_list = list(set(dup_list))
print(unique_list) # Output: [1, 2, 3, 4]

# Q19. Write a program to iterate through a dictionary and print its keys and
values.
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
for key, value in my_dict.items():
print(key, value)
# Output: name Alice, age 25, city New York
# Q20. Write a program to check if a key exists in a dictionary.
if "name" in my_dict:
print("Key exists") # Output: Key exists

# Topic 5: Class and Objects Basics

# Q21. Define a class `Person` with attributes name and age. Write a method to
print the person's details.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def display(self):
print(f"Name: {self.name}, Age: {self.age}")

p1 = Person("Alice", 30)
p1.display() # Output: Name: Alice, Age: 30

# Q22. Create a class `Rectangle` with attributes length and width. Write a method
to calculate area.
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width

def area(self):
return self.length * self.width

rect = Rectangle(5, 3)
print(rect.area()) # Output: 15

# Q23. Define a class `Circle` with an attribute radius. Write a method to


calculate the circumference.
class Circle:
def __init__(self, radius):
self.radius = radius

def circumference(self):
return 2 * 3.14159 * self.radius

c = Circle(4)
print(c.circumference()) # Output: 25.13272

# Q24. Create a class `Student` with attributes name and marks. Write a method to
determine if the student has passed (marks >= 50).
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks

def has_passed(self):
return self.marks >= 50

s1 = Student("Bob", 45)
print(s1.has_passed()) # Output: False

# Q25. Define a class `Book` with attributes title and author. Write a method to
display book details.
class Book:
def __init__(self, title, author):
self.title = title
self.author = author

def details(self):
print(f"Title: {self.title}, Author: {self.author}")

b = Book("1984", "George Orwell")


b.details() # Output: Title: 1984, Author: George Orwell

# Extra Questions for Practice

# Q26. Write a function that takes a string and returns its uppercase version.
def to_uppercase(s):
return s.upper()

print(to_uppercase("hello")) # Output: HELLO

# Q27. Write a program to find the second largest number in a list.


def second_largest(lst):
lst = list(set(lst)) # Remove duplicates
lst.sort()
return lst[-2]

print(second_largest([1, 2, 3, 4, 5])) # Output: 4

# Q28. Write a function to count the vowels in a string.


def count_vowels(s):
vowels = "aeiou"
count = 0
for char in s:
if char.lower() in vowels:
count += 1
return count

print(count_vowels("hello world")) # Output: 3

# Q29. Write a program to check if a string is a palindrome.


def is_palindrome(s):
return s == s[::-1]

print(is_palindrome("madam")) # Output: True

# Q30. Write a class `Car` with attributes make and model. Write a method to print
car details.
class Car:
def __init__(self, make, model):
self.make = make
self.model = model

def display(self):
print(f"Make: {self.make}, Model: {self.model}")

car = Car("Toyota", "Camry")


car.display() # Output: Make: Toyota, Model: Camry

You might also like