This program demonstrates basic list operations in Python, including append, insert, extend, pop, sort, and index.
It builds a list step by step and shows the results after each operation.
The program performs the following steps:
- Create an empty list called
my_list. - Append the elements
10, 20, 30, 40to the list. - Insert the value
15at the second position (index1). - Extend the list with another list:
[50, 60, 70]. - Remove the last element from the list using
pop(). - Sort the list in ascending order.
- Find and print the index of the value
30.
This program demonstrates the use of control flows (if, else, try-except) and functions in Python.
It calculates the final price of an item based on the discount percentage provided by the user.
-
Defines a function
calculate_discount(price, discount_percent):- If the discount is 20% or higher, the discount is applied.
- If the discount is less than 20%, the original price is returned.
-
Uses a try-except block to handle invalid user input (e.g., non-numeric values).
-
Provides clear user feedback:
- Displays the applied discount percentage and the final price (if applicable).
- Informs the user if no discount was applied.
This program demonstrates file handling in Python by reading from a file, modifying its contents, and writing the modified version to a new file.
It also includes error handling to manage cases where the file doesn’t exist or can’t be accessed.
- Prompts the user to enter a filename to read.
- Attempts to open and read the file content safely.
- Modifies the file’s content (e.g., converts all text to uppercase).
- Writes the modified content to a new file named
modified_<original_filename>. - Handles common file-related errors:
FileNotFoundError– when the file doesn’t exist.PermissionError– when the user lacks read/write permissions.- Any other unexpected error is caught and displayed gracefully.
This week explores Object-Oriented Programming (OOP) concepts such as classes, objects, constructors, encapsulation, inheritance, and polymorphism.
File: smartphone_oop_inheritance.py
This program defines a Smartphone class that inherits from a parent Device class.
It demonstrates encapsulation using private attributes and inheritance through class extension.
Device(Parent Class) – stores general device info (brand, model).Smartphone(Child Class) – adds attributes likestorageandbattery.- Implements encapsulation using private attributes and getters/setters.
- Demonstrates real-world behavior with methods like
charge()andspecs(). - Uses a constructor (
__init__) to initialize objects with unique values.
File: vehicle_polymorphism_demo.py
This program demonstrates polymorphism using different vehicle classes that all share the same method name (move()), but perform different actions.
- Defines classes
Car,Plane, andBoat. - Each class implements its own version of
move(). - Demonstrates polymorphism by calling
move()on different objects in a loop. - Showcases flexibility and extensibility of OOP design.
This program demonstrates practical use of Python networking and file handling through an image downloader built using the requests library.
It aligns with the Ubuntu principles of mindful connection, community, and respect while teaching robust error handling and resource management.
Create a command-line tool that:
- Downloads images from user-provided URLs.
- Saves them neatly into a
Fetched_Imagesfolder. - Handles network errors gracefully.
- Promotes community values by writing clean, respectful, and open code.