0% found this document useful (0 votes)
18 views5 pages

Python Programming Answer Key

Uploaded by

Sed Person
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views5 pages

Python Programming Answer Key

Uploaded by

Sed Person
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Python Programming Answer Key

Section A (2 Marks Each)

Answers (Short)

1. Define the term List.

A list in Python is a collection data type that is ordered, changeable, and allows duplicate elements. It is defined using

square brackets, e.g., my_list = [1, 2, 3].

2. Write about Variable.

A variable in Python is a symbolic name that is a reference or pointer to an object. It is used to store data that can be

used and modified throughout the program.

3. Define Reserve word.

Reserved words, or keywords, are words that have a predefined meaning in Python and cannot be used as variable

names, e.g., if, for, while.

4. Write about Dictionary.

A dictionary is a Python data type that stores key-value pairs. It is unordered and changeable. Defined using curly

braces, e.g., my_dict = {'key': 'value'}.

5. Write about Type coercion.

Type coercion is the automatic or implicit conversion of data from one type to another, such as from an integer to a float.

6. What is a function?

A function is a reusable block of code that performs a specific task. It is defined using the def keyword in Python.

7. Write about runtime errors.

Runtime errors occur during program execution and are typically due to invalid operations, such as dividing by zero or

accessing an undefined variable.

8. Write note on data streams.


Data streams are sequences of data elements made available over time, often used for input/output operations. In

Python, they can be handled with file operations or network sockets.

9. What is a class?

A class in Python is a blueprint for creating objects (instances) that defines attributes and methods. Defined using the

class keyword.

10. Write about inheritance.

Inheritance allows one class to inherit attributes and methods from another, promoting code reuse. Defined by

specifying a parent class in a new class definition.

Section B (5 Marks Each)

Answers (Brief)

11. Explain about Tuple.

A tuple is a collection data type in Python that is ordered and immutable, meaning it cannot be modified after creation. It

is defined using parentheses, e.g., my_tuple = (1, 2, 3). Tuples are often used for data that should not change.

12. Write a program using three built-in functions.

my_list = [1, 2, 3, 4]

print(len(my_list))

print(sum(my_list))

print(max(my_list))

This program uses len, sum, and max to get the length, sum, and maximum of a list.

13. Write a program using list and dictionary.

students = [{'name': 'Alice', 'age': 20}, {'name': 'Bob', 'age': 22}]

for student in students:

print(student['name'], student['age'])

This program creates a list of dictionaries representing students and prints their details.
14. Write a note on Lambda.

A lambda function is an anonymous, inline function in Python defined using the lambda keyword. It can take multiple

arguments but has only one expression, e.g., lambda x: x + 1. Useful for short, one-time operations.

15. Write about dir and help function.

The dir() function lists the attributes and methods of an object, while help() provides documentation for Python objects,

functions, and modules. Both are useful for understanding and exploring Python objects.

16. Write about Python.

Python is a high-level, interpreted programming language known for its readability and ease of use. It supports multiple

programming paradigms, including procedural, object-oriented, and functional programming.

17. Write about exception model.

The exception model in Python handles errors through try-except blocks. When an error occurs, Python searches for a

matching except block to handle it, allowing graceful error handling.

18. Write about instance method.

An instance method is a function defined within a class that operates on instances of that class. It requires self as the

first parameter and can access and modify instance attributes.

Section C (10 Marks Each)

Answers (Detailed)

19. Explain about dictionary and set in detail with example program.

A dictionary in Python is a collection of key-value pairs, where each key must be unique. It allows for fast data retrieval.

A set is an unordered collection of unique items, useful for membership tests and removing duplicates.

Example:

my_dict = {'apple': 1, 'banana': 2}


print(my_dict['apple'])

my_set = {1, 2, 3, 3}

print(my_set)

20. Explain about function and module.

A function is a reusable block of code that performs a specific task, defined using def. A module is a file containing

Python definitions and statements, which can be imported into other scripts to reuse functions, classes, and variables.

Example:

import math allows access to mathematical functions.

21. Write about operators.

Python has various operators:

- Arithmetic operators: +, -, *, / for mathematical calculations.

- Comparison operators: ==, !=, >, < for comparing values.

- Logical operators: and, or, not for logical operations.

Operators help perform operations on variables and values.

22. Write about Exception handling.

Exception handling in Python is done using try, except, else, and finally blocks.

- try: Block where code execution is attempted.

- except: Block that executes if an error occurs in the try block.

- else: Executes if no error occurs.

- finally: Executes regardless of error occurrence.

Example:

try:

1/0
except ZeroDivisionError:

print('Cannot divide by zero.')

23. Write about file organization.

File organization in Python involves reading from and writing to files using open, read, write, and close methods. Files

can be opened in various modes, such as 'r' for read, 'w' for write, and 'a' for append. Proper file handling includes

closing files to free up resources.

Example:

with open('file.txt', 'w') as f:

f.write('Hello, World!')

You might also like