APP Solution 2022-1
APP Solution 2022-1
Q.1 (a) Give the difference between Tuple and List in python. 03
Tuple List
The Tuple is immutable. The List is mutable.
The Tuple is Enclosed in parentheses ( ) The List is Enclosed in square brackets [ ]
The Tuple has Fixed length. The List has Variable length
The Tuple provides less functionality than The List provides more functionality than
the List. a Tuple.
The Tuples are more memory efficient The Lists are less memory efficient than a
because of its immutability. Tuple
- The key and its value is separated by colon ( : ) and all key-value is separated by
comma ( , ) and enclosed in curly braces ( { } ).
OR
(c) What is a list in python? Write a program that finds maximum and minimum
numbers from a list. 07
- A List in python is used to store the sequence of various types of data.
- The in the List are separated with the comma ( , ) and enclosed with the square
brackets ( [ ] ).
Subject Code: 4321602 Subject Name: Advanced Python Programming
(c) Explain different ways of importing package. Give one example of it. 07
- In Python, there are several ways to import packages or modules. Here are the
different ways of importing packages:
- This imports the entire package/module, and you can access its contents using dot
notation, such as < package_name.module_name.function() >.
- This imports a specific module from the package, allowing you to directly access its
contents without using the package name. You can then use the module's functions or
classes directly, like < module_name.function() >.
- This imports the package/module and assigns it an alias or a shorter name. You can
then use the alias to access the package/module's contents.
- This imports a specific module from the package and assigns it an alias. You can then
use the alias to access the module's contents directly.
- Here's an example that demonstrates these different ways of importing:
Subject Code: 4321602 Subject Name: Advanced Python Programming
OR
Q.2 (a) Write down the properties of dictionary in python. 03
- Properties of Dictionary: -
- Mutable: Dictionaries are mutable, which means you can modify, add, or remove
key-value pairs after they are created.
- No Duplicate Keys: Dictionaries do not allow duplicate keys. If you try to add a key-
value pair with a key that already exists, the previous value will be replaced.
- Example: -
- Unordered: Dictionary elements are not stored in any specific order. The order of
items may vary, and it is not preserved in the dictionary.Unordered: Dictionary
elements are not stored in any specific order. The order of items may vary, and it is
not preserved in the dictionary.
Subject Code: 4321602 Subject Name: Advanced Python Programming
(c) Write a program to define module to find sum of two numbers. Import module to
another program. 07
- sum_module.py
- main_program.py
OUTPUT: -
Q.3 (a) What is Runtime error and Logical error. Explain with example. 03\
- “Python compiler” does not detect these type of error, it is detected by “PVM”
( Python Virtual Machine ).
Subject Code: 4321602 Subject Name: Advanced Python Programming
- Example: -
2) Logical Error: - It is a type of error that executes the code without problem, but the
result displayed is wrong.
- Example: -
OUTPUT: -
- “Except” is a keyword used to define a block of code that is executed when a specific
exception is raised within a preceding “try” block.
- The “excerpt” block is where you specify the types of exception you want to catch,
and you can include the appropriate error handling code to address the exception
scenario.
- Built-in Exception Types: Python provides a wide range of built-in exception types
that cover various error conditions. Some common exception types include
- ValueError
- TypeError
- FileNotFoundError
- IndexError, and
(c) Write a user defined exception that could be raised when the text entered by a
user consists of less than 10 characters. 07
class TException(Exception):
pass
n=10
while True:
try:
a=input("Enter Your String : - ")
print("\r")
l=len(a)
if l<n :
raise TException
break
except TException :
print("You Have Entered Less Than 10 Characters ")
print("\r")
print("Plz Enter 10 OR More Than 10 Characters ")
print("\r")
print("!! CORRECT !!")
Subject Code: 4321602 Subject Name: Advanced Python Programming
OUTPUT: -
OR
Q.3 (a) What are the built-in exceptions and gives its types. 03
- A list of common exceptions that can be thrown from a standard python programme
is given below.
1) SyntaxError:
2) TypeError:
3) NameError:
- Description: Raised when a local or global name is not found.
- Example:
4) ZeroDivisionError:
- Example:
Subject Code: 4321602 Subject Name: Advanced Python Programming
(b) Explain Syntax error and how do we identify it? Give an example. 04
- Syntax Error: - It’s one of the most basic types of error in programming whenever
we do not write a proper syntax, It generates syntax error.
- To identify syntax error, The error message usually indicates the line number or
location where the error occurred and provides information about the specific nature
of the error.
- By analyzing this information, developers can identify the cause of the syntax
error and correct it.
- Example: -
- In this case, the error is a missing closing parenthesis in the print statement. If you
attempt to run this code, Python will raise a syntax error with a message similar to:
Subject Code: 4321602 Subject Name: Advanced Python Programming
- 1) try: - The “ try ” block contains the code that have the exceptions.
- This block is where you specify the types of exception you want to catch, and you
can include the appropriate except scenario.
- It’s always be executed, where the “try-except" block has an exception or not.
- Example: -
OUTPUT: -
Subject Code: 4321602 Subject Name: Advanced Python Programming
- Syntax:
file = open("filename.txt", "mode")
- Example: -
f = open(“abc.txt”,‘r’)
2) Reading from a file: - After opening a file, you can read its content using the
“read()” method. It returns the entire content of the file as a string.
- Syntax:
1) content = file.read() # Read the entire content of the file
2) line = file.readline() # Read a single line from the file
3) lines = file.readlines() # Read all lines of the file and return as a list
- Example: -
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()
3) Writing to a file: - You can write data to a file using the “write()” method. It
replaces the existing content with the new data. If the file doesn’t exist, It will be created.
- Syntax:
file.write("content")
- Example: -
file = open("example.txt", "w")
file.write("Hello, World!")
file.close()
- Syntax:
file = open("filename.txt", "a")
file.write("content")
- Example: -
file = open("example.txt", "a")
file.write("Appending new content.")
file.close()
5) Closing a file: - After performing operation on a file, its important to close the file
using “close()” method
- Syntax:
file.close()
- Example: -
(b) Give list of file modes. Write Description of any four mode. 04
File opening modes determine the purpose & permissions of file access when
mopening a file in python.
- "r": Read mode - Opens a file for reading. The file must exist.
- "w": Write mode - Opens a file for writing. If the file already exists, it is truncated
(emptied). If it doesn't exist, a new file is created.
- "a": Append mode - Opens a file for appending data. The file pointer is positioned at
the end of the file. If the file doesn't exist, a new file is created.
- "x": Exclusive creation mode - Opens a file for exclusive creation. If the file already
exists, an error is raised.
- "b": Binary mode - Opens a file in binary mode. This mode is used to handle non-
text files like images or binary data.
- "t": Text mode - Opens a file in text mode (default). This mode is used to handle text
files.
- "+": Read and write mode - Opens a file for both reading and writing.
Subject Code: 4321602 Subject Name: Advanced Python Programming
- Example:
file = open("example.txt", "a")
- This mode is used to create a new file, but it raises a FileExistsError if the file
already exists.
- The file pointer is positioned at the beginning of the file.
- Example:
file = open("example.txt", "x")
Subject Code: 4321602 Subject Name: Advanced Python Programming
(c) Write a program to sort all the words in a file and put it in list. 07
Subject Code: 4321602 Subject Name: Advanced Python Programming
OR
Q.4 (a) What is file handling? List files handling operation and explain it. 03
- Files are named locations on a disk to store related information. They are used to
permanently store data in non-volatile memory ( e.g. hard disk ).
- 1) Creating a file: File handling allows you to create a new file in the file system.
The file can be empty or contain initial data.
- 2) Opening a file: Once a file is created, you can open it to perform operations on its
contents. Opening a file establishes a connection between the file and your program.
- 3) Reading from a file: You can read data from a file using various methods. For
example, you can read the entire contents of a file or read it line by line.
- 4) Writing to a file: File handling enables you to write data to a file. You can write
text, numbers, or other types of data to a file. This operation can overwrite existing
data or append data to the end of the file.
- 6) Closing a file: After reading from or writing to a file, it's important to close the
file. Closing a file releases system resources associated with it and ensures that
changes made to the file are saved.
- In python, serialization allows the developers to convert the complex object structure
into a stream of bytes that can be saved in the disk or can send through the network.
- In python three modules in the standard library allows the developer to serialize and
deserialize the objects:
(c) Write a program that inputs a text file .The program should print all of the
unique words in the file in alphabetical order. 07
") f = open(n)
l = list()
for line in f:
word =
line.split() for
element in word:
if element in l:
continue
else:
l.append(element)
l.sort()
print(l)
My File: -
File Name: - tilak.txt
Q.5 (a) Explain the use of the following turtle function with an appropriate example.
(a) turn () (b) move (). 03
- (a) turn: - This method rotates the turtle by the specified angle in degrees.
- Example: -
turtle.right(90)
- Example: -
turtle.left(90)
- (b) move (): - This method moves the turtle forward and backward by the specified
distance in Pixels.
- Example: -
turtle.forward(100)
- Example: -
turtle.backward(100)
(b)Explain the various inbuilt methods to change the direction of the turtle. 04
- There are two inbuilt methods to change the direction of the turtle in turtle in Turtle.
- Example: -
turtle.right(90)
- Example: -
turtle.left(90)
(c) Write a program to draw square, rectangle and circle using turtle. 07
APP Turtle1 PR 21.py
import turtle
t=turtle.Turtle()
t.shape("turtle")
t.fd(200)
t.lt(90)
t.fd(200)
t.lt(90)
t.fd(200)
t.lt(90)
t.fd(200)
APP Turtle2 PR 21.py
import turtle
t=turtle.Turtle()
t.shape("turtle")
t.circle(125)
APP Turtle3 PR 21.py
import turtle
t=turtle.Turtle()
t.shape("turtle")
t.fd(300)
t.lt(90)
t.fd(150)
t.lt(90)
t.fd(300)
t.lt(90)
t.fd(150)
Subject Code: 4321602 Subject Name: Advanced Python Programming
- OUTPUT: -
Subject Code: 4321602 Subject Name: Advanced Python Programming
OR
Q.5 (a) What are the various types of pen command in turtle Explain them all. 03
- In the turtle graphics module, there are several pen related commands that allow
you to control the appearance and behaviour of the drawing pen.
Example: -
turtle.color(“Blue”)
- pensize ( ): - You can increase or decrease thickness of your pen.
Example: -
turtle.pensize(10)
- seed ( ): - You can increase or decrease the speed of a turtle to move slower or
faster
Example: -
turtle.speed(15)
(b) Draw circle and triangle shapes using turtle and fill them with red color. 04
# Draw a circle
begin_fill()
circle(100)
end_fill()
# Draw a triangle
begin_fill() OUTPUT: -
for _ in range(3):
forward(100)
left(120)
end_fill()
Subject Code: 4321602 Subject Name: Advanced Python Programming
import turtle as t
t.speed(5)
#circle
t.pencolor("black")
t.fillcolor("yellow")
t.begin_fill()
t.circle(150)
t.end_fill()
t.up()
t.goto(60,180)
t.down()
#eye
t.fillcolor("white")
t.begin_fill()
t.circle(15)
t.end_fill()
#retina
t.fillcolor("black")
t.begin_fill()
t.circle(8)
t.end_fill()
t.up()
t.goto(-60,180)
t.down()
#eye
t.fillcolor("white")
t.begin_fill()
t.circle(15)
t.end_fill()
#retina
t.fillcolor("black")
t.begin_fill()
t.circle(8)
t.end_fill()
#nose
t.fillcolor("red")
Subject Code: 4321602 Subject Name: Advanced Python Programming
t.begin_fill()
t.up()
t.goto(0,130)
t.down()
t.circle(8)
t.end_fill()
t.up()
t.goto(0,60)
t.down()
#right smile
t.pensize(6)
for i in range(60):
t.lt(1)
t.fd(1)
t.up()
t.goto(0,60)
t.down()
t.rt(59)
#left smile
for i in range(60):
t.rt(1)
t.bk(1)
OUTPUT: -