Here’s a list of Python programs covering all the topics we discussed, from basic OOP concepts to advanced features like inheritance, overloading, and polymorphism. ๐นย 1๏ธโฃ Classes and Objects ๐ Program: Create a class Car with attributes and methods. class Car: def __init__(self, brand, model): self.brand = brand self.model = model def display_info(self): print(f”Car: {self.brand}…
Python supports Object-Oriented Programming (OOP), which allows structuring programs using classes, objects, attributes, and principles like inheritance, overloading, overriding, and polymorphism. ๐น 1๏ธโฃ Classes and Objects โค What is a Class? A class is a blueprint for creating objects. It defines attributes (variables) and methods (functions) that describe the behavior of objects. โค What is…
The os module in Python provides powerful functions for file and directory operations, including creating, renaming, deleting, and checking file properties. ๐น Importing the os Module import os ๐น 1๏ธโฃ Checking if a File Exists (os.path.exists()) Before performing file operations, it’s a good practice to check if the file exists. file_path = “example.txt” if os.path.exists(file_path):…
Python provides built-in methods to read from and write to files using file objects. The most commonly used methods are: read() โ Reads file content. write() โ Writes data to a file. ๐น Writing to a File Using write() You must open the file in write (‘w’) or append (‘a’) mode. The method returns the…
When a file is opened using Pythonโs open() function, it returns a file object. This object contains several attributes that provide information about the file and its state. ๐น File Object Attributes 1๏ธโฃ name โ File Name Returns the name of the file. file = open(“example.txt”, “w”) print(file.name) # Output: example.txt file.close() 2๏ธโฃ mode โ…
Python allows you to control file access using different file modes in the open() function. These modes determine whether a file is opened for reading, writing, appending, or binary access. ๐น File Access Modes in Python Mode Description ‘r’ Read (default), raises an error if the file does not exist. ‘w’ Write, creates a new…
The open() function in Python is used to create file objects, which allow us to read, write, or manipulate files. ๐น Syntax of open() file_object = open(“filename”, mode) “filename”: Name of the file (with extension, e.g., “data.txt”). mode: Specifies how the file should be opened (read, write, append, etc.). ๐น File Open Modes Mode Description…
The input() function in Python is used to take user input from the keyboard during program execution. It always returns a string, so type conversion may be needed. ๐น Basic Syntax user_input = input(“Enter something: “) print(“You entered:”, user_input) ๐ Example Output Enter something: Hello, Python! You entered: Hello, Python! ๐น Taking Integer Input Since…
Python provides powerful tools for file handling and managing standard output (STDOUT) using the print() function. ๐น File Handling in Python Python allows us to read, write, and manipulate files using the built-in open() function. ๐ Syntax of open() file = open(“filename”, mode) Mode Description ‘r’ Read (default), raises an error if the file doesnโt…
The dir() function is a built-in Python function used to list all attributes and methods available in an object, module, or class. It helps developers explore and debug Python objects. ๐น Syntax dir(object) # Returns a list of attributes and methods of the object If no object is passed, dir() returns the list of all…