Unit3 Python Notes
Unit3 Python Notes
#### 1. Functions
- **Syntax**:
def function_name(parameters):
# Code block
return value
- Predefined functions provided by Python, e.g., print(), len(), type(), input(), etc.
- Examples:
- **os**: Interact with the operating system (file operations, environment variables).
- **Defining**:
def greet(name):
return a + b
def say_hello():
print("Hello")
- **Lifetime**: A variable's lifetime depends on its scope; local variables are destroyed after the
function ends.
def greet(name="Guest"):
person_details(age=20, name="Tanisha")
def add(*numbers):
return sum(numbers)
def display_info(**details):
print(f"{key}: {value}")
- Using the sys module to access arguments passed from the command line.
import sys
import random
---
### Recursion
#### 1. Introduction
- **Factorial**:
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
- **Fibonacci**:
def fibonacci(n):
if n <= 1:
return n
---
- **Write**:
f.write("Hello World")
- **Read**:
print(f.read())
- **Write Binary**:
f.write(b"Binary Data")
- **Read Binary**:
print(f.read())
pickle.dump([1, 2, 3], f)
print(pickle.load(f))
import csv
writer = csv.writer(f)
writer.writerow(["Name", "Age"])
writer.writerow(["Tanisha", 20])
reader = csv.reader(f)
print(row)