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

Python Programming Assignment

This document is a Python programming assignment consisting of 15 questions that cover various programming concepts including data types, arithmetic operations, control structures, functions, data structures, file handling, error handling, classes, and modules. Each question requires the implementation of a specific task, such as creating functions, manipulating strings, and handling exceptions. Students are instructed to write clean, well-commented code and test their programs before submission.

Uploaded by

kabirkaranwal456
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)
5 views3 pages

Python Programming Assignment

This document is a Python programming assignment consisting of 15 questions that cover various programming concepts including data types, arithmetic operations, control structures, functions, data structures, file handling, error handling, classes, and modules. Each question requires the implementation of a specific task, such as creating functions, manipulating strings, and handling exceptions. Students are instructed to write clean, well-commented code and test their programs before submission.

Uploaded by

kabirkaranwal456
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

PYTHON PROGRAMMING ASSIGNMENT

Instructions
• Attempt all 15 questions. Each question carries equal marks.
• Write clean, well-commented code with proper variable names.
• Test your programs with multiple inputs before submitting.

Q1

Write a Python program that declares variables of the following data types: integer, float, string, and Boolean.
Print each variable along with its type using the type() function.

Q2

Write a Python program that takes two numbers as input and performs all arithmetic operations: addition,
subtraction, multiplication, division, floor division, modulus, and exponentiation. Display the result of each
operation.

Q3

Write a Python program that accepts a student's marks (0–100) and prints the corresponding grade using if-elif-
else:
A: 90–100 | B: 75–89 | C: 60–74 | D: 45–59 | F: below 45

Q4

Write a Python program using a for loop to print the multiplication table of a given number n (from 1 to 10).
The output should be formatted as: n x i = result.

Q5

Write a Python program using a while loop to find and print all prime numbers between 1 and 50. A prime
number is only divisible by 1 and itself.

Q6

Python Programming Assignment | Page 1 of 3


Create a Python function called calculator(a, b, operation) that accepts two numbers and an operation string
('add', 'subtract', 'multiply', 'divide'). Return the result, and handle division by zero with an appropriate message.

Q7

Write a Python program that:


(a) Creates a list of 10 integers.
(b) Finds the maximum, minimum, and average.
(c) Sorts the list in ascending and descending order.
(d) Removes all duplicate elements.

Q8

Write a Python program that:


(a) Creates a tuple of 5 fruits.
(b) Creates two sets of numbers and finds union, intersection, and difference.
(c) Demonstrates that a tuple is immutable by attempting to modify it (catch the error).

Q9

Write a Python program to maintain a student records dictionary where:


(a) Each student name maps to their marks.
(b) Add a new student, update a student's marks, and delete a student.
(c) Print all students whose marks are above 70.

Q10 Topic: String Manipulation

Write a Python program that accepts a sentence from the user and:
(a) Counts the number of vowels and consonants.
(b) Reverses the string.
(c) Checks whether it is a palindrome (ignoring spaces and case).
(d) Replaces all spaces with underscores.

Q11

Write a Python program that:


(a) Creates a text file '[Link]' and writes 5 student names.
(b) Reads and displays the file contents.
(c) Appends 2 more student names.
(d) Counts the total number of lines in the file.

Python Programming Assignment | Page 2 of 3


Q12

Write a Python program that:


(a) Asks the user to enter two numbers and divide them.
(b) Handles ZeroDivisionError, ValueError (non-numeric input), and any unexpected exception.
(c) Uses finally block to print 'Operation complete.' regardless of the outcome.

Q13

Create a Python class BankAccount with:


Attributes: account_number, owner_name, balance
Methods: deposit(amount), withdraw(amount), get_balance(), __str__()
Ensure withdrawal raises an error if balance is insufficient. Create two objects and demonstrate all methods.

Q14

Write a Python program that uses the random module to:


(a) Generate a random number guessing game (1–100) where the user guesses until correct.
(b) Track the number of attempts.
(c) Give hints: 'Too high' or 'Too low' after each wrong guess.
(d) Import and use the math module to compute the square root of the final answer.

Q15

Write a Python program that:


(a) Uses list comprehension to generate a list of squares of even numbers from 1 to 20.
(b) Uses a lambda function with filter() to extract numbers divisible by 3 from a list.
(c) Uses a lambda function with map() to convert a list of Celsius temperatures to Fahrenheit.
(d) Sorts a list of tuples (name, age) by age using a lambda as key.

Python Programming Assignment | Page 3 of 3

You might also like