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

Day7 Notes

The document covers key topics in Python including functions, file handling, and overviews of NumPy and Pandas. It explains the use of functions for code modularity, file I/O operations, and the capabilities of NumPy for numerical computations and Pandas for data analysis. Additionally, it provides syntax examples and installation instructions for the libraries.
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)
2 views4 pages

Day7 Notes

The document covers key topics in Python including functions, file handling, and overviews of NumPy and Pandas. It explains the use of functions for code modularity, file I/O operations, and the capabilities of NumPy for numerical computations and Pandas for data analysis. Additionally, it provides syntax examples and installation instructions for the libraries.
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
You are on page 1/ 4

Day 7 Notes

Topics Covered
• Functions in Python
• Python File Handling (Files I/O)
• NumPy Overview
• Pandas Overview

1. Using Functions in Python


Functions break down code into reusable blocks. They simplify code, improve
readability, and support modularity.

Basic Syntax:

def function_name(param1, param2):


# code block
return result

Calling Functions:

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

print(greet("Ashish"))

Keyword Arguments:

def student(name, age):


print(name, age)

student(age=20, name="Rahul")

Default Parameters:

def greet(name="Guest"):
print("Hello", name)

Variable-Length Arguments (*args and **kwargs):


def show_all(*args):
for item in args:
print(item)

def show_details(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

Return Statement:

def add(a, b):


return a + b

Lambda Functions (Anonymous Functions):

square = lambda x: x * x
print(square(4))

Using Lambda with filter():

nums = [1, 2, 3, 4, 5]
even = list(filter(lambda x: x % 2 == 0, nums))

Using Lambda with map():

squares = list(map(lambda x: x * x, nums))

Using Lambda with reduce():

from functools import reduce


total = reduce(lambda x, y: x + y, nums)

2. Modules in Python
Modules organize related code (functions, classes) into separate files.

Creating a Module (calc.py):

def add(x, y):


return x + y

def subtract(x, y):


return x - y
Importing a Module:
import calc
print(calc.add(10, 5))

3. Python Files I/O


Python uses file I/O to store and retrieve data persistently.

Text vs Binary Files:

• Text files store data as readable characters.


• Binary files store data as bytes (e.g., images, videos).

Opening Files:

file = open("data.txt", "r")

File Modes:

• r - Read
• w - Write (overwrite)
• a - Append
• rb/wb/ab - Binary Read/Write/Append
• r+/w+/a+ - Read+Write variants

Writing to a File:

f = open("file.txt", "w")
f.write("Hello World")
f.close()

Reading from a File:

f = open("file.txt", "r")
print(f.read())

Reading line-by-line:
f = open("file.txt", "r")
print(f.readline())

Working with Binary Files:


f1 = open("mickey.jpg", "rb")
f2 = open("new.jpg", "wb")
bytes = f1.read()
f2.write(bytes)
f1.close()
f2.close()

4. NumPy Overview
NumPy is used for numerical computations with support for multi-
dimensional arrays.

• Array creation: np.array(), np.zeros(), np.ones()


• Mathematical operations: addition, subtraction, etc.
• Linear algebra and statistics: dot, mean, std, etc.
• Random number generation from distributions

Installation:

pip install numpy

Importing NumPy:

import numpy as np

5. Pandas Overview
Pandas is a powerful data analysis library in Python.

• Used to handle tabular data (rows and columns)


• Functions to read, filter, group, and clean data
• Handles missing values and data manipulation

Installation:

pip install pandas

Importing Pandas:

import pandas as pd

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy