0% found this document useful (0 votes)
3 views

python code

The document contains a series of programming exercises involving Python classes and functions. It includes tasks such as reversing a string, calculating the area and perimeter of a circle, creating a Teacher class with private attributes, sorting a list of dictionaries, and file handling operations. Additionally, it covers generating text files, printing patterns, and reading CSV file headers.

Uploaded by

marwasamad2410
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)
3 views

python code

The document contains a series of programming exercises involving Python classes and functions. It includes tasks such as reversing a string, calculating the area and perimeter of a circle, creating a Teacher class with private attributes, sorting a list of dictionaries, and file handling operations. Additionally, it covers generating text files, printing patterns, and reading CSV file headers.

Uploaded by

marwasamad2410
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
You are on page 1/ 4

Quiz # 2

1. Create a class to reverse a string word by word.


Example: Python to nohtyp
s = "i like this program very much"
words = s.split(' ')
string =[]
for word in words:
string.insert(0, word)

print("Reversed String:")
print(" ".join(string))

2. Create a class named MyCircle built by a radius and two methods which will
compute the area and the perimeter of a circle.
class MyCircle():

def radius(self,radius):
r = radius
def area(self,r):
area = 3.142*r*r
print("Area of the circle is: ",area)
def peri(self,r):
peri = 2*3.14*r
print("Perimeter of the circle is: ",peri)

c = Circle()
radius = int(input("Enter the radius of circle: "))
c.area(radius)
c.peri(radius)

3. Create a class Teacher with name, age, and salary attributes, where salary
must be a private attribute that cannot be accessed outside the class.

class Teacher():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.__salary = salary

def show_details(self):
print("Name:", self.name)
print("Age:", self.age)
print("Salary: ", self.__salary)
teacher = Teacher("", 45, 25000)
teacher.show_details()

4. Write a program to sort a list of dictionaries using Lambda Function.


models = [{'make':'samsung', 'model':A5, 'color':'Black'},
{'make':'iphone', 'model':'13', 'color':'white'}, {'make':vivo', 'model': 9,
'color':'gold'}]
print("Original list of dictionaries :")
print(models)
sorted_models = sorted(models, key = lambda x: x['color'])
print("\nSorting the List of dictionaries :")
print(sorted_models)
5. Write a program to assess if a file is closed or not and if It’s not closed then
write program that takes a text file as input and returns the number of
words of a given text file.

6. Create program to generate 26 text files named A.txt, B.txt, and so on up to


Z.txt.

import string, os
if not os.path.exists("letters"):
os.makedirs("letters")
for letter in string.ascii_uppercase:
with open(letter + ".txt", "w") as f:
f.writelines(letter)
7. Write to program to print buzz on multiple of input number and
take the length of the series as input too.
Example:
Inputed Series : 15
Number:5
1,2,3,4,buzz,6,7,8,9,buzz,11,12,13,14,buzz

8. Write code to print digit pattern with stars.


Example: 431
****
***
*
9. Write a code to get column names from csv file.
from csv import reader
with open('demo.csv', 'r') as readObj:
csvReader = reader(readObj)
header = next(csvReader)
print(header)

10. Write a program to use four pillars of Object-oriented


programming in a single program.

11. Write a code to read content from one file and write it into
another file.

You might also like