Python Programming Answer Key
Python Programming Answer Key
Answers (Short)
A list in Python is a collection data type that is ordered, changeable, and allows duplicate elements. It is defined using
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
Reserved words, or keywords, are words that have a predefined meaning in Python and cannot be used as variable
A dictionary is a Python data type that stores key-value pairs. It is unordered and changeable. Defined using curly
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.
Runtime errors occur during program execution and are typically due to invalid operations, such as dividing by zero or
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.
Inheritance allows one class to inherit attributes and methods from another, promoting code reuse. Defined by
Answers (Brief)
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.
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.
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.
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.
Python is a high-level, interpreted programming language known for its readability and ease of use. It supports multiple
The exception model in Python handles errors through try-except blocks. When an error occurs, Python searches for a
An instance method is a function defined within a class that operates on instances of that class. It requires self as the
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_set = {1, 2, 3, 3}
print(my_set)
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:
Exception handling in Python is done using try, except, else, and finally blocks.
Example:
try:
1/0
except ZeroDivisionError:
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
Example:
f.write('Hello, World!')