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

Revision Python

The document contains a series of questions and answers related to Python programming concepts, including syntax, data types, functions, and object-oriented programming. It also includes examples of Python code for various tasks such as finding the largest number in a list, handling exceptions, and understanding the difference between shallow and deep copies. Additionally, it explains built-in data structures and the differences between 'is' and '==' in Python.

Uploaded by

Okwir Edmond
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)
12 views10 pages

Revision Python

The document contains a series of questions and answers related to Python programming concepts, including syntax, data types, functions, and object-oriented programming. It also includes examples of Python code for various tasks such as finding the largest number in a list, handling exceptions, and understanding the difference between shallow and deep copies. Additionally, it explains built-in data structures and the differences between 'is' and '==' in Python.

Uploaded by

Okwir Edmond
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

1. Which of the following is the correct syntax to output "Hello, World!

" in
Python?

Answer:
b) print("Hello, World!")

2. Which of the following is not a valid Python data type?

Answer:
d) number

3. What will be the output of the following Python code?


python
Syntax
x = [1, 2, 3]
y = x
x[0] = 10
print(y)

Answer:
b) [10, 2, 3]

4. What is the correct way to define a function in Python?

Answer:
b) def myFunc():

5. What is the output of the following Python code?


python
Syntax
x = 5
y = 10
print(x > y)

Answer:
b) False
6. Which of the following is used to create a tuple in Python?

Answer:
c) ()

7. What is the result of the following expression in Python?


python
Syntax
3 * 3 ** 3

Answer:
b) 81

8. What will be the output of the following Python code?


python
Syntax
x = 10
y = 20
z = 30
print(x < y and y < z)

Answer:
a) True

9. Which of the following statements is true about lists in Python?

Answer:
b) Lists are ordered collections.

10. What does the len() function do in Python?

Answer:
b) Returns the length of an object
11. How do you create a variable in Python?

Answer:
a) variable = value

12. Which of the following Python methods is used to add an element to a list?

Answer:
b) list.append()

13. What is the output of the following code?


python
Syntax
print("Python"[1:4])

Answer:
b) yth

14. How can you handle exceptions in Python?

Answer:
a) try...except

15. Which of the following is used to remove an item from a list in Python?

Answer:
a) list.remove()

16. What will be the output of the following code?


python
Syntax
x = [1, 2, 3]
y = x
x = [4, 5, 6]
print(y)
Answer:
b) [1, 2, 3]

17. What is the purpose of the self keyword in Python?

Answer:
a) To refer to the current instance of a class

18. How can you make a Python function return multiple values?

Answer:
b) Return a list or tuple

19. What does the break statement do in a loop in Python?

Answer:
b) Exits the loop entirely

20. Which of the following is a correct way to import a module in Python?

Answer:
a) import module

SECTION B

1. Write a Python program to perform the following:

a) Accept a list of numbers and return the largest number in the list.

python
Syntax
numbers = [3, 5, 2, 8, 1]
largest = max(numbers)
print("Largest number:", largest)

Answer: Largest number is 8.


b) Accept a list of numbers and return the smallest number in the list.

python
Syntax
smallest = min(numbers)
print("Smallest number:", smallest)

Answer: Smallest number is 1.

c) Sort the list in ascending order and return the sorted list.

python
Syntax
sorted_list = sorted(numbers)
print("Sorted list:", sorted_list)

Answer: Sorted list is [1, 2, 3, 5, 8].

d) Find the sum of all elements in the list.

python
Syntax
total_sum = sum(numbers)
print("Sum of elements:", total_sum)

Answer: Sum of elements is 19.

e) Return the average of the numbers in the list.

python
Syntax
average = total_sum / len(numbers)
print("Average:", average)

Answer: Average is 3.8.

2. Explain the concept of object-oriented programming (OOP) in Python.

a) Four Main Principles of OOP:

 Encapsulation: Bundling data and methods that operate on the data into a single unit.
 Abstraction: Hiding the complex implementation details and showing only essential features.
 Inheritance: A class can inherit attributes and methods from another class.
 Polymorphism: A method can have different implementations depending on the object.

b) Python Code for Car class:


python
Syntax
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def car_details(self):
return f"{self.year} {self.make} {self.model}"

# Creating an instance
car = Car("Toyota", "Camry", 2021)
print(car.car_details())

Answer: The output is: 2021 Toyota Camry.

3. Write a Python function that:

a) Removes vowels from a string:

python
Syntax
def remove_vowels(s):
vowels = "aeiouAEIOU"
return "".join([char for char in s if char not in vowels])

print(remove_vowels("Hello World")) # Hll Wrld

Answer: Hll Wrld

b) Explanation:
The function removes vowels by iterating over the string and checking if each character is a
vowel, and if not, adding it to the result.

c) Test cases:

python
Syntax
print(remove_vowels("Python")) # Pythn
print(remove_vowels("aeiou")) # ""

4. Difference between deepcopy() and shallow copy().

a) Definition:
 Shallow Copy: Copies the reference to the object. Changes to the copied object affect the
original.
 Deep Copy: Copies the entire object and nested objects. Changes to the copied object do not
affect the original.

b) Code to demonstrate:

python
Syntax
import copy

original_list = [1, [2, 3], 4]


shallow_copied_list = copy.copy(original_list)
deep_copied_list = copy.deepcopy(original_list)

# Modifying the copy


shallow_copied_list[1][0] = 99
deep_copied_list[1][0] = 55

print("Original List:", original_list)


print("Shallow Copy:", shallow_copied_list)
print("Deep Copy:", deep_copied_list)

Answer:

 Shallow Copy modifies the original list since lists are mutable.
 Deep Copy keeps the original list unchanged.

c) Output Explanation:

 Shallow Copy: [1, [99, 3], 4] (The nested list is modified in the original).
 Deep Copy: [1, [55, 3], 4] (The original list is unchanged).

5. Write a Python program that:

a) Checks if a number is prime:

python
Syntax
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True

print(is_prime(5)) # True
print(is_prime(10)) # False
Answer: True for 5, False for 10.

b) Explanation:
The program checks if the number is divisible by any number other than 1 and itself. If divisible,
it is not prime.

c) Sample output:

 5 is a prime number.
 10 is not a prime number.

6. Python’s built-in data structures:

a) List:
A list is a mutable, ordered collection of elements.
Example:

python
Syntax
lst = [1, 2, 3]

b) Tuple:
A tuple is an immutable, ordered collection of elements.
Example:

python
Syntax
tpl = (1, 2, 3)

c) Dictionary:
A dictionary is a collection of key-value pairs.
Example:

python
Syntax
dct = {"a": 1, "b": 2}

d) Set:
A set is an unordered collection of unique elements.
Example:

python
Syntax
st = {1, 2, 3}

e) Use case examples:


 List: Used when order matters and data is mutable.
 Tuple: Used for fixed data collections.
 Dictionary: Used for fast lookups via keys.
 Set: Used for uniqueness and set operations like union and intersection.

7. Write a Python program to handle exceptions:

a) Explanation of exception handling:

python
Syntax
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")

b) Handling file opening errors:

python
Syntax
try:
f = open("nonexistentfile.txt", "r")
except FileNotFoundError:
print("File not found!")

c) Explanation of how exception handling improves program robustness: It allows the program
to continue running by handling errors gracefully and not crashing.

8. Difference between is and ==:

a) is checks object identity (whether two references point to the same object in memory).
== checks if two objects have the same value.

b) Code to demonstrate:

python
Syntax
a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b) # True
print(a is b) # False
print(a is c) # True

c) Output Explanation:
 a == b: True because they have the same values.
 a is b: False because they refer to different objects.
 a is c: True because c is a reference to a.

You might also like