03-Python intermediate
Exception Handling
# Topic covered
* Exception Handling
* Syntax Errors
* Runtime Errors
* Custom Exception
* List Of General Use Exceptions
* Pythonβs Exception Hierarchy
# Topic covered
* Exception Handling
* Syntax Errors
* Runtime Errors
* Custom Exception
* List Of General Use Exceptions
* Pythonβs Exception Hierarchy
# Topic covered
* Regular expression
* Raw string
* String operation
* RegEx function
* Metacharacters
* Advance RegEx
# Topic covered
* File Handling
* Various properties of File Object
* Allowed Modes
* Open and close file
* The with statement
* seek() and tell() methods
* Writing data into files
* Reading Data from files
* Buffer Read and Write file
* Zipping and Unzipping Files
# Topic covered
* Python OS Module
* Common OS functions
* OS Path
* Additional OS Fxn
Local scope - inside a function
Enclosing scope - from outer function if nested
Global - Top leve script
def serve_chai():
chai_type = "Masala" # local scope
print(f"Inside function {chai_type}")
chai_type = "Lemon"
serve_chai()
print(f"Outside function: {chai_type}")
def chai_counter():
chai_order = "lemon" # Enclosing scope
def print_order():
chai_order = "Ginger"
print("Inner:", chai_order)
print_order()
print("Outer: ", chai_order)
chai_order = "Tulsi" # Global
chai_counter()
print("Global :", chai_order)
NonLocal - The nonlocal keyword is used to modify a variable in an enclosing but non-global scope, which is specifically relevant for nested functions.
# Topic covered
* Generators
* Iterable and Iterator in Python
* Create an Iterator Class
Generator is a function which is responsible to generate a sequence of values.
We can write generator functions just like ordinary functions, but it uses yield keyword to return values.
def simpleGeneratorFun():
yield 1
yield 2
yield 3
for value in simpleGeneratorFun():
print(value, end=' ')
# 1 2 3
x = simpleGeneratorFun()
print(list(x))
# [1, 2, 3]
memory utilization and performance.* Saves memory
* Don't want the result immediately
* Lazy evaluation
We will get MemoryError in this case because all these values are required to store in the memory.
# Topic covered
* Built-in Functions
* Anonymous/lambda Functions
* map() function
* reduce() function
* filter() function
* eval() function
Sometimes we can declare a function without any name, such type of nameless functions are called
anonymous/lambda functions
The main purpose of anonymous function is just for instant use(i.e. for one time usage)
AKA lambda functions or shorthand function
# Syntax
lambda argument_list : expression
print((lambda x: x + 1)(2)) # 3
s = lambda n:n*n
print(s(4)) # 64
s = lambda a,b:a+b
print(s(2, 3)) # 5
s = lambda a,b:a if a>b else b
print(s(10,20)) # 20
print(s(100,200)) # 200
print((lambda x, y, z=3: x + y + z)(1, 2))
Lambda Function internally returns expression value and we are not required to write return statement explicitly
# Topic covered
* Dictionary Data Structure
* Dictionary declaration
* Keys of Dictionary
* Update Dictionary
* Display/access Dictionary
* Dictionary in-built function
* Dictionary Comprehension
* Iterate Through a Dictionary
* Usage
# Topic covered
* Functions
* User Defined Functions
* Return Statement
* Parameters & Arguments
* Positional arguments
* Keyword arguments
* Default arguments
* Variable length arguments
# Topic covered
* Tuple Data Structure
* Tuple Packing and Unpacking
* Mathematical operators for tuple
* Tuple in-built function
* Tuple Comprehension
* Set Data Structure
* Mathematical operations on the Set
* Set in-built function
* Set Comprehension