# 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
[Read More]
All the Tags in this Category
09-Python Basic
Functions, Parameters, Arguments
# Topic covered
* Functions
* User Defined Functions
* Return Statement
* Parameters & Arguments
* Positional arguments
* Keyword arguments
* Default arguments
* Variable length arguments
[Read More]
10-Python Basic
Built-in Functions, Generator, Iterator
# Topic covered
* Built-in Functions
* Anonymous/lambda Functions
* map() function
* reduce() function
* filter() function
* eval() function
10.1 Anonymous/lambda Functions
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))
Note
Lambda Function internally returns expression value and we are not required to write return statement explicitly
10-Python Basic
Generator, Iterator
# Topic covered
* Generators
* Iterable and Iterator in Python
* Create an Iterator Class
11.1 Generators
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]
Advantages of Generator Functions
- When compared with class level iterators, generators are very easy to use
- Improves
memory utilizationandperformance. - Generators are the best suitable for reading data from large number of large files
- Generators work great for web scraping and crawling.
* Saves memory
* Don't want the result immediately
* Lazy evaluation
[…] → list comprehension (eager, stores everything in memory).
We will get MemoryError in this case because all these values are required to store in the memory.
10-Python Basic
Scopes and Name Resolution
Scopes and Name Resolution
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.
01-Python intermediate
OS Module and methods with Examples
# Topic covered
* Python OS Module
* Common OS functions
* OS Path
* Additional OS Fxn
[Read More]
02-Python intermediate
File Handling, File operation in Python
# 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
[Read More]
03-Python intermediate
Exception Handling
# Topic covered
* Exception Handling
* Syntax Errors
* Runtime Errors
* Custom Exception
* List Of General Use Exceptions
* Python’s Exception Hierarchy
[Read More]
04-Python intermediate
Regular expression in Python
# Topic covered
* Regular expression
* Raw string
* String operation
* RegEx function
* Metacharacters
* Advance RegEx
[Read More]
05-Python intermediate
Deepcopy and shallow copy, Value equality vs identity
# Topic covered
* Deepcopy and Shallow copy
* Value equality vs identity
Deepcopy and Shallow copy
- In case of
shallow copy, a reference of object is copied in other object.- It means that any changes made to the copied object do reflect in the original object
External object id changes but Internal object id remains same
- In case of
deep copy, a copy of object is copied in other object.- It means that any changes made to the copied object do not reflect in the original object
External and internal object ids changes