Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
92 views

Programming With Python

Uploaded by

artibhardwaj8419
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Programming With Python

Uploaded by

artibhardwaj8419
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PROGRAMMING WITH PYTHON

GE-GENERAL ELECTIVE
(Undergraduate courses)
University of Delhi

Practicals / Activities

NAME - Mohd Akram


ROLL NO. - 543
COURSE - HINDI(HONS)
SEMESTER - FIRST
Acknowledgement

I would want to convey my heartfelt gratitude to karan gupta sir, my mentor, for her invaluable
advice and assistance in completing my project. He was there to assist me every step of the way,
and her motivation is what enabled me to accomplish my task effectively. I would also like to
thank all of the other supporting personnel who assisted me by supplying the equipment that was
essential and vital, without which I would not have been able to perform efficiently on this project.

I am grateful to the college administration for providing me with such a significant chance. I
guarantee that this project was created entirely by me.
Practical 1:
WAP to find the roots of a quadratic equation
Output:
2. WAP 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
This program may be done using functions
This program defines three functions:

1.is_prime(num): Checks whether a number is prime or not.


2.generate_primes_till_n(n): Generates all prime numbers till a given number 'n'.
3.generate_first_n_primes(n): Generates the first 'n' prime numbers.

Upon running this program, it prompts the user to enter a number 'n', and then it performs the
tasks of checking if the number is prime, generating all prime numbers till 'n', and generating
the first 'n' prime numbers.
3. WAP to create a pyramid of the character ‘*’ and a reverse pyramid
This program uses a function print_pyramid(rows) that takes the number of rows
for the pyramid as input. It first prints the upper part of the pyramid with increasing '*'
count per row and then prints the lower part of the pyramid in reverse order.

You can change the rows variable to adjust the size of the pyramid. For instance,
setting rows = 5 will create a pyramid of 5 rows (and the same number of rows in the
reverse pyramid). Adjust the value of rows as desired to change the pyramid's size.
4.WAP that accepts a character and performs the following:
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)
This program defines a function character_analysis(char) that analyzes the given
character and performs the specified tasks:

a. It checks whether the character is a letter (uppercase or lowercase), a numeric digit,


or a special character.

b. If the character is a letter, it identifies whether it is uppercase or lowercase.

c. If the character is a numeric digit, it prints its name in text.


5.WAP 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 occurrence of a character from a string.
d. Remove all occurrences of a character from a string.
This program defines four functions to perform the specified operations on a given string:

a. find_frequency(string, char): Finds the frequency of a character in a string.


b. replace_character(string, old_char, new_char) : Replaces a character by another character in a string.
c. remove_first_occurrence(string, char) : Removes the first occurrence of a character from a string.
d. remove_all_occurrences(string, char) : Removes all occurrences of a character from a string.
6. WAP to swap the first n characters of two strings.
7. Write a function that accepts two strings and returns the indices of all the occurrences 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.
8. WAP 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
Using 'for' loop:
Using list comprehension:

This program includes two functions:

cubes_of_even_integers_for_loop(input_list): Generates a list of cubes of even integers from the input list using a 'for'
loop.
cubes_of_even_integers_list_comprehension(input_list): Generates a list of cubes of even integers from the input list
using list comprehension.
9. WAP 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 reverse order.
d. Copy even lines of the file to a file named ‘File1’ and odd lines to another file named ‘File2’.
This program performs the following operations on a given file:
a. Counts the total number of characters, words, and lines in the file.
b. Calculates the frequency of each character in the file.
c. Prints the words in reverse order.
d. Copies even lines to 'File1.txt' and odd lines to 'File2.txt'.
10. Write a function that prints a dictionary where the keys are numbers
between 1 and 5 and the values are cubes of the keys.
11. Consider a tuple t1=(1, 2, 5, 7, 9, 2, 4, 6, 8, 10). WAP 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
This program performs the following operations on the given tuple t1:

a. Prints half the values of the tuple in one line and the other half in the next line.
b. Prints another tuple whose values are even numbers in the given tuple.
c. Concatenates a tuple t2=(11,13,15) with t1.

d. Returns the maximum and minimum values from the concatenated tuple.
12. WAP 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.
This program defines a function check_name(name) that iterates through each character

of the entered name. If it encounters any character that is not an alphabet or space, it

raises a ValueError with an appropriate error message.

The try-except block handles this potential exception. If the entered name contains

only alphabets and spaces, it prints the validated name. Otherwise, it catches the

ValueError and prints the corresponding error message.


THANKYOU

You might also like