W02.2- Python Concept
W02.2- Python Concept
PYTHON PROGRAMMING
FUNDAMENTALS
2
REVIEW PYTHON CONCEPTS
THE PYTHON REVIEW
Python concepts
• Python concepts expand your programming capabilities and allow you to write more efficient,
scalable, and maintainable code.
• Understanding and mastering these concepts will enable you to tackle more complex
programming challenges and write more sophisticated Python code.
Here’s a brief overview of some key advanced Python concepts:
def my_decorator(func):
def wrapper():
• Function Decorators: Modify print("Something is happening before the function is called.")
the behavior of a function or func()
print("Something is happening after the function is called.")
method using the @decorator
return wrapper
syntax.
# Output:
• Class Decorators: Modify or @my_decorator
Something is happening before the function is called.
def say_hello():
enhance classes in a similar way. Hello!
print("Hello!")
Something is happening after the function is called.
say_hello()
6
THE PYTHON CONCEPTS Output:
10
Generators
3. Generators and Iterators 9
import time 8
7
• Iterators: Objects that implement the ‘__iter__( )’ and ‘__next__( )’ 6
methods. def countdown(n): 5
while n >= 0: 4
• Generators: Functions that use ‘yield’ to return a sequence of values, yield n
3
providing a memory-efficient way to handle large datasets. 2
time.sleep(1) 1
• In Python, ‘yield’ is used in a function to make it a generator. n -= 1 Start Now…
• When the function is called, it returns a generator object instead of
executing the function's code immediately. for number in countdown(10):
• Each time the generator's ‘__next__( )’ method is called, the function if number == 0:
executes until it hits a ‘yield’ statement, which returns the yielded value print('Start Now...')
and pauses the function's state, allowing it to resume from where it left
off the next time ‘__next__( )’ is called.
• This makes it efficient for iterating over large datasets without storing else:
the entire dataset in memory. print(number)
7
THE PYTHON CONCEPTS
4. Context Managers
Using ‘with’ Statements
• Using with Statements:
Simplify resource
with open('example.txt', 'w') as file:
management, such as file
file.write('Hello, world!')
operations, by ensuring
proper acquisition and # Automatically closes the file when the block ends
release of resources.
8
THE PYTHON CONCEPTS
5. Metaclasses
Metaclasses
import threading
• Threading: Achieve concurrent execution using def print_numbers():
threads for I/O-bound tasks. for i in range(10):
print(i)
• Multiprocessing: Use multiple processes to achieve thread = threading.Thread(target=print_numbers)
parallelism, especially for CPU-bound tasks. thread.start()
2). Asyncio
thread.join()
import asyncio
• Asyncio: Write asynchronous code using ‘async’
async def main():
and ‘await’ for improved performance in I/O-bound print("Hello...")
await asyncio.sleep(1)
tasks.
print("...World!")
10 asyncio.run(main())
THE PYTHON CONCEPTS
7. Type Hinting Type Hinting
• This line defines a function named ‘greeting’ that takes one parameter named name.
• ‘name: str’ indicates that the parameter name is expected to be a string.
• ‘-> str’ indicates that the function is expected to return a string.
11
THE PYTHON CONCEPTS
8. Advanced Data Structures
1). Collections Module