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

Python Mid 2 Solution

The document provides a series of programming tasks and explanations related to file handling and turtle graphics in Python. It covers differences between text and binary files, object serialization using the pickle module, file reading methods, and turtle movement and pen commands. Additionally, it includes example programs for adding student records, counting vowels, and drawing shapes using turtle graphics.

Uploaded by

rehanboiizzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Python Mid 2 Solution

The document provides a series of programming tasks and explanations related to file handling and turtle graphics in Python. It covers differences between text and binary files, object serialization using the pickle module, file reading methods, and turtle movement and pen commands. Additionally, it includes example programs for adding student records, counting vowels, and drawing shapes using turtle graphics.

Uploaded by

rehanboiizzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q.

No: 1 (a)

List five points on difference between text file and Binary file. (2 Marks)

No. Text File Binary File

1. Stores data in human-readable format Stores data in machine-readable binary format

2. File extension is usually .txt, .csv, etc. File extension is usually .bin, .dat, etc.

3. Ends each line with a newline character (\n) Stores data as a continuous stream of bytes

4. Can be opened in any text editor Cannot be opened directly in a text editor

5. Slower for large data processing Faster and more efficient for large data processing

Q. No: 1 (b)

Explain the object serialization. Explain load() and dump() with example. (4 Marks)

1. Object serialization is converting an object into byte stream.


2. In Python, pickle module is used for serialization.
3. dump() function writes the object into a binary file.
4. load() function reads the object from the binary file.
5. Syntax:

python
CopyEdit
import pickle
pickle.dump(obj, file)
obj = pickle.load(file)

6. Example:

python
CopyEdit
data = {"name": "John"}
with open("data.dat", "wb") as f:
pickle.dump(data, f)
with open("data.dat", "rb") as f:
print(pickle.load(f))

7. Useful for saving Python objects permanently.

Q. No: 1 (b) [Alternate]


Explain read(), readline(), and readlines() with syntax and example. (4 Marks)

1. read() reads whole file as a single string.


2. readline() reads one line at a time.
3. readlines() reads all lines and returns a list.
4. Syntax:

python
CopyEdit
file.read()
file.readline()
file.readlines()

5. Example:

python
CopyEdit
f = open("demo.txt", "r")
print(f.read())
print(f.readline())
print(f.readlines())
f.close()

6. Use read() for complete file, readline() for line-by-line, and readlines() for list
of lines.

Q. No: 1 (c)

Develop a python program to add student record like Roll number, name and marks in
binary file. (5 Marks)

python
CopyEdit
import pickle

student = {"Roll": 1, "Name": "Ravi", "Marks": 85}


with open("student.dat", "wb") as f:
pickle.dump(student, f)
print("Student record saved.")

Q. No: 1 (d)

Develop a program to count all the vowels in the file. (5 Marks)

python
CopyEdit
def count_vowels(filename):
vowels = "aeiouAEIOU"
count = 0
with open(filename, "r") as file:
for line in file:
for char in line:
if char in vowels:
count += 1
print("Total vowels:", count)

count_vowels("demo.txt")

Q. No: 1 (d) [Alternate]

Develop a Python program to demonstrate the seek() and tell() methods. (5 Marks)

python
CopyEdit
f = open("demo.txt", "r")
print("Current position:", f.tell())
f.seek(5)
print("After seeking, position:", f.tell())
data = f.read()
print("Data:", data)
f.close()

Q. No: 2 (a)

What are the different ways to move turtle to another position? (2 Marks)

1. goto(x, y) - moves turtle to given coordinates.


2. setx(x) - changes only x-coordinate.
3. sety(y) - changes only y-coordinate.
4. setpos(x, y) - similar to goto().
5. setposition(x, y) - another name for goto().

Q. No: 2 (b)

Explain various pen commands in turtle. (3 Marks)

1. penup() - lifts pen, turtle moves without drawing.


2. pendown() - puts pen down to draw.
3. pensize() - changes width of the pen.
4. pencolor() - sets pen color.
5. pen() - sets multiple pen attributes.
6. Example:

python
CopyEdit
turtle.pencolor("red")
turtle.pensize(5)

Q. No: 2 (b) [Alternate]


Explain various turtle screen method. (3 Marks)

1. setup(width, height) - sets window size.


2. bgcolor(color) - sets background color.
3. title("Title") - sets title of turtle window.
4. clearscreen() - clears everything.
5. exitonclick() - closes window on click.
6. Example:

python
CopyEdit
screen = turtle.Screen()
screen.bgcolor("blue")
screen.title("Turtle Window")

Q. No: 2 (c)

Develop a program to draw square, rectangle and circle using turtle. (4 Marks)

python
CopyEdit
import turtle

t = turtle.Turtle()
# Square
for _ in range(4):
t.forward(100)
t.right(90)
# Rectangle
t.penup(); t.goto(150, 0); t.pendown()
for _ in range(2):
t.forward(150)
t.right(90)
t.forward(100)
t.right(90)
# Circle
t.penup(); t.goto(-150, 0); t.pendown()
t.circle(50)

turtle.done()

Q. No: 2 (d)

Develop a program to draw star, triangle and fill them with red color. (5 Marks)

python
CopyEdit
import turtle

t = turtle.Turtle()
t.color("red")
t.begin_fill()
# Triangle
for _ in range(3):
t.forward(100)
t.left(120)
t.end_fill()

t.penup(); t.goto(150, 0); t.pendown()


t.begin_fill()
# Star
for _ in range(5):
t.forward(100)
t.right(144)
t.end_fill()

turtle.done()

Q. No: 2 (d) [Alternate]

Develop a program to draw any shape using for loop. (5 Marks)

python
CopyEdit
import turtle

t = turtle.Turtle()
sides = 6 # Hexagon
angle = 360 / sides
for _ in range(sides):
t.forward(100)
t.left(angle)

turtle.done()

You might also like