Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Python Command Notes

This document is a comprehensive guide to Python programming, designed to take readers from beginner to advanced levels. It covers essential concepts including syntax, data structures, object-oriented programming, and advanced topics such as concurrency and web programming. By the end, readers will have a solid understanding of Python and its applications in various fields.

Uploaded by

devagya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Command Notes

This document is a comprehensive guide to Python programming, designed to take readers from beginner to advanced levels. It covers essential concepts including syntax, data structures, object-oriented programming, and advanced topics such as concurrency and web programming. By the end, readers will have a solid understanding of Python and its applications in various fields.

Uploaded by

devagya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Python

Comprehensive Python Programming


Notes
Welcome to your journey into the world of Python programming! This guide is
designed to take you from a beginner to an advanced level, covering all
essential concepts with detailed explanations and examples. By the end of
these notes, you'll have a solid understanding of Python's syntax, data
structures, object-oriented programming, functional programming, and
advanced features used in professional and academic settings.

Table of Contents
1. Introduction to Python

2. Setting Up Your Python Environment

3. Python Syntax and Basics

3.1 Variables and Data Types

3.2 Basic Input and Output

3.3 Operators

4. Control Flow

4.1 Conditional Statements

4.2 Loops

4.3 List Comprehensions

5. Functions

5.1 Defining and Calling Functions

5.2 Function Arguments

5.3 Lambda Functions

Python 1
5.4 Decorators

6. Modules and Packages

7. Data Structures

7.1 Lists

7.2 Tuples

7.3 Sets

7.4 Dictionaries

8. Object-Oriented Programming (OOP)

8.1 Classes and Objects

8.2 Inheritance

8.3 Encapsulation

8.4 Polymorphism

8.5 Abstract Classes and Interfaces

9. Exceptions and Error Handling

10. File Handling

11. Advanced Topics

11.1 Iterators and Generators

11.2 Context Managers

11.3 Metaprogramming

11.4 Concurrency and Parallelism

11.5 Asynchronous Programming

12. Regular Expressions

13. Working with Databases

14. Networking and Web Programming

15. Testing and Debugging

16. Scientific Computing and Data Science

17. Best Practices and Coding Standards

18. Additional Resources

Python 2
19. Conclusion

1. Introduction to Python
What is Python?
Python is a high-level, interpreted programming language known for its
readability and simplicity. It was created by Guido van Rossum and first
released in 1991. Python emphasizes code readability with its use of significant
indentation, allowing programmers to express concepts in fewer lines of code
than might be used in languages such as C++ or Java.

Key Features of Python


Simple and Readable Syntax: Python's syntax is clear and intuitive, making
it an excellent language for beginners.

Interpreted Language: Python code is executed line by line, which makes


debugging easier.

Dynamically Typed: Variable types are determined at runtime, which adds


flexibility.

Extensive Standard Library: Python has a vast library of modules and


functions for various tasks.

Cross-Platform: Python runs on Windows, macOS, Linux, and more.

Why Learn Python?


Versatility: Used in web development, data analysis, artificial intelligence,
scientific computing, automation, and more.

Large Community: Active community provides extensive resources and


support.

Career Opportunities: High demand for Python developers in various


industries.

2. Setting Up Your Python Environment


Installing Python
1. Download Python:

Python 3
Visit the official Python website and download the latest version
compatible with your operating system (Windows, macOS, or Linux).

2. Install Python:

Run the installer and follow the instructions.

Important: On Windows, during installation, check the option "Add


Python to PATH" to ensure you can run Python from the command line.

3. Verify Installation:

Open your command prompt (Windows) or terminal (macOS/Linux).

Type python --version or python3 --version and press Enter.

You should see the installed Python version displayed.

Choosing an Integrated Development Environment (IDE)


An IDE provides tools for writing, testing, and debugging code.

IDLE: Comes with Python installation; suitable for beginners.

Visual Studio Code (VSCode): A lightweight editor with powerful


extensions for Python.

PyCharm: A feature-rich IDE designed specifically for Python development.

Jupyter Notebook: An interactive coding environment ideal for data


science and exploratory programming.

Your First Python Program


Let's write a simple program to ensure everything is set up correctly.

1. Create a File:

Open your IDE or a text editor.

Create a new file named hello_world.py .

2. Write the Code:

python
Copy code
print("Hello, World!")

Python 4
3. Save and Run the Program:

Save the file.

Open your command prompt or terminal.

Navigate to the directory containing hello_world.py .

Run the program by typing python hello_world.py and pressing Enter.

You should see Hello, World! printed in the console.

Explanation:

print() is a built-in function that outputs data to the console.

The string "Hello, World!" is passed as an argument to print() , which


displays it when the program runs.

3. Python Syntax and Basics


Understanding Python's syntax is crucial as it forms the foundation for writing
code.

3.1 Variables and Data Types

Variables
Variables are containers for storing data values. In Python, variables are
created when you assign a value to them.

Example:

python
Copy code
age = 25
name = "Alice"
height = 5.6
is_student = True

Explanation:

age : An integer variable assigned the value 25 .

name : A string variable assigned the value "Alice" .

Python 5
height : A float variable assigned the value 5.6 .

is_student : A boolean variable assigned the value True .

Dynamic Typing
Python is dynamically typed, meaning you don't need to declare the type of a
variable explicitly.

Example:

python
Copy code
x = 10 # x is an integer
x = "Hello" # Now x is a string

Data Types
1. Numeric Types:

Integer ( int ): Whole numbers, positive or negative, without decimals.

python
Copy code
count = 100

Floating Point ( float ): Numbers with decimal points.

python
Copy code
temperature = 98.6

Complex Numbers ( complex ): Numbers with a real and imaginary part.

python
Copy code

Python 6
z = 3 + 5j

2. String ( str ):

A sequence of characters enclosed in single ' or double " quotes.

python
Copy code
message = "Hello, World!"

3. Boolean ( bool ):

Represents True or False .

python
Copy code
is_active = False

4. NoneType:

Represents the absence of a value.

python
Copy code
result = None

Type Checking and Casting


Type Checking:

python
Copy code
type_of_variable = type(variable)
print(type_of_variable)

Python 7
Example:

python
Copy code
print(type(age)) # Output: <class 'int'>

Type Casting:

Converting one data type to another.

python
Copy code
# String to integer
number = int("10") # Output: 10

# Integer to string
text = str(10) # Output: '10'

# Float to integer (loses decimal part)


whole_number = int(5.99) # Output: 5

# String to float
decimal_number = float("3.14") # Output: 3.14

Important: If the conversion is not possible, Python will raise a ValueError .

3.2 Basic Input and Output

Output Using print()


The print() function displays the given object to the standard output (console).
Example:

python
Copy code
print("Welcome to Python!")
print("Name:", name)

Python 8
print("Age:", age)

Explanation:

You can print multiple items by separating them with commas.

print() adds a newline at the end by default.

Formatted Strings (f-Strings)


Introduced in Python 3.6, f-strings allow you to embed expressions inside string
literals.
Example:

python
Copy code
print(f"Hello, {name}! You are {age} years old.")

Explanation:

Prefix the string with f to use f-strings.

Place variables or expressions inside {} within the string.

Input Using input()


The input() function reads a line from input (console) and returns it as a string.
Example:

python
Copy code
user_name = input("Enter your name: ")
print(f"Hello, {user_name}!")

Explanation:

The prompt string inside input() is displayed to the user.

The user's input is stored in user_name .

Python 9
Remember that input() always returns a string. If you need another data
type, cast it.

Casting Input:

python
Copy code
age = int(input("Enter your age: "))
print(f"You will be {age + 1} next year.")

Explanation:

The input string is converted to an integer using int() .

Be cautious; if the user enters non-numeric data, a ValueError will occur.

Handling Invalid Input:

python
Copy code
try:
age = int(input("Enter your age: "))
except ValueError:
print("Please enter a valid number.")

3.3 Operators
Operators are used to perform operations on variables and values.

Arithmetic Operators
Used for mathematical calculations.

1. Addition ( + ):

python
Copy code
sum = 10 + 5 # Output: 15

Python 10
2. Subtraction ( ):

python
Copy code
difference = 10 - 5 # Output: 5

3. Multiplication ( ):

python
Copy code
product = 10 * 5 # Output: 50

4. Division ( / ):

python
Copy code
quotient = 10 / 3 # Output: 3.3333333333333335

5. Floor Division ( // ):

Returns the largest integer less than or equal to the result.

python
Copy code
floor_div = 10 // 3 # Output: 3

6. Modulus ( % ):
Returns the remainder of the division.

python
Copy code
remainder = 10 % 3 # Output: 1

Python 11
7. Exponentiation ( * ):
Raises the number to the power of the exponent.

python
Copy code
power = 2 ** 3 # Output: 8

Example Application:

python
Copy code
radius = float(input("Enter the radius of the circle: "))
area = 3.14159 * radius ** 2
print(f"The area of the circle is {area}")

Assignment Operators
Used to assign values to variables.

1. Simple Assignment ( = ):

python
Copy code
x = 5

2. Compound Assignment Operators:

Addition Assignment ( += ):

python
Copy code
x += 3 # Equivalent to x = x + 3

Subtraction Assignment ( = ):

Python 12
python
Copy code
x -= 2 # Equivalent to x = x - 2

Multiplication Assignment ( = ):

python
Copy code
x *= 4 # Equivalent to x = x * 4

Division Assignment ( /= ):

python
Copy code
x /= 2 # Equivalent to x = x / 2

Modulus Assignment ( %= ):

python
Copy code
x %= 3 # Equivalent to x = x % 3

Exponentiation Assignment ( *= ):

python
Copy code
x **= 2 # Equivalent to x = x ** 2

Comparison Operators
Used to compare two values and return a boolean ( True or False ).

1. Equal To ( == ):

Python 13
python
Copy code
x == y

2. Not Equal To ( != ):

python
Copy code
x != y

3. Greater Than ( > ):

python
Copy code
x > y

4. Less Than ( < ):

python
Copy code
x < y

5. Greater Than or Equal To ( >= ):

python
Copy code
x >= y

6. Less Than or Equal To ( <= ):

python
Copy code

Python 14
x <= y

Example:

python
Copy code
score = 85

if score >= 90:


grade = 'A'
elif score >= 80:
grade = 'B'
else:
grade = 'C'

print(f"Your grade is {grade}")

Logical Operators
Used to combine conditional statements.

1. Logical AND ( and ):

Returns True if both operands are True .

python
Copy code
if x > 0 and x < 10:
print("x is between 1 and 9")

2. Logical OR ( or ):

Returns True if at least one operand is True .

python
Copy code
if x < 0 or x > 100:

Python 15
print("x is out of range")

3. Logical NOT ( not ):

Reverses the boolean value.

python
Copy code
if not x == 5:
print("x is not equal to 5")

Identity Operators
Check if two variables are actually the same object (same memory location).

1. Is ( is ):

python
Copy code
if x is y:
print("x and y are the same object")

2. Is Not ( is not ):

python
Copy code
if x is not y:
print("x and y are different objects")

Note: == compares values, while is compares identities.


Example:

python
Copy code
a = [1, 2, 3]

Python 16
b = a
c = [1, 2, 3]

print(a == b) # Output: True (same values)


print(a is b) # Output: True (same object)

print(a == c) # Output: True (same values)


print(a is c) # Output: False (different objects)

Membership Operators
Test for membership in a sequence (such as strings, lists, or tuples).

1. In ( in ):

python
Copy code
if 'apple' in fruits:
print("Apple is in the list")

2. Not In ( not in ):

python
Copy code
if 'banana' not in fruits:
print("Banana is not in the list")

Example:

python
Copy code
fruits = ['apple', 'orange', 'grape']

if 'apple' in fruits:

Python 17
print("Found an apple!")

4. Control Flow
Control flow statements allow you to control the execution order of your code.

4.1 Conditional Statements

If Statement
Executes a block of code if a condition is True .
Syntax:

python
Copy code
if condition:
# Code to execute if condition is True

Example:

python
Copy code
temperature = 75

if temperature > 70:


print("It's a warm day.")

Explanation:

If temperature is greater than 70 , the message is printed.

If-Else Statement
Provides an alternative block of code if the condition is False .
Syntax:

Python 18
python
Copy code
if condition:
# Code if condition is True
else:
# Code if condition is False

Example:

python
Copy code
age = int(input("Enter your age: "))

if age >= 18:


print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

Explanation:

The program checks if age is at least 18 .

Outputs the appropriate message based on the condition.

Elif (Else If) Statement


Allows multiple conditions to be checked sequentially.

Syntax:

python
Copy code
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
elif condition3:
# Code if condition3 is True

Python 19
else:
# Code if all above conditions are False

Example:

python
Copy code
score = int(input("Enter your score: "))

if score >= 90:


grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'

print(f"Your grade is {grade}")

Explanation:

The program assigns a grade based on the score.

Conditions are checked in order; the first condition that evaluates to True

executes its block.

Nested If Statements
You can place an if statement inside another if or else block.
Example:

python
Copy code
num = int(input("Enter a number: "))

Python 20
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

Explanation:

The outer if checks if the number is non-negative.

The inner if distinguishes between zero and positive numbers.

4.2 Loops
Loops allow you to execute a block of code multiple times.

While Loop
Repeats as long as a condition is True .
Syntax:

python
Copy code
while condition:
# Code to execute repeatedly

Example:

python
Copy code
count = 1

while count <= 5:


print(f"Count is {count}")
count += 1

Python 21
Explanation:

Initializes count to 1 .

Prints the value of count and increments it by 1 each time.

Loop continues until count exceeds 5 .

For Loop
Iterates over a sequence (list, tuple, dictionary, set, or string).
Syntax:

python
Copy code
for variable in sequence:
# Code to execute for each element

Example:

python
Copy code
fruits = ['apple', 'banana', 'cherry']

for fruit in fruits:


print(f"I like {fruit}")

Explanation:

Iterates over each element in fruits .

fruit takes the value of each item in the list.

Prints a message for each fruit.

Using range() Function


Generates a sequence of numbers.
Syntax:

Python 22
python
Copy code
range(start, stop, step)

start : Starting number (inclusive). Default is 0 .

stop : Ending number (exclusive).

step : Increment value. Default is 1 .

Example:

python
Copy code
for i in range(5):
print(i)

Output:

Copy code
0
1
2
3
4

Explanation:

range(5) generates numbers from 0 to 4 .

Example with Start and Step:

python
Copy code
for i in range(2, 10, 2):
print(i)

Python 23
Output:

Copy code
2
4
6
8

Nested Loops
A loop inside another loop.
Example:

python
Copy code
for i in range(1, 4):
for j in range(1, 3):
print(f"i={i}, j={j}")

Output:

css
Copy code
i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2
i=3, j=1
i=3, j=2

Explanation:

The outer loop runs i from 1 to 3 .

The inner loop runs j from 1 to 2 for each value of i .

Loop Control Statements

Python 24
1. Break Statement:

Exits the nearest enclosing loop.

Example:

python
Copy code
for num in range(1, 10):
if num == 5:
break
print(num)

Output:

Copy code
1
2
3
4

Explanation:

Loop stops when num equals 5 .

2. Continue Statement:

Skips the current iteration and continues with the next.

Example:

python
Copy code
for num in range(1, 6):
if num == 3:
continue
print(num)

Output:

Python 25
Copy code
1
2
4
5

Explanation:

When num is 3 , the continue statement skips the rest of the loop body
for that iteration.

3. Pass Statement:

Does nothing; a null operation.

Example:

python
Copy code
for letter in 'Python':
if letter == 'h':
pass # Placeholder for future code
print('Current Letter:', letter)

Explanation:

passis used when a statement is syntactically required but no action is


needed.

4.3 List Comprehensions


A concise way to create lists using a single line of code.

Basic Syntax

python
Copy code
new_list = [expression for item in iterable]

Python 26
Example:

python
Copy code
squares = [x ** 2 for x in range(1, 6)]
print(squares)

Output:

csharp
Copy code
[1, 4, 9, 16, 25]

Explanation:

Iterates over numbers 1 to 5 .

Calculates the square of each number and adds it to the list squares .

With Conditionals

python
Copy code
new_list = [expression for item in iterable if condition]

Example:

python
Copy code
even_numbers = [x for x in range(1, 11) if x % 2 == 0]
print(even_numbers)

Output:

csharp
Copy code

Python 27
[2, 4, 6, 8, 10]

Explanation:

Includes only numbers that are even ( x % 2 == 0 ).

Nested List Comprehensions


Used for creating lists of lists.
Example:

python
Copy code
matrix = [[j for j in range(3)] for i in range(3)]
print(matrix)

Output:

lua
Copy code
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

Explanation:

Creates a 3x3 matrix where each row is [0, 1, 2] .

5. Functions
Functions are reusable blocks of code that perform a specific task. They help
make programs more modular, easier to read, and easier to maintain.

5.1 Defining and Calling Functions

Defining a Function
In Python, you define a function using the def keyword, followed by the
function name and parentheses () containing optional parameters.

Python 28
Syntax:

python
Copy code
def function_name(parameters):
"""Docstring describing the function."""
# Function body
return value # Optional

def : Keyword used to define a function.

function_name : The name you assign to the function.

parameters : Optional, variables passed to the function.

"""Docstring""" : Optional, a string that describes what the function does.

return : Optional, specifies the value to be returned by the function.

Example:

python
Copy code
def greet():
"""Prints a greeting message."""
print("Hello, welcome to Python programming!")

Calling a Function
To execute a function, you call it by its name followed by parentheses, including
any required arguments.
Example:

python
Copy code
greet() # Output: Hello, welcome to Python programming!

Function with Parameters

Python 29
Functions can accept parameters to make them more flexible.

Example:

python
Copy code
def greet(name):
"""Greets a person by name."""
print(f"Hello, {name}! How are you?")

greet("Alice") # Output: Hello, Alice! How are you?

Explanation:

The function greet accepts one parameter name .

When calling greet("Alice") , the function prints a personalized greeting.

Function with Return Value


Functions can return values using the return statement.
Example:

python
Copy code
def add(a, b):
"""Returns the sum of two numbers."""
return a + b

result = add(5, 3)
print(f"The sum is {result}") # Output: The sum is 8

Explanation:

The function add takes two parameters a and b .

It calculates the sum and returns it.

The returned value is stored in result .

5.2 Function Arguments

Python 30
Python functions support several types of arguments:

Positional Arguments
Arguments are matched to parameters based on their position.

Example:

python
Copy code
def subtract(a, b):
"""Subtracts b from a."""
return a - b

print(subtract(10, 5)) # Output: 5

Explanation:

a gets the value 10 , b gets 5 .

The function returns 10 - 5 .

Keyword Arguments
Arguments can be passed using the parameter names.
Example:

python
Copy code
print(subtract(b=5, a=10)) # Output: 5

Explanation:

By specifying a=10 and b=5 , we can pass arguments in any order.

Default Parameters
You can assign default values to parameters, making them optional when
calling the function.
Example:

Python 31
python
Copy code
def power(base, exponent=2):
"""Raises base to the power of exponent."""
return base ** exponent

print(power(3)) # Output: 9 (uses default exponent=2)


print(power(2, 3)) # Output: 8

Explanation:

If exponent is not provided, it defaults to 2 .

Calling power(3) computes 3 ** 2 .

Variable-Length Arguments
Allows a function to accept an arbitrary number of arguments.

Arbitrary Positional Arguments ( args )


Used when you don't know beforehand how many arguments will be passed.
Example:

python
Copy code
def sum_all(*args):
"""Returns the sum of all arguments."""
total = 0
for num in args:
total += num
return total

print(sum_all(1, 2, 3)) # Output: 6


print(sum_all(4, 5, 6, 7, 8)) # Output: 30

Explanation:

args collects all extra positional arguments into a tuple.

Python 32
You can iterate over args to process each argument.

Arbitrary Keyword Arguments ( *kwargs )


Used when you want to handle named arguments that you have not defined
beforehand.
Example:

python
Copy code
def display_info(**kwargs):
"""Prints key-value pairs passed as keyword argument
s."""
for key, value in kwargs.items():
print(f"{key}: {value}")

display_info(name="Alice", age=30, city="New York")

Output:

vbnet
Copy code
name: Alice
age: 30
city: New York

Explanation:

*kwargs collects extra keyword arguments into a dictionary.

You can iterate over kwargs.items() to access keys and values.

Order of Arguments
When combining different types of arguments, the order must be:

1. Regular positional arguments

2. Default arguments

3. args

Python 33
4. *kwargs

Example:

python
Copy code
def func(a, b=2, *args, **kwargs):
pass

5.3 Lambda Functions


Lambda functions are small anonymous functions defined using the lambda

keyword. They can have any number of arguments but only one expression.
Syntax:

python
Copy code
lambda arguments: expression

Example:

python
Copy code
# Regular function
def square(x):
return x ** 2

# Lambda function
square_lambda = lambda x: x ** 2

print(square(5)) # Output: 25
print(square_lambda(5)) # Output: 25

Explanation:

Both functions compute the square of a number.

Python 34
Lambda functions are often used for short, simple functions.

Use Cases for Lambda Functions


As arguments to higher-order functions (functions that take other
functions as arguments):
Example with map() :

python
Copy code
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))
print(squares) # Output: [1, 4, 9, 16, 25]

Explanation:

map() applies the lambda function to each item in numbers .

list() converts the map object to a list.

With filter() :

python
Copy code
even_numbers = list(filter(lambda x: x % 2 == 0, number
s))
print(even_numbers) # Output: [2, 4]

Explanation:

filter() keeps elements where the lambda function returns True .

Sorting with custom keys:

python
Copy code
words = ["apple", "banana", "cherry", "date"]
words_sorted = sorted(words, key=lambda x: len(x))
print(words_sorted) # Output: ['date', 'apple', 'banan

Python 35
a', 'cherry']

Explanation:

sorted() uses the length of each word as the sorting key.

5.4 Decorators
Decorators are functions that modify the behavior of other functions or
methods. They allow you to wrap another function to extend its behavior
without permanently modifying it.

Defining a Decorator
A decorator is a function that takes a function as an argument and returns a
new function.
Example:

python
Copy code
def decorator_function(original_function):
def wrapper_function(*args, **kwargs):
# Code to execute before the original function
print(f"Wrapper executed before {original_function.
__name__}")
result = original_function(*args, **kwargs)
# Code to execute after the original function
print(f"Wrapper executed after {original_function._
_name__}")
return result
return wrapper_function

Using a Decorator
You can apply a decorator to a function using the @decorator_name syntax.
Example:

Python 36
python
Copy code
@decorator_function
def display():
print("Display function ran")

display()

Output:

css
Copy code
Wrapper executed before display
Display function ran
Wrapper executed after display

Explanation:

The @decorator_function syntax is syntactic sugar for:

python
Copy code
display = decorator_function(display)

When display() is called, it actually calls wrapper_function() inside the


decorator.

Practical Example: Logging Decorator

python
Copy code
def logger(func):
"""Decorator that logs the function execution."""
def wrapper(*args, **kwargs):
print(f"Executing '{func.__name__}' with arguments

Python 37
{args} {kwargs}")
result = func(*args, **kwargs)
print(f"'{func.__name__}' returned {result}")
return result
return wrapper

@logger
def multiply(a, b):
return a * b

product = multiply(5, 3)

Output:

csharp
Copy code
Executing 'multiply' with arguments (5, 3) {}
'multiply' returned 15

Explanation:

The logger decorator logs the function name, arguments, and return value.

Useful for debugging and monitoring.

Decorating Functions with Arguments


Decorators can handle any number of positional and keyword arguments using
*args and **kwargs .

Example:

python
Copy code
def uppercase_decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result.upper()
return wrapper

Python 38
@uppercase_decorator
def greet(name):
return f"Hello, {name}"

print(greet("Alice")) # Output: HELLO, ALICE

Explanation:

The decorator converts the result of greet() to uppercase.

Chaining Decorators
You can apply multiple decorators to a single function.
Example:

python
Copy code
def italic_decorator(func):
def wrapper(*args, **kwargs):
return f"<i>{func(*args, **kwargs)}</i>"
return wrapper

def bold_decorator(func):
def wrapper(*args, **kwargs):
return f"<b>{func(*args, **kwargs)}</b>"
return wrapper

@bold_decorator
@italic_decorator
def text(message):
return message

print(text("Hello")) # Output: <b><i>Hello</i></b>

Explanation:

Python 39
Decorators are applied from the bottom up ( italic_decorator first, then
bold_decorator ).

6. Modules and Packages


Modules and packages help you organize your code by grouping related
functions, classes, or variables into separate files.

Modules
A module is a file containing Python definitions and statements. The file name
is the module name with the suffix .py added.

Creating a Module
1. Create a File: Save your code in a file named my_module.py .

python
Copy code
# my_module.py

def greet(name):
return f"Hello, {name}!"

pi = 3.14159

Importing a Module
1. Import the Module in Another Script:

python
Copy code
# main.py

import my_module

message = my_module.greet("Alice")
print(message) # Output: Hello, Alice!

Python 40
print(my_module.pi) # Output: 3.14159

Explanation:

import my_module allows you to access functions and variables using


my_module.name .

The interpreter searches for my_module.py in the current directory and in


the directories listed in sys.path .

Importing Specific Attributes


You can import specific functions or variables.

python
Copy code
from my_module import greet, pi

print(greet("Bob")) # Output: Hello, Bob!


print(pi) # Output: 3.14159

Explanation:

Now you can use greet and pi directly without the module prefix.

Importing with an Alias


You can assign an alias to a module or function.

python
Copy code
import my_module as mm

print(mm.greet("Carol")) # Output: Hello, Carol!

Explanation:

mm is now an alias for my_module .

Packages

Python 41
A package is a way of structuring Python's module namespace by using
"dotted module names". A package is a directory containing a special file
__init__.py and other modules.

Creating a Package
1. Directory Structure:

markdown
Copy code
my_package/
__init__.py
module1.py
module2.py

2. __init__.py File:

This file can be empty or can execute initialization code for the
package.

It indicates to Python that the directory should be treated as a package.

Using a Package
1. Importing from a Package:

python
Copy code
# module1.py

def module1_func():
print("This is module1 function")

# module2.py

def module2_func():
print("This is module2 function")

# main.py

Python 42
from my_package import module1, module2

module1.module1_func() # Output: This is module1 functi


on
module2.module2_func() # Output: This is module2 functi
on

Explanation:

You can import modules from a package using from package import module .

Importing Specific Functions from Modules

python
Copy code
from my_package.module1 import module1_func

module1_func() # Output: This is module1 function

Explanation:

You can import specific functions directly from a module within a package.

Relative Imports
Used within packages to import modules relative to the current module.

Example:

python
Copy code
# Inside module2.py

from .module1 import module1_func

def module2_func():
module1_func()

Python 43
print("This is module2 function")

Explanation:

The . indicates a relative import from the same package.

7. Data Structures
Python provides built-in data structures that are essential for efficient data
manipulation.

7.1 Lists
Lists are ordered, mutable collections of items.

Creating Lists

python
Copy code
# Empty list
empty_list = []

# List of integers
numbers = [1, 2, 3, 4, 5]

# List of mixed data types


mixed_list = [1, "two", 3.0, True]

# Nested list
nested_list = [1, [2, 3], [4, 5]]

Accessing Elements
By Index:

python
Copy code
first_item = numbers[0] # Output: 1

Python 44
last_item = numbers[-1] # Output: 5

Explanation:

Indexing starts at 0 .

Negative indices start from the end ( 1 is the last item).

Slicing:

python
Copy code
sub_list = numbers[1:4] # Output: [2, 3, 4]

Explanation:

numbers[start:stop] returns a new list from start index up to but not


including stop index.

Modifying Lists
Changing Elements:

python
Copy code
numbers[0] = 10 # numbers is now [10, 2,
3, 4, 5]

Adding Elements:

python
Copy code
numbers.append(6) # Adds 6 at the end
numbers.insert(0, 0) # Inserts 0 at index 0

Removing Elements:

Python 45
python
Copy code
numbers.remove(3) # Removes first occurrence
of 3
popped_item = numbers.pop() # Removes and returns the
last item
del numbers[1] # Deletes the item at inde
x 1

List Methods
Sorting:

python
Copy code
numbers.sort() # Sorts the list in ascend
ing order
numbers.sort(reverse=True) # Sorts in descending orde
r

Reversing:

python
Copy code
numbers.reverse() # Reverses the list

Counting Occurrences:

python
Copy code
count_of_twos = numbers.count(2)

Length of List:

Python 46
python
Copy code
length = len(numbers)

Iterating Over Lists

python
Copy code
for num in numbers:
print(num)

List Comprehensions
See Section 4.3 for detailed explanations.

7.2 Tuples
Tuples are ordered, immutable collections of items.

Creating Tuples

python
Copy code
# Empty tuple
empty_tuple = ()

# Tuple of integers
coordinates = (10, 20)

# Single-element tuple (note the comma)


singleton = (5,)

Accessing Elements
Same as lists, using indexing and slicing.

Python 47
python
Copy code
x = coordinates[0] # Output: 10

Why Use Tuples?


Immutability: Ensures data cannot be modified.

Hashable: Can be used as keys in dictionaries.

Performance: Slightly faster than lists due to immutability.

Tuple Unpacking
You can assign tuple elements to variables.

python
Copy code
x, y = coordinates
print(x) # Output: 10
print(y) # Output: 20

Explanation:

Variables x and y receive the values from coordinates .

7.3 Sets
Sets are unordered collections of unique elements.

Creating Sets

python
Copy code
# Empty set (note the use of set())
empty_set = set()

# Set with elements


unique_numbers = {1, 2, 3, 2, 1}

Python 48
print(unique_numbers) # Output: {1, 2, 3}

Explanation:

Duplicates are automatically removed.

Adding and Removing Elements


Adding Elements:

python
Copy code
unique_numbers.add(4)

Removing Elements:

python
Copy code
unique_numbers.remove(2) # Raises KeyError if 2 is
not present
unique_numbers.discard(5) # Does nothing if 5 is not
present

Set Operations
Union ( | ):

python
Copy code
set_a = {1, 2, 3}
set_b = {3, 4, 5}
union_set = set_a | set_b # Output: {1, 2, 3, 4, 5}

Intersection ( & ):

Python 49
python
Copy code
intersection_set = set_a & set_b # Output: {3}

Difference ( ):

python
Copy code
difference_set = set_a - set_b # Output: {1, 2}

Symmetric Difference ( ^ ):

python
Copy code
symmetric_diff_set = set_a ^ set_b # Output: {1, 2, 4,
5}

Membership Testing

python
Copy code
if 3 in set_a:
print("3 is in set_a")

7.4 Dictionaries
Dictionaries are unordered collections of key-value pairs.

Creating Dictionaries

python
Copy code
# Empty dictionary

Python 50
empty_dict = {}

# Dictionary with data


person = {
"name": "Alice",
"age": 30,
"city": "New York"
}

Accessing Values

python
Copy code
print(person["name"]) # Output: Alice

Explanation:

Access the value associated with the key "name" .

Adding and Updating Entries

python
Copy code
person["email"] = "alice@example.com" # Adds new key-value
pair
person["age"] = 31 # Updates value for k
ey "age"

Removing Entries

python
Copy code
del person["city"] # Removes the key "c
ity"
age = person.pop("age") # Removes "age" and

Python 51
returns its value

Dictionary Methods
Keys, Values, Items:

python
Copy code
keys = person.keys() # Returns a view of
keys
values = person.values() # Returns a view of
values
items = person.items() # Returns a view of
key-value pairs

Iterating Over a Dictionary:

python
Copy code
for key, value in person.items():
print(f"{key}: {value}")

Dictionary Comprehensions
Similar to list comprehensions.
Example:

python
Copy code
squares = {x: x ** 2 for x in range(1, 6)}
print(squares) # Output: {1: 1, 2:
4, 3: 9, 4: 16, 5: 25}

Python 52
8. Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm that uses
objects and classes to design and develop applications. It enables developers
to model real-world entities and relationships, making code more modular,
reusable, and easier to maintain.

8.1 Classes and Objects

Classes
A class is a blueprint or template for creating objects (instances). It defines a
set of attributes and methods that the objects created from the class can have.
Syntax:

python
Copy code
class ClassName:
"""Docstring describing the class."""
# Class attributes and methods

Example:

python
Copy code
class Car:
"""A simple Car class."""

# Class attribute (shared by all instances)


wheels = 4

def __init__(self, make, model, year):


"""Initializer (constructor) method."""
# Instance attributes
self.make = make
self.model = model
self.year = year

Python 53
# Instance method
def start_engine(self):
print(f"The {self.make} {self.model}'s engine has s
tarted.")

# Instance method
def get_info(self):
return f"{self.year} {self.make} {self.model}"

Explanation:

Class Definition: We define a class named Car .

Docstring: A description of the class.

Class Attribute: wheels is a class attribute shared by all instances.

Initializer ( __init__ method): Sets up the initial state of an object.

Instance Attributes: make , model , year are specific to each instance.

Instance Methods: Functions that operate on instances of the class.

Objects (Instances)
An object is an instance of a class. It has its own data attributes and can
perform actions defined by its methods.
Creating Objects:

python
Copy code
car1 = Car("Toyota", "Corolla", 2020)
car2 = Car("Honda", "Civic", 2019)

Accessing Attributes and Methods:

python
Copy code
print(car1.get_info()) # Output: 2020 Toyota Corolla
car1.start_engine() # Output: The Toyota Corolla's

Python 54
engine has started.

print(car2.get_info()) # Output: 2019 Honda Civic


car2.start_engine() # Output: The Honda Civic's en
gine has started.

print(Car.wheels) # Output: 4
print(car1.wheels) # Output: 4

Explanation:

car1 and car2 : Instances of the Car class.

Accessing Class Attributes: Can be accessed using the class name or


instance.

Methods: Called using the dot notation (e.g., car1.start_engine() ).

Modifying Attributes
You can modify the attributes of an instance after it has been created.

python
Copy code
car1.year = 2021
print(car1.get_info()) # Output: 2021 Toyota Corolla

Explanation:

car1.year = 2021 : Updates the year attribute of car1 .

8.2 Inheritance
Inheritance allows a class to inherit attributes and methods from another class,
promoting code reusability.
Syntax:

python
Copy code
class DerivedClass(BaseClass):

Python 55
# Body of derived class

Example:

python
Copy code
class ElectricCar(Car):
"""Represents aspects of a car specific to electric veh
icles."""

def __init__(self, make, model, year, battery_size):


"""Initialize attributes of the parent class."""
super().__init__(make, model, year)
self.battery_size = battery_size # New attribute s
pecific to ElectricCar

def describe_battery(self):
"""Prints information about the battery size."""
print(f"This car has a {self.battery_size}-kWh batt
ery.")

Explanation:

ElectricCar : A subclass of Car .

super().__init__() : Calls the initializer of the parent class to inherit its


attributes.

Additional Attributes: battery_size is specific to ElectricCar .

Additional Methods: describe_battery() is specific to ElectricCar .

Creating an Instance of the Subclass:

python
Copy code
my_tesla = ElectricCar("Tesla", "Model S", 2021, 100)
print(my_tesla.get_info()) # Output: 2021 Tesla Mo
del S

Python 56
my_tesla.describe_battery() # Output: This car has
a 100-kWh battery.
my_tesla.start_engine() # Output: The Tesla Mod
el S's engine has started.

Explanation:

Inherited Methods: get_info() and start_engine() are inherited from Car .

Subclass Methods: describe_battery() is specific to ElectricCar .

Method Overriding
A subclass can override methods from the parent class.
Example:

python
Copy code
class ElectricCar(Car):
# ... (same as before)
def start_engine(self):
print(f"The {self.make} {self.model} is powered on
silently.")

python
Copy code
my_tesla.start_engine() # Output: The Tesla Model S is
powered on silently.

Explanation:

Overridden Method: start_engine() in ElectricCar replaces the one in Car .

8.3 Encapsulation
Encapsulation is the practice of keeping the internal details (data and methods)
of an object hidden from the outside world.

Python 57
Private Attributes and Methods
In Python, we use a double underscore prefix to indicate that an attribute or
method is intended to be private.
Example:

python
Copy code
class Employee:
def __init__(self, name, salary):
self.name = name
self.__salary = salary # Private attribute

def display_employee(self):
print(f"Name: {self.name}")
print(f"Salary: {self.__salary}")

def __calculate_tax(self): # Private method


return self.__salary * 0.2

Accessing Private Members:

python
Copy code
emp = Employee("John Doe", 50000)
emp.display_employee()
# Output:
# Name: John Doe
# Salary: 50000

print(emp.name) # Output: John Doe


print(emp.__salary) # AttributeError: 'Employee' ob
ject has no attribute '__salary'

Explanation:

Private Attribute: __salary cannot be accessed directly.

Python 58
Private Method: __calculate_tax() cannot be called from outside.

Name Mangling:
Python changes the name of private attributes to prevent accidental access.

python
Copy code
print(emp._Employee__salary) # Output: 50000

Note: Accessing private members via name mangling is discouraged.

Getter and Setter Methods


Provide controlled access to private attributes.
Example:

python
Copy code
class Employee:
# ... (same as before)

def get_salary(self):
return self.__salary

def set_salary(self, new_salary):


if new_salary > 0:
self.__salary = new_salary
else:
print("Invalid salary amount.")

Using Getter and Setter:

python
Copy code
print(emp.get_salary()) # Output: 50000
emp.set_salary(55000)

Python 59
print(emp.get_salary()) # Output: 55000

8.4 Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a
common superclass.
Example:

python
Copy code
class Bird:
def fly(self):
print("Birds can fly")

class Sparrow(Bird):
def fly(self):
print("Sparrow flies")

class Ostrich(Bird):
def fly(self):
print("Ostriches cannot fly")

# Function that takes a bird object


def bird_flight(bird):
bird.fly()

# Using polymorphism
sparrow = Sparrow()
ostrich = Ostrich()

bird_flight(sparrow) # Output: Sparrow flies


bird_flight(ostrich) # Output: Ostriches cannot fly

Explanation:

bird_flight() : Calls the fly() method on any bird object.

Python 60
Polymorphic Behavior: The fly() method behaves differently depending
on the object.

8.5 Abstract Classes and Interfaces


Abstract classes are classes that contain one or more abstract methods, which
are methods declared but not implemented. They cannot be instantiated and
are designed to be subclassed.

Using the abc Module


The abc module provides the infrastructure for defining abstract base classes
(ABCs).
Example:

python
Copy code
from abc import ABC, abstractmethod

class Animal(ABC):
@abstractmethod
def move(self):
pass

class Human(Animal):
def move(self):
print("Humans can walk and run")

class Snake(Animal):
def move(self):
print("Snakes can slither")

Instantiation:

python
Copy code
human = Human()
snake = Snake()

Python 61
human.move() # Output: Humans can walk and r
un
snake.move() # Output: Snakes can slither

Explanation:

Abstract Method: move() in Animal must be overridden.

Cannot Instantiate Animal : Attempting animal = Animal() will raise a


TypeError .

9. Exceptions and Error Handling


Exceptions are errors detected during execution. Exception handling ensures
that the flow of the program is not interrupted abruptly.

Try-Except Blocks
Syntax:

python
Copy code
try:
# Code that may raise an exception
except ExceptionType as e:
# Code to handle the exception
else:
# Code to execute if no exception occurs
finally:
# Code that will always execute

Example:

python
Copy code
try:
num = int(input("Enter a number: "))
result = 100 / num

Python 62
except ZeroDivisionError:
print("Error: Division by zero is undefined.")
except ValueError:
print("Error: Invalid input. Please enter a numeric val
ue.")
else:
print(f"Result: {result}")
finally:
print("Execution finished.")

Explanation:

ZeroDivisionError : Catches division by zero errors.

ValueError : Catches invalid inputs when converting to integer.

else Block: Executes if no exceptions occur.

finally Block: Always executes, useful for cleanup operations.

Raising Exceptions
You can raise exceptions to signal that an error has occurred.
Example:

python
Copy code
def calculate_square_root(x):
if x < 0:
raise ValueError("Cannot calculate square root of a
negative number.")
return x ** 0.5

try:
result = calculate_square_root(-9)
except ValueError as e:
print(e)

Output:

Python 63
css
Copy code
Cannot calculate square root of a negative number.

Custom Exceptions
Create custom exceptions by inheriting from the Exception class.
Example:

python
Copy code
class InsufficientFundsError(Exception):
"""Exception raised when attempting to withdraw more mo
ney than available."""
pass

class BankAccount:
def __init__(self, balance):
self.balance = balance

def withdraw(self, amount):


if amount > self.balance:
raise InsufficientFundsError("Insufficient fund
s in your account.")
self.balance -= amount

try:
account = BankAccount(100)
account.withdraw(150)
except InsufficientFundsError as e:
print(e)

Output:

Copy code
Insufficient funds in your account.

Python 64
10. File Handling
File handling in Python allows you to work with files for reading, writing, and
appending data.

Opening and Closing Files


Files are opened using the open() function and closed automatically when using
a with statement.
Modes:

'r' : Read (default)

'w' : Write (overwrites existing content)

'a' : Append (adds to the end)

'r+' : Read and write

'b' : Binary mode (e.g., 'rb' for reading binary)

Reading Files

Reading the Entire File

python
Copy code
with open("example.txt", "r") as file:
content = file.read()
print(content)

Explanation:

file.read() : Reads the entire file as a string.

Reading Line by Line

python
Copy code

Python 65
with open("example.txt", "r") as file:
for line in file:
print(line.strip())

Explanation:

Iteration: The file object can be iterated over line by line.

line.strip() : Removes leading and trailing whitespace.

Reading Specific Amount of Data

python
Copy code
with open("example.txt", "r") as file:
content = file.read(10)
print(content)

Explanation:

file.read(10) : Reads the first 10 characters.

Writing to Files

Writing Data

python
Copy code
with open("output.txt", "w") as file:
file.write("This is a new file.\n")
file.write("It contains some text.\n")

Explanation:

'w' Mode: Opens the file for writing, overwriting existing content.

file.write() : Writes the string to the file.

Appending Data

Python 66
python
Copy code
with open("output.txt", "a") as file:
file.write("This line is appended.\n")

Explanation:

'a' Mode: Opens the file for appending.

File Methods
file.tell() : Returns the current position in the file.

file.seek(offset, from_what) : Changes the current file position.

offset : Number of bytes to move.

from_what : Reference point ( 0 for beginning, 1 for current, 2 for end).

Example:

python
Copy code
with open("example.txt", "r") as file:
file.seek(5)
content = file.read()
print(content)

Explanation:

file.seek(5) : Moves the cursor to the 5th byte from the beginning.

11. Advanced Topics


11.1 Iterators and Generators
Iterators and generators are advanced concepts that allow for efficient looping
and data processing in Python.

Python 67
Iterators
An iterator is an object representing a stream of data; it returns data one
element at a time. Iterators implement two methods:

__iter__() : Returns the iterator object itself.

__next__() : Returns the next value from the iterator. If there are no more
items, it raises a StopIteration exception.

Example: Creating a Custom Iterator

python
Copy code
class MyIterator:
def __init__(self, limit):
self.limit = limit
self.counter = 0

def __iter__(self):
return self # Returns the iterator object itself

def __next__(self):
if self.counter < self.limit:
self.counter += 1
return self.counter
else:
raise StopIteration

# Using the custom iterator


iterator = MyIterator(5)
for number in iterator:
print(number)

Output:

Copy code
1
2
3

Python 68
4
5

Explanation:

MyIterator : A class that counts up to a specified limit.

__iter__() : Returns the iterator object.

__next__() : Returns the next number until the limit is reached.

Generators
Generators are a simple and powerful tool for creating iterators using functions
and the yield statement. They allow you to declare a function that behaves like
an iterator.
Creating a Generator Function

python
Copy code
def my_generator():
yield 1
yield 2
yield 3

# Using the generator


for value in my_generator():
print(value)

Output:

Copy code
1
2
3

Explanation:

Python 69
yield : Pauses the function and returns a value; resumes on the next call.

Generator for Fibonacci Sequence

python
Copy code
def fibonacci(n):
"""Generates Fibonacci sequence up to n terms."""
a, b = 0, 1
count = 0
while count < n:
yield a
a, b = b, a + b
count += 1

# Using the generator


for num in fibonacci(10):
print(num)

Output:

Copy code
0
1
1
2
3
5
8
13
21
34

Advantages of Generators:

Memory Efficiency: Generators compute one value at a time, which is


efficient for large datasets.

Python 70
Represent Infinite Streams: Can model sequences without predefined
limits.

11.2 Context Managers


Context managers allow you to allocate and release resources precisely when
you want to. The most widely used example is the with statement when
working with files.
Using with Statement

python
Copy code
with open('example.txt', 'r') as file:
content = file.read()
print(content)

Explanation:

with Statement: Ensures that the file is properly closed after its suite
finishes, even if an exception is raised.

Creating Custom Context Managers


You can create a context manager using a class that implements __enter__()

and __exit__() methods.


Example:

python
Copy code
class MyContextManager:
def __enter__(self):
print("Entering the context")
return self # Can return any object to be used in
the context

def __exit__(self, exc_type, exc_value, traceback):


print("Exiting the context")

# Using the custom context manager

Python 71
with MyContextManager() as manager:
print("Inside the context")

Output:

scss
Copy code
Entering the context
Inside the context
Exiting the context

Using contextlib Module


The contextlib module provides utilities for creating context managers.
Example with contextmanager Decorator:

python
Copy code
from contextlib import contextmanager

@contextmanager
def my_context():
print("Entering the context")
yield
print("Exiting the context")

with my_context():
print("Inside the context")

Explanation:

@contextmanager Decorator: Simplifies the creation of context managers using


generators.

yield Statement: The code before yield is executed on entering the


context, and the code after is executed on exiting.

Python 72
11.3 Metaprogramming
Metaprogramming involves writing code that manipulates code. In Python, this
is often done using decorators, metaclasses, and introspection.

Introspection
Introspection is the ability of a program to examine the type or properties of an
object at runtime.
Example:

python
Copy code
x = 42
print(type(x)) # Output: <class 'int'>
print(dir(x)) # Lists all attributes and meth
ods of x
print(isinstance(x, int)) # Output: True

Dynamic Attribute Access


You can access or modify an object's attributes dynamically.
Example:

python
Copy code
class Person:
pass

person = Person()
setattr(person, 'name', 'Alice')
print(getattr(person, 'name')) # Output: Alice

Metaclasses
Metaclasses are classes of classes; they define how classes behave.
Example: Custom Metaclass

Python 73
python
Copy code
class MyMeta(type):
def __new__(cls, name, bases, attrs):
attrs['id'] = 100
return super(MyMeta, cls).__new__(cls, name, bases,
attrs)

class MyClass(metaclass=MyMeta):
pass

print(MyClass.id) # Output: 100

Explanation:

MyMeta : A metaclass that adds an id attribute to classes.

metaclass Parameter: Specifies the metaclass for MyClass .

11.4 Concurrency and Parallelism


Concurrency and parallelism enable your programs to perform multiple tasks
simultaneously, improving performance for I/O-bound and CPU-bound tasks.

Threading
Used for concurrent execution in I/O-bound tasks.
Example:

python
Copy code
import threading

def worker(num):
"""Thread worker function."""
print(f'Worker {num}')

threads = []
for i in range(5):

Python 74
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()

for t in threads:
t.join()

Explanation:

threading.Thread : Creates a new thread.

t.start() : Starts the thread.

t.join() : Waits for the thread to finish.

Limitations:

Global Interpreter Lock (GIL): Only one thread executes Python bytecode
at a time, limiting CPU-bound threading benefits.

Multiprocessing
Used for parallel execution in CPU-bound tasks by creating separate
processes.
Example:

python
Copy code
import multiprocessing

def worker(num):
"""Process worker function."""
print(f'Worker {num}')

if __name__ == '__main__':
processes = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=
(i,))
processes.append(p)
p.start()

Python 75
for p in processes:
p.join()

Explanation:

multiprocessing.Process : Creates a new process.

No GIL Limitation: Each process has its own Python interpreter and
memory space.

11.5 Asynchronous Programming


Asynchronous programming allows for non-blocking execution, useful for high-
performance network and web applications.

Asyncio Module
Introduced in Python 3.4, asyncio provides infrastructure for writing single-
threaded concurrent code.
Example:

python
Copy code
import asyncio

async def say_after(delay, message):


await asyncio.sleep(delay)
print(message)

async def main():


await asyncio.gather(
say_after(1, 'Hello'),
say_after(2, 'World')
)

asyncio.run(main())

Output:

Python 76
Copy code
Hello
World

Explanation:

async def : Defines an asynchronous function (coroutine).

await : Suspends execution until the awaited task is complete.

asyncio.run() : Runs the event loop.

asyncio.gather() : Runs multiple coroutines concurrently.

12. Regular Expressions


Regular expressions (regex) are patterns used to match character combinations
in strings. In Python, the re module provides support for regex.

Basic Syntax
Literal Characters: Match themselves.

Metacharacters: Characters with special meanings ( . ^ $ * + ? { } \ | ( ) ).

Example:

python
Copy code
import re

pattern = r'hello'
text = 'hello world'

match = re.match(pattern, text)


if match:
print("Match found!")
else:
print("No match.")

Python 77
Output:

sql
Copy code
Match found!

Common Functions
re.match() : Checks for a match at the beginning of the string.

re.search() : Searches the string for a match anywhere.

re.findall() : Returns all non-overlapping matches.

re.sub() : Replaces matches with a string.

Example: Validating an Email Address

python
Copy code
email = 'user@example.com'
pattern = r'^\w+@\w+\.\w+$'

if re.match(pattern, email):
print("Valid email")
else:
print("Invalid email")

Explanation:

^ and $ : Start and end of the string.

\w+ : One or more word characters.

@ and \. : Literal @ and . characters.

13. Working with Databases


Python provides several ways to interact with databases, including SQLite
(built-in), PostgreSQL, MySQL, and others through external libraries.

Python 78
SQLite with sqlite3 Module
SQLite is a lightweight, disk-based database.
Example:

python
Copy code
import sqlite3

# Connect to database (creates if it doesn't exist)


conn = sqlite3.connect('example.db')
cursor = conn.cursor()

# Create a table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER
)
''')

# Insert data
cursor.execute('INSERT INTO users (name, age) VALUES (?,
?)', ('Alice', 30))
conn.commit()

# Query data
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
for row in rows:
print(row)

# Close connection
conn.close()

Output:

Python 79
arduino
Copy code
(1, 'Alice', 30)

Explanation:

? Placeholders: Prevent SQL injection by using parameterized queries.

cursor.fetchall() : Retrieves all rows from the query.

Using SQLAlchemy ORM


SQLAlchemy is a powerful ORM (Object-Relational Mapping) library.
Example:

python
Copy code
from sqlalchemy import create_engine, Column, Integer, Stri
ng
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# Create an engine
engine = create_engine('sqlite:///example.db')
Base = declarative_base()

# Define a model
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)

# Create tables
Base.metadata.create_all(engine)

# Create a session
Session = sessionmaker(bind=engine)

Python 80
session = Session()

# Add a new user


new_user = User(name='Bob', age=25)
session.add(new_user)
session.commit()

# Query users
users = session.query(User).all()
for user in users:
print(user.name, user.age)

Output:

Copy code
Alice 30
Bob 25

14. Networking and Web Programming


Python provides libraries for networking and building web applications.

HTTP Requests with requests Library


The requests library simplifies HTTP requests.
Installation:

bash
Copy code
pip install requests

Example:

python
Copy code

Python 81
import requests

response = requests.get('https://api.github.com')
print(response.status_code) # Output: 200
print(response.headers['content-type'])
print(response.json()) # Parses the response as JSON

Explanation:

requests.get() : Sends a GET request.

response.status_code : HTTP status code of the response.

response.json() : Parses JSON response to a Python dictionary.

Building Web Applications with Flask


Flask is a lightweight web application framework.
Installation:

bash
Copy code
pip install Flask

Basic Flask Application:

python
Copy code
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/')
def home():
return "Welcome to the Flask app!"

@app.route('/add', methods=['POST'])
def add():
data = request.get_json()

Python 82
result = data['a'] + data['b']
return jsonify({'result': result})

if __name__ == '__main__':
app.run(debug=True)

Explanation:

Routes ( @app.route ): Define URL endpoints.

request.get_json() : Retrieves JSON data from a POST request.

jsonify() : Converts a dictionary to a JSON response.

Running the Application:

bash
Copy code
python app.py

15. Testing and Debugging


Testing ensures your code works as expected, and debugging helps identify
and fix issues.

Unit Testing with unittest


The unittest module provides a framework for writing test cases.
Example:

python
Copy code
import unittest

def add(a, b):


return a + b

class TestMathFunctions(unittest.TestCase):
def test_add(self):

Python 83
self.assertEqual(add(5, 3), 8)
self.assertEqual(add(-1, 1), 0)
self.assertNotEqual(add(2, 2), 5)

if __name__ == '__main__':
unittest.main()

Running Tests:

bash
Copy code
python test_math_functions.py

Explanation:

unittest.TestCase : Base class for test cases.

Assertions: Methods like assertEqual , assertNotEqual to check conditions.

Debugging with pdb


The pdb module provides an interactive debugging environment.
Example:

python
Copy code
import pdb

def buggy_function():
x = 10
y = 0
pdb.set_trace() # Sets a breakpoint
result = x / y
return result

buggy_function()

Python 84
Using pdb :

n : Next line.

s : Step into function.

c : Continue execution.

q : Quit debugger.

16. Scientific Computing and Data Science


Python is widely used in data science due to libraries like NumPy, Pandas, and
Matplotlib.

NumPy
NumPy provides support for large, multi-dimensional arrays and matrices.
Installation:

bash
Copy code
pip install numpy

Example:

python
Copy code
import numpy as np

# Creating arrays
a = np.array([1, 2, 3])
b = np.array([[1, 2], [3, 4]])

# Arithmetic operations
c = a * 2 # Output: array([2, 4, 6])
d = b + 3 # Output: array([[4, 5], [6, 7]])

# Array operations
e = np.dot(a, a) # Dot product

Python 85
f = np.mean(b) # Mean of array elements

Pandas
Pandas provides data structures for data manipulation and analysis.
Installation:

bash
Copy code
pip install pandas

Example:

python
Copy code
import pandas as pd

# Creating a DataFrame
data = {'Name': ['Alice', 'Bob'], 'Age': [30, 25]}
df = pd.DataFrame(data)

# Reading from CSV


df = pd.read_csv('data.csv')

# Data exploration
print(df.head()) # First 5 rows
print(df.describe()) # Statistical summary

# Data manipulation
df['Age'] += 1 # Increase age by 1
df['Country'] = 'USA' # Add a new column

Matplotlib

Python 86
Matplotlib is a plotting library for creating static, animated, and interactive
visualizations.
Installation:

bash
Copy code
pip install matplotlib

Example:

python
Copy code
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# Plotting
plt.plot(x, y, marker='o')

# Adding labels and title


plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Plot')

# Display the plot


plt.show()

17. Best Practices and Coding Standards


Following best practices ensures code quality, readability, and maintainability.

PEP 8 Style Guide


PEP 8 is the official Python style guide.

Python 87
Key Points:
Indentation: Use 4 spaces per indentation level.

Maximum Line Length: Limit lines to 79 characters.

Blank Lines: Use blank lines to separate functions and classes.

Imports: Place imports at the top of the file.

Naming Conventions:

Functions and Variables: lowercase_with_underscores

Classes: CapWords (PascalCase)

Constants: UPPERCASE_WITH_UNDERSCORES

Writing Readable Code


Comments: Explain the why, not the what.

Docstrings: Use triple quotes to describe modules, classes, and functions.

Example:

python
Copy code
def calculate_area(radius):
"""Calculate the area of a circle given its radius."""
import math
return math.pi * radius ** 2

Version Control with Git


Version control systems like Git help track changes and collaborate.
Basic Commands:

Initialize Repository:

bash
Copy code
git init

Python 88
Adding Files:

bash
Copy code
git add filename.py

Committing Changes:

bash
Copy code
git commit -m "Commit message"

Pushing to Remote Repository:

bash
Copy code
git push origin main

18. Additional Resources


Official Python Documentation: docs.python.org

Books:

Automate the Boring Stuff with Python by Al Sweigart

Python Crash Course by Eric Matthes

Online Tutorials:

Real Python

W3Schools Python Tutorial

Interactive Coding Platforms:

Codecademy

LeetCode

Python 89
HackerRank

19. Conclusion
Congratulations on completing this comprehensive guide to Python
programming! We've covered a wide range of topics, from basic syntax and
data types to advanced concepts like metaprogramming and asynchronous
programming.
Key Takeaways:

Foundational Knowledge: Understanding variables, data types, and control


flow is crucial.

Data Structures: Mastery of lists, tuples, sets, and dictionaries enhances


data manipulation capabilities.

Object-Oriented Programming: Classes and objects help model real-world


entities.

Advanced Concepts: Iterators, generators, context managers, and


concurrency provide powerful tools for efficient programming.

Practical Skills: File handling, regular expressions, database interactions,


and web programming are essential for real-world applications.

Best Practices: Following coding standards and writing clean, readable


code is vital for collaboration and maintenance.

Next Steps:

Practice: Implement projects to apply what you've learned.

Explore Libraries: Dive deeper into Python's extensive libraries and


frameworks.

Stay Updated: Keep learning about new Python features and updates.

Python 90

You might also like