- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python String
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Built-in Functions
- Python Lambda Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python List Comprehension
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python JSON
- Python OOPs Concepts
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
python3
Python Loop MCQ
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Python Loops MCQ Quiz (Random 10 from 50)
Python If Conditions MCQ
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Python IF Conditions MCQ Quiz (Random 10 from 50)
Python Datatypes MCQ
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Python Datatypes MCQ Quiz (Random 10 Questions)
Python Variables MCQ
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Python Variables MCQ Quiz (Random 10 of 50 Questions)
Python Generators
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Python Generators Introduction
A Generator in Python is a special type of function that yields values one at a time, instead of returning them all at once.
Generators are memory-efficient and used for working with large datasets, streams, or infinite sequences.
Why Generators?
| Feature | Why it matters |
|---|---|
| Lazy evaluation | Values are produced on demand, not all at once |
| Memory efficient | Doesn’t store entire data in memory |
| Improves performance | Faster for large datasets / loops |
| Infinite sequences | Possible to generate endless values |
Regular Function vs Generator
Normal Function
def numbers():
return [1,2,3]
Generator Function
def numbers():
yield 1
yield 2
yield 3
The keyword yield makes it a generator.
How to Use a Generator
def numbers():
yield 1
yield 2
yield 3
gen = numbers()
print(next(gen)) # 1
print(next(gen)) # 2
print(next(gen)) # 3
Generator with Loop
def countdown(n):
while n > 0:
yield n
n -= 1
for num in countdown(5):
print(num)
Difference Between return and yield
| Keyword | Behavior |
|---|---|
| return | Exits function and sends a single value |
| yield | Pauses function, saves state, returns value, resumes on next call |
Generator Expression
List comprehension
squares = [x*x for x in range(5)]
Generator expression
squares = (x*x for x in range(5))
print(squares) # <generator object>
print(next(squares)) # 0, then 1, 4, 9...
Practical Examples
Generate Even Numbers
def even_numbers(limit):
for n in range(limit+1):
if n % 2 == 0:
yield n
for i in even_numbers(10):
print(i)
Read Large Files Line by Line (Memory Friendly)
def read_file(filepath):
with open(filepath) as f:
for line in f:
yield line
for line in read_file("large_data.txt"):
print(line)
Infinite Generator (Be careful!)
def infinite_counter():
num = 1
while True:
yield num
num += 1
gen = infinite_counter()
print(next(gen))
print(next(gen))
Fibonacci Series Generator
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
for num in fibonacci(10):
print(num)
Generator to Filter Data
def filter_positive(numbers):
for num in numbers:
if num > 0:
yield num
nums = [-4, 3, 5, -1, 9]
for n in filter_positive(nums):
print(n)
Python Decorators
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Decorator Definition
A decorator in Python is a function that modifies the behavior of another function, without changing its code.
Think of a decorator as adding a layer of functionality before and/or after the original function runs.
Why Use Decorators?
| Purpose | Example Use Case |
|---|---|
| Add functionality without editing original code | Logging, Authorization |
| Reuse common logic | Validation |
| Track performance | Execution time calculator |
| Restrict access | Admin/user roles |
Basic Decorator Structure
def decorator_function(original_function):
def wrapper_function():
# Code before
original_function()
# Code after
return wrapper_function
Applying a Decorator
@decorator_function
def display():
print("Display function executed")
The @decorator_function applies extra behavior to the display() function.
Basic Example
def my_decorator(func):
def wrapper():
print("Before function runs")
func()
print("After function runs")
return wrapper
@my_decorator
def greet():
print("Hello!")
greet()
Output:
Before function runs
Hello!
After function runs
Decorator With Arguments
def decorator(func):
def wrapper(*args, **kwargs):
print("Arguments:", args, kwargs)
return func(*args, **kwargs)
return wrapper
@decorator
def add(a, b):
print("Sum =", a + b)
add(5, 3)
Decorator Returning a Value
def decorator(func):
def wrapper(*args):
result = func(*args)
print("Function returned:", result)
return result
return wrapper
@decorator
def multiply(x, y):
return x * y
multiply(4, 5)
Real-World Example – Logging
def logger(func):
def wrapper(*args, **kwargs):
print(f"Running function: {func.__name__}")
return func(*args, **kwargs)
return wrapper
@logger
def show():
print("Inside show function")
show()
Example – Execution Time Calculator
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"Execution Time: {end-start:.5f} seconds")
return result
return wrapper
@timer
def slow_function():
time.sleep(2)
slow_function()
Authentication / Access Control
def require_admin(func):
def wrapper(user):
if user != "admin":
print("Access Denied!")
else:
return func(user)
return wrapper
@require_admin
def delete_data(user):
print("Data deleted successfully!")
delete_data("guest")
delete_data("admin")
Decorators with Parameters (Advanced)
Sometimes we need the decorator to accept its own argument:
def repeat(num):
def decorator(func):
def wrapper():
for _ in range(num):
func()
return wrapper
return decorator
@repeat(3)
def hello():
print("Hello")
hello()
Output
Hello
Hello
Hello
Nesting Multiple Decorators
def bold(func):
def wrapper():
print("<b>", end="")
func()
print("</b>")
return wrapper
def italic(func):
def wrapper():
print("<i>", end="")
func()
print("</i>")
return wrapper
@bold
@italic
def text():
print("Decorated Text", end="")
text()
Python OS Module
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Introduction
The os module in Python provides functions to interact with the operating system. It allows you to work with files, folders, system paths, and environment variables and execute system commands.
Why Use the os Module?
| Task you want to do | os Module Feature |
|---|---|
| Get current directory | os.getcwd() |
| Change directory | os.chdir() |
| Create/Remove folders | os.mkdir() / os.rmdir() |
| List files and folders | os.listdir() |
| Work with environment variables | os.environ |
| Join file paths safely | os.path.join() |
| Delete a file | os.remove() |
| Run system commands | os.system() |
Importing OS Module
import os
Directory Operations
Get the current working directory
import os
print(os.getcwd())
Change working directory
os.chdir("C:/Users")
List all files and folders
print(os.listdir())
Create and Delete Directories
Create a new folder
os.mkdir("myfolder")
Remove a folder (only if empty)
os.rmdir("myfolder")
Create multiple nested directories
os.makedirs("main/subfolder/new")
Remove nested directories
os.removedirs("main/subfolder/new")
File Handling
Delete a file
os.remove("sample.txt")
Rename a file
os.rename("oldname.txt", "newname.txt")
Path Operations (os.path)
| Function | Description |
|---|---|
os.path.join() | Safely join folder and file names |
os.path.exists() | Check if path/file exists |
os.path.isdir() | Check if the path is file |
os.path.isfile() | Check if the path is a file |
os.path.getsize() | Get file size in bytes |
Example
# This will join the folder path and file name.
path = os.path.join("folder", "file.txt")
# This will return True if path exists.
print(os.path.exists(path))
# get size of file
print(os.path.getsize(path))
Environment Variables
View environment variables
print(os.environ)
Fetch a specific variable
print(os.environ.get("PATH"))
Run System Commands
Open Command Prompt / Terminal command
# get list of file and folder in windos current directory.
os.system("dir") # Windows
# get list of file and folder in linux current directory.
os.system("ls") # Mac/Linux
Examples of Python OS Module
Create a Folder Only If It Doesn’t Exist
import os
folder = "project_data"
if not os.path.exists(folder):
os.mkdir(folder)
print("Folder created")
else:
print("Folder already exists")
List Files Only (Exclude folders)
import os
for item in os.listdir():
if os.path.isfile(item):
print("File:", item)
List Folders Only (Exclude files)
import os
for item in os.listdir():
if os.path.isdir(item):
print("Folder:", item)
Recursively List Files from All Subdirectories
import os
path = "C:/Users"
for root, dirs, files in os.walk(path):
print("Current Path:", root)
print("Folders:", dirs)
print("Files:", files)
print("----------")
Count Number of Files and Folders
import os
files = 0
folders = 0
for item in os.listdir():
if os.path.isfile(item):
files += 1
elif os.path.isdir(item):
folders += 1
print("Files:", files)
print("Folders:", folders)
Copy File to Another Folder (Using os)
osdoesn’t have copy directly, so use system command
import os
os.system("copy source.txt destination\\") # Windows
os.system("cp source.txt destination/") # Linux/Mac
Get File Size in KB / MB
import os
file = "example.txt"
size = os.path.getsize(file) # in bytes
print(size / 1024, "KB")
print(size / (1024*1024), "MB")
Check File Permissions
import os
filepath = "example.txt"
print("Readable:", os.access(filepath, os.R_OK))
print("Writable:", os.access(filepath, os.W_OK))
print("Executable:", os.access(filepath, os.X_OK))
Execute Multiple Commands at Once
import os
os.system("dir && echo Hello World") # Windows
# os.system("ls; echo Hello World") # Linux/Mac
Change File Permission (Linux/Mac)
import os
os.chmod("script.sh", 0o755) # Owner read/write/execute, others read/execute
Python Read/Write CSV
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
What is a CSV File?
CSV stands for Comma-Separated Values and is commonly used for tabular data like spreadsheets.
Python has a built-in csv module for handling CSV files.
import csv
Write to CSV File
Write Rows Manually
import csv
with open("students.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Name", "Age", "City"]) # Header
writer.writerow(["Deepesh", 30, "Delhi"])
writer.writerow(["Riya", 25, "Mumbai"])
Write Multiple Rows (Loop / List)
import csv
data = [
["Name", "Age", "City"],
["Aman", 28, "Pune"],
["Neha", 24, "Chennai"],
["Kunal", 29, "Bangalore"]
]
with open("data.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerows(data) # write all rows at once
Read CSV File
Read and Print Entire File
import csv
with open("students.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
Read CSV as Dictionary
💡 Best when the first row contains headers.
import csv
with open("students.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row["Name"], row["City"])
Write CSV Using Dictionary
import csv
data = [
{"Name": "Raj", "Age": 27, "City": "Delhi"},
{"Name": "Simran", "Age": 23, "City": "Kolkata"}
]
with open("students_dict.csv", "w", newline="") as file:
fieldnames = ["Name", "Age", "City"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
Append to a CSV File
import csv
with open("students.csv", "a", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Kiran", 22, "Hyderabad"])
Handling Custom Delimiters
Example: Semi-colon ; separated file
import csv
with open("data_semicolon.csv", "r") as file:
reader = csv.reader(file, delimiter=";")
for row in reader:
print(row)
Real Example: CSV Filtering
Print only rows where Age > 25
import csv
with open("students.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
if int(row["Age"]) > 25:
print(row)
Summary
| Task | Method |
|---|---|
| Write row | writer.writerow() |
| Write multiple rows | writer.writerows() |
| Read CSV | csv.reader() |
| Read as dict | csv.DictReader() |
| Write dict | csv.DictWriter() |
| Append data | "a" mode |
| Change delimiter | delimiter=";" |
Python Read/Write JSON
- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
JSON in Python
JSON (JavaScript Object Notation) is used for storing and exchanging data, commonly in APIs and configuration files.
Python has a built-in module:
import json
Write (Save) JSON to a File
Example Dictionary/Data
data = {
"name": "Deepesh",
"age": 30,
"skills": ["Python", "Selenium", "Automation"]
}
Save to JSON file
import json
with open("data.json", "w") as file:
json.dump(data, file, indent=4)
indent=4 makes the JSON formatted & readable.
Read (Load) JSON from a File
import json
with open("data.json", "r") as file:
content = json.load(file)
print(content)
Output
{'name': 'Deepesh', 'age': 30, 'skills': ['Python', 'Selenium', 'Automation']}
Now content is a Python dictionary, and you can access elements like:
print(content["name"]) print(content["skills"][1])
Convert Python Data ↔ JSON String
Convert Python → JSON string
import json
person = {"city": "Delhi", "pin": 110001}
json_string = json.dumps(person)
print(json_string)
Convert JSON string → Python
import json
json_str = '{"status": "success", "code": 200}'
data = json.loads(json_str)
print(data)
Update JSON File
import json
# Read existing content
with open("data.json", "r") as file:
data = json.load(file)
# Modify data
data["age"] = 31
data["skills"].append("API Testing")
# Write back to file
with open("data.json", "w") as file:
json.dump(data, file, indent=4)
Work with Lists in JSON
Example JSON file (employees.json)
[
{"id": 1, "name": "Ankit"},
{"id": 2, "name": "Riya"}
]
Add a new record
import json
with open("employees.json", "r") as file:
employees = json.load(file)
employees.append({"id": 3, "name": "Aman"})
with open("employees.json", "w") as file:
json.dump(employees, file, indent=4)
Summary
| Task | Function |
|---|---|
| Write Python → JSON file | json.dump() |
| Read JSON file → Python | json.load() |
| Python → JSON string | json.dumps() |
| JSON string → Python | json.loads() |