Python for Kids - Day 6: Modules | File Handling | Directory | Namespace | Exceptions | CSC Institute
Python for Kids
DAY 6 - Class Notes
Modules | File Handling | Directories | Namespace | Exception Handling
Python Modules
▶ Built-in Module Programs
01. Import Math Module 02. Import Random Module
import math import random
print([Link](25)) print([Link](1,10))
03. Import Specific Function 04. Import with Alias
from math import factorial import math as m
print(factorial(5)) print([Link])
05. Multiple Modules
import math
import random
print([Link](16))
print([Link](1,
5))
▶ User-Defined Modules
01. User-Defined Module - Greeting
FILE 1: [Link]
# [Link] (your own module)
def hello(name):
print("Hello,", name)
Page 1
Python for Kids - Day 6: Modules | File Handling | Directory | Namespace | Exceptions | CSC Institute
def bye(name):
print("Goodbye,", name)
FILE 2: [Link] (your program)
# [Link] (import and use your module)
import greet
[Link]("Ravi")
[Link]("Ravi")
02. User-Defined Module - Calculator
FILE 1: [Link]
# [Link] (your own module)
def add(a, b):
return a + b
def multiply(a, b):
return a * b
FILE 2: [Link] (your program)
# [Link] (import and use your module)
import calc
print([Link](10, 5))
print([Link](4, 3))
File Handling
Mode Symbol What it does
Read "r" Opens existing file to read. Error if file not
found.
Write "w" Creates new file OR overwrites existing file.
Append "a" Opens existing file and adds content at the
end.
Page 2
Python for Kids - Day 6: Modules | File Handling | Directory | Namespace | Exceptions | CSC Institute
Create "x" Creates a new file. Error if file already exists.
▶ File Handling Programs
01. Create and Write File 02. Read File
f = open("[Link]", "w") f = open("[Link]",
[Link]("Hello Python") "r")
[Link]() print([Link]())
[Link]()
03. Append File 04. Read Line by Line
f = open("[Link]", "a") f = open("[Link]",
[Link]("\nWelcome") "r")
[Link]() for line in f:
print(line)
[Link]()
05. Close File
f = open("[Link]", "r")
print("File Opened")
[Link]()
print("File Closed")
Directory Management
Function Code What it does
Get current [Link]() Shows the folder you
folder are working in
Create folder [Link]("FolderName") Creates a new folder
Page 3
Python for Kids - Day 6: Modules | File Handling | Directory | Namespace | Exceptions | CSC Institute
Delete folder [Link]("FolderName") Removes an empty
folder
List all files [Link]() Shows all files in current
folder
Change folder [Link]("FolderName") Moves into a different
folder
▶ Directory Programs
01. Current Directory 02. Create Directory
import os import os
print([Link]()) [Link]("MyFolder")
03. Remove Directory 04. List Files
import os import os
[Link]("MyFolder") print([Link]())
05. Change Directory
import os
[Link]("MyFolder")
print([Link]())
Namespace & Scope
What is Namespace?
Namespace is the place where Python stores all variable and function
names.
Python has 3 main namespaces:
1. Local Namespace - variables inside a function
(exist only while function runs)
2. Global Namespace - variables at top level of
program (exist for whole program)
Page 4
Python for Kids - Day 6: Modules | File Handling | Directory | Namespace | Exceptions | CSC Institute
3. Built-in Namespace - Python's own names like
print, len, int, str
Type Where it lives Example
Local Inside a function x = 5 inside def show():
Global Outside all x = 5 at the top of program
functions
Built-in Python itself print(), len(), range()
▶ Namespace Programs
01. Local Variable 02. Global Variable
def show(): x = 50
x = 10 def test():
print(x) print(x)
show() test()
03. Local and Global 04. Using global
x = 100 Keyword
def demo(): x = 10
x = 20 def change():
print(x) global x
demo() x = 50
print(x) change()
print(x)
05. Built-in Namespace
print(len("Python"))
Page 5
Python for Kids - Day 6: Modules | File Handling | Directory | Namespace | Exceptions | CSC Institute
Exception Handling
Keyword Purpose
try Put the code that MIGHT cause an error
here
except Runs if an error occurs in the try block
else Runs ONLY if NO error occurred in try
finally ALWAYS runs whether error occurred or not
▶ Exception Handling Programs
01. Basic Exception 02. Handle ValueError
try: try:
print(10 / 0) num = int("abc")
except: except ValueError:
print("Error Occurred") print("Invalid
Number")
03. Multiple Exceptions 04. finally Block
try: try:
a = 10 / 0 print("Hello")
except ZeroDivisionError: finally:
print("Cannot Divide by
Zero") print("Finished")
except:
print("Some Error")
05. else Block
try:
print(10 / 2)
except:
print("Error")
else:
print("No Error")
Page 6
Python for Kids - Day 6: Modules | File Handling | Directory | Namespace | Exceptions | CSC Institute
Page 7