Assignment 1: Python Fundamentals and OOP

Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due

Assignment 1: Python Fundamentals and OOP

Course Title: Basics of Software Coding Based on Computational Thinking

Course code: 011297-013

Submission Date: _____30 April 2025_______________

Instructions:

· Answer all questions.

· Submit a .py file or Jupyter Notebook (as applicable).

· Include comments to explain your logic wherever needed.

· Use meaningful variable/function/class names.

· Create a well-defined report that explains your implementation and explanation along with a .py file or Jupiter Notebook. Submit your PDF or Word file.

Section A: Introduction to Python (10 Marks)

1. Write a short paragraph:

o What can Python do in real-world applications?

o Why is Python considered beginner-friendly?

2. List 3 advantages of Python compared to other programming languages.

3. Explain the importance of indentation in Python with an example.

4. Describe how Python is installed. Mention one IDE used to write Python programs.

5. Create a Python file and write a “Hello, Python!” program using the print() function.


Section B: Python Basics (15 Marks)

6. Write a Python program that uses:

o Single-line comments

o Multi-line comments

7. Define a list, tuple, dictionary, and set in Python. Print each with sample data.

8. Take user input for two numbers and print:

o Their sum

o Their product

o Which one is larger

9. Demonstrate string slicing and string functions like upper(), lower(), replace(), and split().

10. Write a program that prints data with formatted output using f-strings.

11. Write a program that demonstrates the use of arithmetic, logical, and comparison operators.


Section C: Python Program Flow (15 Marks)

12. Create a program that checks if the input number is positive, negative, or zero using if-else.

13. Write a Python program using a while loop to print numbers from 1 to 10.

14. Use a for loop and the range() function to print the square of the first 10 natural numbers.

15. Demonstrate the use of break and continue inside a loop with clear comments.

16. Use assert to ensure a number entered by a user is greater than zero.

17. Write a combined program that:

· Takes 5 inputs from the user and stores them in a list

· Loops through the list and prints the square of even numbers only


Section D: Functions and Modules (20 Marks)

18. Define a function that takes a name as input and returns a greeting.

19. Create a function that takes variable arguments (*args) and returns their average.

20. Write a function to calculate factorial using recursion. Add a docstring to explain it.

21. Use a lambda function with map() to double the elements in a list.

22. Create a separate Python module (mymath.py) that includes functions for addition, subtraction, and multiplication. Import and use it in another Python file.

23. Use at least two standard Python modules (like math, random, datetime) in small examples.

24. Write a real-world mini problem-solving function like:

· Temperature converter (Celsius to Fahrenheit)

· Simple calculator (with if-elif-else)

· Random password generator


Section E: Exception Handling (15 Marks)

25. Write a program that divides two numbers and handles a ZeroDivisionError.

26. Handle multiple exceptions: ValueError and TypeError with one example each.

27. Use try-except-finally in a code block and explain each part using comments.

28. Create a custom exception class NegativeNumberError and raise it if the user enters a negative number.

29. Demonstrate how to use exception handling when reading a file that might not exist.


Section F: Classes and Object-Oriented Programming (25 Marks)

30. Define a class Student with attributes: name, roll number, and grade. Create two objects and print their data.

31. Add instance methods to the Student class to update and display student details.

32. Create a class with a class variable to count the number of objects created.

33. Demonstrate inheritance with classes Vehicle and Car.

34. Show method overriding by defining a speak() method in both Animal and Dog classes.

35. Design a complete example:

· Create a class BankAccount with methods for deposit, withdraw, and check balance.

· Raise custom exceptions for insufficient balance and invalid input.

35. Create a custom exception class and demonstrate polymorphism with two different class objects calling the same method with different outputs.

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注