Python Programming Concepts - Module Summary
1. All String Methods
Definition:
String methods are built-in functions in Python used to perform operations on strings like formatting,
searching, checking conditions, etc.
Examples:
- upper(): "hello".upper() -> "HELLO"
- lower(): "HELLO".lower() -> "hello"
- isupper(): "HELLO".isupper() -> True
- islower(): "hello".islower() -> True
- startswith()/endswith(): "hello world".startswith("hello") -> True
- join(): ', '.join(["apple", "banana"]) -> "apple, banana"
- split(): "a,b,c".split(",") -> ['a', 'b', 'c']
- strip(): " hello ".strip() -> "hello"
- rjust()/ljust()/center(): "hi".rjust(5) -> " hi"
2. os and os.path Modules
Definition:
- os module: For interacting with the operating system.
- os.path: Contains functions for manipulating file paths.
Examples:
import os
os.getcwd() # Current directory
os.path.abspath("file.txt")
os.path.exists("file.txt")
os.path.join("folder", "file.txt")
3. File Path - Absolute & Relative
Definition:
- Absolute path: Starts from root (e.g. C:\folder\file.txt)
- Relative path: Starts from current directory (e.g. .\file.txt)
- . = current directory, .. = parent directory
Examples:
os.path.abspath("myfile.txt")
os.path.relpath("C:\Windows", "C:\")
4. Copying, Moving, Deleting Folders
Definition:
Use shutil and os modules to manage files and folders.
Examples:
shutil.copy("a.txt", "backup/")
shutil.copytree("data", "data_backup")
shutil.move("old.txt", "new_folder/new.txt")
os.unlink("file.txt")
os.rmdir("folder")
shutil.rmtree("folder")
5. Logging Module
Definition:
logging is used to track events and debug the program by recording messages.
Example:
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug("This is a debug message")
6. Inheritance
Definition:
Inheritance allows a class (child) to reuse the attributes and methods of another class (parent).
Example:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
7. __init__ and __str__
__init__:
Constructor method, runs when object is created.
__str__:
Used to return a string when you print() the object.
Example:
class Student:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Student name: {self.name}"
8. Printing Objects
Definition:
By default, printing an object shows memory location. Define __str__() to control this.
Example:
class Time:
def __str__(self):
return "09:45:00"
t = Time()
print(t) # Output: 09:45:00
9. Operator Overloading & Method Overloading
Operator Overloading:
class Point:
def __init__(self, x):
self.x = x
def __add__(self, other):
return Point(self.x + other.x)
Method Overloading:
class Demo:
def greet(self, name=None):
if name:
print("Hello", name)
else:
print("Hello")