0% found this document useful (0 votes)
7 views3 pages

Python Projects: Quizzes, Backup, and More

The document contains multiple Python projects including generating random quiz files, backing up folders into ZIP files, renaming files with American-style dates to European-style, and drawing shapes using the Turtle graphics library. It also explains key concepts in Python such as the __init__() method, __str__() method, and operator overloading with examples. Each project is implemented with code snippets demonstrating their functionality.

Uploaded by

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

Python Projects: Quizzes, Backup, and More

The document contains multiple Python projects including generating random quiz files, backing up folders into ZIP files, renaming files with American-style dates to European-style, and drawing shapes using the Turtle graphics library. It also explains key concepts in Python such as the __init__() method, __str__() method, and operator overloading with examples. Each project is implemented with code snippets demonstrating their functionality.

Uploaded by

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

Q1.

Project: Generating Random Quiz Files

import random

capitals = {
'Andhra Pradesh': 'Amaravati',
'Bihar': 'Patna',
'Karnataka': 'Bengaluru',
'Maharashtra': 'Mumbai',
'Tamil Nadu': 'Chennai',
'West Bengal': 'Kolkata'
}

for quiz_num in range(3):


quiz_file = open(f'quiz_{quiz_num + 1}.txt', 'w')
answer_key_file = open(f'quiz_answers_{quiz_num + 1}.txt', 'w')

states = list([Link]())
[Link](states)

for question_num, state in enumerate(states):


correct_answer = capitals[state]
wrong_answers = list([Link]())
wrong_answers.remove(correct_answer)
wrong_choices = [Link](wrong_answers, 3)
answer_options = wrong_choices + [correct_answer]
[Link](answer_options)

quiz_file.write(f'{question_num + 1}. What is the capital of {state}?\n')


for i, option in enumerate(answer_options):
quiz_file.write(f" {'ABCD'[i]}. {option}\n")
quiz_file.write('\n')

correct_option = 'ABCD'[answer_options.index(correct_answer)]
answer_key_file.write(f'{question_num + 1}. {correct_option}\n')

quiz_file.close()
answer_key_file.close()

Q2. Project: Backing Up a Folder into a ZIP File

import zipfile, os

def backup_to_zip(folder):
folder = [Link](folder)
zip_filename = [Link](folder) + '_backup.zip'
print(f'Creating {zip_filename}...')

with [Link](zip_filename, 'w') as backup_zip:


for foldername, subfolders, filenames in [Link](folder):
backup_zip.write(foldername)
for filename in filenames:
file_path = [Link](foldername, filename)
backup_zip.write(file_path)

print('Backup completed!')
Q3. Renaming Files with American-Style Dates to European-Style

import os, re

date_pattern = [Link](r"""^(.*?)((0|1)?\d)-((0|1|2|3)?\d)-((19|20)\d\d)(.*?)$""", [Link])

def rename_dates(directory):
for filename in [Link](directory):
mo = date_pattern.search(filename)
if not mo:
continue

before, month, day, year, after = [Link](1), [Link](2), [Link](4), [Link](6), [Link](8)
euro_filename = before + day + '-' + month + '-' + year + after
abs_working_dir = [Link](directory)
old_file = [Link](abs_working_dir, filename)
new_file = [Link](abs_working_dir, euro_filename)
[Link](old_file, new_file)
print(f'Renamed: {filename} -> {euro_filename}')

Q4. Drawing Rectangle and Circle with Turtle

import turtle

class Rectangle:
def __init__(self, width, height):
[Link] = width
[Link] = height

class Circle:
def __init__(self, radius):
[Link] = radius

def draw_rect(t, rect):


for _ in range(2):
[Link]([Link])
[Link](90)
[Link]([Link])
[Link](90)

def draw_circle(t, circle):


[Link]([Link])

Q5. Explain with Examples

i. __init__():
class Person:
def __init__(self, name):
[Link] = name
p = Person("Alice")
print([Link])

ii. __str__():
class Person:
def __init__(self, name):
[Link] = name
def __str__(self):
return f'Person: {[Link]}'
p = Person("Bob")
print(p)

iii. Operator Overloading (__add__()):


class Vector:
def __init__(self, x, y):
self.x = x
self.y = y

def __add__(self, other):


return Vector(self.x + other.x, self.y + other.y)

def __str__(self):
return f'({self.x}, {self.y})'

v1 = Vector(2, 3)
v2 = Vector(1, 4)
print(v1 + v2)

You might also like