0% found this document useful (0 votes)
1 views

Python Functions Modules FileIO

This document provides an overview of Python programming concepts related to functions, modules, and file I/O. It covers defining and calling functions, parameters, variable scopes, module usage, file handling, and iterators. The conclusion emphasizes the importance of these concepts for code organization and efficient data management.

Uploaded by

Vidhya Vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python Functions Modules FileIO

This document provides an overview of Python programming concepts related to functions, modules, and file I/O. It covers defining and calling functions, parameters, variable scopes, module usage, file handling, and iterators. The conclusion emphasizes the importance of these concepts for code organization and efficient data management.

Uploaded by

Vidhya Vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Python Programming: Functions,

Modules, and File I/O


Unit IV & Unit V Overview
Functions and Modules
• • Functions allow code reusability and
organization.
• • Modules help in structuring large programs.
• • Topics Covered: Defining, Calling,
Parameters, Types, Global/Local Variables,
Anonymous Functions, Importing & Creating
Modules.
Defining and Calling Functions
• • Defined using `def` keyword.
• • Functions execute when called.

• Example:
• ```python
• def greet():
• print('Hello!')
• greet()
• ```
Function Parameters and Types
• • Formal vs Actual Parameters.
• • Types: Built-in, User-defined, Recursive,
Anonymous (Lambda).

• Example:
• ```python
• def add(a, b):
• return a + b
• print(add(3, 5))
Global and Local Variables
• • Local: Defined inside a function.
• • Global: Defined outside any function.

• Example:
• ```python
• x = 10 # Global
• def example():
• y = 5 # Local
• print(x + y)
Importing and Creating Modules
• • Modules help reuse code.
• • Importing modules using `import`.

• Example:
• ```python
• import math
• print(math.sqrt(25))
• ```
File I/O: Introduction
• • Python provides built-in functions for file
handling.
• • Modes: Read ('r'), Write ('w'), Append ('a').
• • Files must be opened before use and closed
after use.
Reading and Writing Files
• • Reading a file:
• ```python
• file = open('data.txt', 'r')
• print(file.read())
• file.close()
• ```
• • Writing to a file:
• ```python
• file = open('data.txt', 'w')
Manipulating Directories
• • Using `os` module for directory handling.

• Example:
• ```python
• import os
• os.mkdir('new_folder')
• os.rename('old.txt', 'new.txt')
• os.remove('unwanted.txt')
• ```
Iterators and Their Applications
• • Iterators allow sequential access to
elements.
• • Used for handling large datasets efficiently.

• Example:
• ```python
• my_list = [1, 2, 3]
• iterator = iter(my_list)
• print(next(iterator))
Conclusion
• • Functions and Modules improve code
organization.
• • File I/O helps in persistent data storage.
• • Iterators make handling large data easier.
• • Practice examples to strengthen
understanding!

You might also like