Class 12 ICSE Python QA
Class 12 ICSE Python QA
Q5. Write a Python program to calculate the factorial of a number using a loop.
A5. Program:
```python
num = int(input('Enter a number: '))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print('Factorial:', factorial)
```
4. Functions and Scope
Q6. Define a function in Python.
A6. A function is a block of code that performs a specific task. It is defined using the 'def'
keyword.
5. String Manipulation
Q8. How do you concatenate two strings in Python?
A8. Two strings can be concatenated using the '+' operator.
7. File Handling
Q12. Explain how to open a file in Python.
A12. A file can be opened using the open() function. Example: file = open('filename.txt', 'r')
8. Exception Handling
Q14. What is exception handling in Python?
A14. Exception handling is a mechanism in Python to handle errors gracefully using try-
except blocks.
Q17. Write a simple class representing a Car with attributes and a method to display details.
A17. Program:
```python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f'Car make: {self.make}, Model: {self.model}')
```