0% found this document useful (0 votes)
43 views16 pages

Python Programming Practical File

Uploaded by

Gunjan Mittal
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)
43 views16 pages

Python Programming Practical File

Uploaded by

Gunjan Mittal
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

College of Vocational Studies

(University Of Delhi)
Department of Computer Science
Practical File
Programming Using Python

Name- Kavyansh Bishnoi


Roll No.- 25/CS/26
Course- BSc. (Hons.) Computer Science
Semester- I
Subject- Programming Using Python
Enrollment no.- 25CVSTBSCS000025
Examination Roll no.- 25013570002
Submitted to- Mrs. Parul Chachra
Q1. Write a Program to find the roots of
Quadratic Equation.
Ans.- Code:

Output:
Q2. Write a program to accept a number ‘n’ and
a. Check if ‘n’ is prime
b. Generate all prime numbers till ‘n’
c. Generate first ‘n’ prime numbers
Ans.- Code:

Output:
Q3. Write a program to create a pyramid of
the character ‘*’ and a reverse pyramid.
Ans.- Code:

Output:
Q4. Write a program that accepts a
character and performs the folowing:
a. print whether the character is a letter or
numeric digit or a special character.
b. if the character is a letter, print whether the
letter is uppercase or lowercase.
c. if the character is a numeric digit , prints its
name in text (e.g., if input is 9, output is NINE).

Ans.- Code:

Output:
Q5. Write a program to perform the following
operations on a string
a. Find the frequency of a character in a string.
b. Replace a character by another character in a
string.
c. Remove the first occcurence of a character
from a string.
d. Remove all occurences of a character from a
string.
Ans.- Code:
Output:

Q6. Write a program to swap the first n


characters of two strings.
Ans.- Code:

Output:
Q7. Write a function that accepts two strings
and returns the indices of all the occurences of
the second string in the first string as a list. If
the second string is not present in the first
string then it should return -1.
Ans.- Code:

Output:

Q8. Write a program to create a list of the


cubes of only the even integers appearing in
the input list (may have elements of other
types also) using the following:
a. ‘for’ loop
b. list comprehension
Ans.- Code:
a.

Output:

b.

Output:
Q9. Write a program to read a file and
a. Print the total number of characters, words
and lines in the file.
b. Calculate the frequency of each character
in the file. Use a variable of dictionary type to
maintain the count.
c. Print the words in a reverse order.
d. Copy even lines of the file named ‘File1’
and odd lines to another file named ‘File2’.
Ans.- Code:
Output:

Q10. Write a function that prints a dictionary


where the keys are numbers between 1 and
5 and the values are cubes of the keys.
Ans.- Code:

Output:
Q11. Consider a tuple t1=
(1,2,5,7,9,2,4,6,8,10). Write a program to
perform following operations:
a. Print half the values of the tuple in one line
and the other half in the next line.
b. Print another tuple whose values are even
numbers in the given tuple.
c. Concatenate a tuple t2=(11,13,15) with t1
d. Return maximum and minimum value from
this tuple.
Ans.- Code:

Output:
Q12 Define a class Employee that stores information about
employees in the company. The class should contain the
following:
i) data members- count (to keep a record of all the objects
being created for the class) and for every employee: an
employee number, Name, Dept, Basic, DA and HRA.
ii) function members:
a. __init__ method to initialize and/or update the members.
Add statements to ensure that the program is terminated if any
of Basic, DA and HRA is set to a negative value.
b. function salary, that returns salary as the sum of Basic, DA
and HRA.
c. _del_ function to decrease the number of objects created for
the class.
d. _str_ function to display the details of an employee along
with the salary of an employee in a proper format.

Ans.- Code:
Output:

Q13. Write a program to define a class


“2DPoint” with coordinates x and y as
attributes. Create relevant methods and print
the objects. Also define a method distance to
calculate the distance between any two point
objects.
Ans.- Code:
Output:

Q14. Inherit the above class to create a


“3DPoint” with additional attribute z.
Override the method defined in “2DPoint”
class, to calculate distance between two
points of the “3DPoint” class.
Ans.- Code:

Output:
Q15. Write a program to accept a name from
a user. Raise and handle appropriate
exception(s) if the text entered by the user
contains digits and/or special characters.
Ans.- Code:

Output:

You might also like