Python Model Paper 1
Python Model Paper 1
Python Model Paper 1
The above statement produces an type error as python does not allow for
concatenation of different data types such as integer and a string value as
shown above.
1
it is generally not recommended because it can lead to naming conflicts
and make the code harder to read and debug.
addition = lambda x, y: x + y
result = addition(2, 3)
print(result) # Output: 5
In this example, the lambda function is defined with the lambda keyword,
followed by the arguments x and y, separated by a comma. The body of
the lambda function follows the : symbol, which performs the addition of
the two arguments and returns the result.
2
4. What is a flow control statement?. Discuss if and if else statements
with flow chart.
3
The if-else statement is used to execute one block of code if a certain
condition is true and another block of code if the condition is false.
4
NOTE:- Add rectangular End box in both the flow chart.
5
6. How can you prevent a python program from crashing? discuss
different ways to avoid crashing.
1. Exception handling
Python provides a built-in mechanism for handling exceptions that can
occur during program execution. By using try and except statements, you
can catch and handle exceptions that might cause your program to crash.
2. Defensive programming
Defensive programming involves anticipating and handling potential errors
or bugs in your code. One way to do this is to use conditional statements
to check for edge cases or unexpected input. For example, you might
check that a variable is not None before using it, or that a list has a certain
length before accessing an index. By checking for potential errors ahead
of time, you can prevent your program from crashing due to unexpected
behavior.
6
input. For example, if your program expects a number as input, you might
check that the input is a valid number and not a string or other type of
data.
7. Discuss list and dictionary data structure with example for each.
A list is an ordered collection of values, which can be of any data type. You
can add, remove, and modify elements of a list, and you can access
individual elements by their position in the list (also called an index).
Here's an example:
my_list = [1, 2, 3, 4, 5]
my_list.append(6)
my_list.remove(3)
print(my_list[2]) # prints 4
7
8. write a Python program to accept n numbers and store them in a
list. Then print the list without ODD numbers in it.
8
9. write a python program to read dictionary data and delete any given
key entry in the dictionary.
Some of the commonly used clipboard functions in Python for wiki markup are:
9
➢ pyperclip.paste() - This function returns the text currently on the
clipboard. For example, my_text = pyperclip.paste() will retrieve
the text currently on the clipboard and store it in the variable my_text.
➢ pyperclip.waitForNewPaste() - This function waits for new text to
be copied to the clipboard and returns it. This can be useful in situations
where the program needs to wait for the user to copy text to the clipboard
before continuing.
11. Using string slicing operation write a Python program to reverse each
word in a given string
10
NOTE:- In the above program use input() function for sample_string
variable to get the string from the user instead of the
predetermined string.
directory.
➢ Home path: A home path allows you to reference the home directory of the
current user. It uses the tilde ("~") character followed by the username to
specify the home directory. For example, ~/Documents is a home path to
the Documents directory of the current user.
13. Explain how to read specific lines from a file?. illustrate with
python program.
To read specific lines from a file in Python, you can use the readlines()
method to read all the lines into a list, and then access the specific lines
you need using list indexing. Here's an example program that
demonstrates how to do this:
11
14. What is logging? how this would be used to debug the python
program?
12
debug (for detailed information about the program's execution) to error
15. What is the use of ZIP? how to create a ZIP folder explain.
ZIP is a popular file compression and archival format that is widely used
for packaging and distributing files. It is a way to compress multiple files
into a single archive file, making it easier to share or transfer large sets of
files. ZIP files can be created and opened on almost any operating system,
including Windows, macOS, and Linux.
In Python, the zipfile module provides functionality for working with ZIP
files. You can use this module to create a new ZIP file and add files to it, or
to extract files from an existing ZIP file.
13
➢ Create an empty dictionary called clipboard to store the copied text and a
variable called count to keep track of the number of items in the clipboard.
➢ Implement a function called copy() that takes a string as input and adds it
to the clipboard with a unique key generated by incrementing the count
variable.
➢ Implement a function called paste() that takes an integer key as input
and returns the string associated with that key from the clipboard.
➢ Implement a function called list_clipboard() that prints all the keys and
their associated strings in the clipboard.
➢ Implement a loop that repeatedly prompts the user for input and performs
the appropriate operation based on the input. The loop should continue
until the user enters a specific command to exit the program.
17. Discuss how lists would be written in the file and read from the
file?
14
18. Define the terms with example: (i) class (ii) objects (iii) instance
variables.
15
variables by assigning values to them within the __init__() method of the
class. Here's an example:
19. create a Time class with hour, min and sec as attributes. Demonstrate
how two Time objects would be added.
16
In this example, the __add__() method is defined to allow two Time objects
to be added together using the + operator. The to_seconds() method
converts a Time object to seconds, and the from_seconds() method
creates a new Time object from a given number of seconds.
In this example, we create two Time objects (time1 and time2) with
different hours, minutes, and seconds. We then add them together using
the + operator, which calls the __add__() method we defined earlier. The
resulting total_time object represents the sum of the two Time objects,
which we can print out using the __str__() method we defined earlier.
is used to initialize the attributes of the object with the values passed to it
17
(In this example, the __init__() method is defined with self, name, and age
__init__() method is automatically called and the name and age attributes
18
Encapsulation is one of the fundamental concepts of object-oriented
programming (OOP) that involves bundling data and methods that operate
on that data within a single unit, called a class. The primary purpose of
outside world and to provide a public interface for interacting with the
object.
are special keywords that control the visibility of attributes and methods
prefixed with any underscore are considered public and can be accessed
with double underscore (__) are considered private and can only be
19
22. What is a class diagram? Create empty class and corresponding
class diagram for following statements (i) class A derives from class B
and Class C (ii) Class D derived from Class A defined in statement (i)
different ways. In other words, polymorphism allows different classes to have the
overloading.
method that is already defined in its superclass. This allows the subclass to
inherit the method from its superclass but also tailor it to its specific needs.
Method overloading is when a class defines multiple methods with the same
name but different parameters. This allows the class to handle different
20
(In this example, we have a base class Animal with an abstract method
make_sound. We also have two subclasses, Cat and Dog, which inherit from
input and calls their make_sound method to produce their respective sounds.
Since both Cat and Dog classes have implemented the make_sound method, they
can be used interchangeably and produce different sounds when the function is
21
24. Write python program to read cell 2C from sheet 2 of workbook
To read cell 2C from sheet 2 of a workbook using Python, we can use the
25. Explain how pdf pages would created in a pdf document with example.
libraries. Here, I'll provide an example of how to create PDF pages using Python's
reportlab library.
reportlab is a Python library that allows you to create PDF documents from
scratch or modify existing ones. To create a PDF page with this library, you need
to create a canvas object and then add various elements to it, such as text,
22
26. What is JSON? discuss with example. Compare it with dictionary.
interchange format. It is easy for humans to read and write, and for machines to
A JSON data structure consists of key-value pairs, which are similar to Python
dictionaries. However, JSON has a stricter syntax and is typically used for data
23
27. Compare and contrast Excel and CSV files.
Excel and CSV (Comma-Separated Values) files are two types of file formats that
are commonly used for storing and manipulating data. While there are some
similarities between the two formats, there are also some important differences
Excel files are spreadsheet files created by Microsoft Excel or other spreadsheet
software. They can contain multiple sheets, and each sheet can have multiple
cells, columns, and rows. Excel files can be saved in various formats, including
24
XLS, XLSX, and CSV. Excel files can contain various data types, including text,
numbers, dates, and formulas. Excel files also support formatting and styling of
CSV files, on the other hand, are simple text files that contain data in a tabular
format. Each row of data is represented by a line in the file, and each cell is
or styling of cells, and they can only contain simple data types such as text and
numbers.
One of the main advantages of CSV files over Excel files is their simplicity and
compatibility with a wide range of software and platforms. CSV files can be
easily opened and edited in a text editor or spreadsheet software, and they can
Excel files, on the other hand, offer more advanced features such as formulas,
macros, and formatting, which make them more suitable for complex data
analysis and reporting tasks. Excel files can also contain charts, graphs, and
28. Demonstrate how a Class would be converted into JSON object with
an example.
To convert a class object to a JSON object, we can use the json module in
25
(In this example, we define a Person class with name and age attributes. We also
define a custom encoder PersonEncoder that extends the JSONEncoder class and
overrides the default method to handle the Person class. The default method
checks if the object is an instance of Person, and if so, returns a dictionary with
the name and age attributes. Finally, we create a Person object, and use
JSON and dictionaries are similar in that they both represent data in a key-value
format. However, JSON has some differences, such as requiring all keys to be
strings and not allowing trailing commas. JSON also has a more standardized
26
implementation. Additionally, JSON is designed to be language-independent and
to Python.)
29. Explain how a page from different PDFs files would be merged into a
Merging pages from different PDF files into a new PDF file can be achieved using
various Python libraries such as PyPDF2, PyMuPDF, etc. Here are the general
steps to merge pages from different PDF files into a new PDF file using PyPDF2:
4. Add the pages from the PDF files to the merged_pdf object using the
append() method:
27
5. Save the merged PDF file using the write() method:
NOTE:~ If you want you can combine all the snippets into a single program and
28