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…
🔹 What is a Python Module? A module in Python is a file containing Python functions, classes, and variables that can be imported and reused in other programs. It helps organize code logically. Reduces redundancy and improves code reusability. A module is simply a Python file (.py) that can be imported into another script. ✅…