0% found this document useful (0 votes)
4 views4 pages

Python Functions and File Handling Guide

Uploaded by

Al Psahir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Python Functions and File Handling Guide

Uploaded by

Al Psahir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Study Material – Functions & File

Handling

1. Functions in Python
A function is a block of reusable code that performs a specific task.

1.1 Types of Functions

1.​ Built-in Functions


○​ Predefined in Python, e.g., print(), len(), type(), max(), min().
2.​ Functions Defined in Modules
○​ Python standard library functions, e.g., [Link](), [Link]().
3.​ User-defined Functions
○​ Functions created by programmers using the def keyword.

1.2 Creating User-defined Functions


def greet(name):
print(f"Hello, {name}!")

●​ Calling the function: greet("Alice") → Output: Hello, Alice!

1.3 Arguments and Parameters

1.​ Positional Parameters


○​ Values are passed in the same order as defined.

def add(a, b):


return a + b
print(add(5, 3)) # Output: 8

2.​
3.​ Default Parameters
○​ Default values used if argument not provided.
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Output: Hello, Guest!

4.​

1.4 Returning Values


def square(n):
return n ** 2
result = square(5) # 25

1.5 Flow of Execution

●​ Python executes function code only when it is called.


●​ After executing return, the control goes back to caller.

1.6 Scope of Variables

1.​ Local Scope


○​ Defined inside a function, accessible only there.
2.​ Global Scope
○​ Defined outside all functions, accessible anywhere.

x = 10 # Global
def func():
y = 5 # Local
print(x, y)
func() # Output: 10 5

2. Introduction to Files
●​ Files allow data storage and retrieval from disk.​

●​ Types of files:​

○​ Text files: Human-readable (.txt)


○​ Binary files: Machine-readable (.bin, .jpg)
○​ CSV files: Comma-separated values (.csv)
●​ Paths:​

○​ Relative path: Relative to current directory


○​ Absolute path: Full path from root, e.g., C:/Users/...

3. Text File Handling


3.1 Opening a File
f = open("[Link]", "r") # r → read mode

3.2 File Modes


Mode Description

r Read (default)

r+ Read & write

w Write (overwrite)

w+ Write & read

a Append

a+ Append & read

3.3 Closing a File


[Link]()

●​ Or use with clause (auto-close):

with open("[Link]", "r") as f:


data = [Link]()

3.4 Writing to a File


with open("[Link]", "w") as f:
[Link]("Hello World\n")
[Link](["Line 1\n", "Line 2\n"])

3.5 Reading from a File


with open("[Link]", "r") as f:
data = [Link]() # Read whole file
line = [Link]() # Read single line
lines = [Link]() # Read all lines into a list

3.6 File Manipulation

●​ seek(offset, whence=0): Move file pointer


●​ tell(): Get current pointer position

with open("[Link]", "r") as f:


print([Link]()) # Pointer position
[Link](0) # Go to start

3.7 Key Points

●​ Use with for safe file handling.


●​ Writing overwrites unless using append (a).
●​ Always close files (or use with) to save changes.

📌 Quick Revision
●​ Functions: Built-in, Module, User-defined
●​ Parameters: Positional, Default
●​ Return values with return
●​ Scope: Local & Global
●​ Files: Text, Binary, CSV
●​ Modes: r, r+, w, w+, a, a+
●​ File methods: read(), readline(), readlines(), write(), writelines(), seek(), tell()

You might also like