Python Command Notes
Python Command Notes
Table of Contents
1. Introduction to Python
3.3 Operators
4. Control Flow
4.2 Loops
5. Functions
Python 1
5.4 Decorators
7. Data Structures
7.1 Lists
7.2 Tuples
7.3 Sets
7.4 Dictionaries
8.2 Inheritance
8.3 Encapsulation
8.4 Polymorphism
11.3 Metaprogramming
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.
Python 3
Visit the official Python website and download the latest version
compatible with your operating system (Windows, macOS, or Linux).
2. Install Python:
3. Verify Installation:
1. Create a File:
python
Copy code
print("Hello, World!")
Python 4
3. Save and Run the Program:
Explanation:
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:
Python 5
height : A float variable assigned the value 5.6 .
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:
python
Copy code
count = 100
python
Copy code
temperature = 98.6
python
Copy code
Python 6
z = 3 + 5j
2. String ( str ):
python
Copy code
message = "Hello, World!"
3. Boolean ( bool ):
python
Copy code
is_active = False
4. NoneType:
python
Copy code
result = None
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:
python
Copy code
# String to integer
number = int("10") # Output: 10
# Integer to string
text = str(10) # Output: '10'
# String to float
decimal_number = float("3.14") # Output: 3.14
python
Copy code
print("Welcome to Python!")
print("Name:", name)
Python 8
print("Age:", age)
Explanation:
python
Copy code
print(f"Hello, {name}! You are {age} years old.")
Explanation:
python
Copy code
user_name = input("Enter your name: ")
print(f"Hello, {user_name}!")
Explanation:
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:
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 ( // ):
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
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
python
Copy code
x > y
python
Copy code
x < y
python
Copy code
x >= y
python
Copy code
Python 14
x <= y
Example:
python
Copy code
score = 85
Logical Operators
Used to combine conditional statements.
python
Copy code
if x > 0 and x < 10:
print("x is between 1 and 9")
2. Logical OR ( or ):
python
Copy code
if x < 0 or x > 100:
Python 15
print("x is out of range")
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")
python
Copy code
a = [1, 2, 3]
Python 16
b = a
c = [1, 2, 3]
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.
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
Explanation:
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: "))
Explanation:
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: "))
Explanation:
Conditions are checked in order; the first condition that evaluates to True
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:
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
Python 21
Explanation:
Initializes count to 1 .
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']
Explanation:
Python 22
python
Copy code
range(start, stop, step)
Example:
python
Copy code
for i in range(5):
print(i)
Output:
Copy code
0
1
2
3
4
Explanation:
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:
Python 24
1. Break Statement:
Example:
python
Copy code
for num in range(1, 10):
if num == 5:
break
print(num)
Output:
Copy code
1
2
3
4
Explanation:
2. Continue Statement:
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:
Example:
python
Copy code
for letter in 'Python':
if letter == 'h':
pass # Placeholder for future code
print('Current Letter:', letter)
Explanation:
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:
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:
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:
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.
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
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!
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?")
Explanation:
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:
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
Explanation:
Keyword Arguments
Arguments can be passed using the parameter names.
Example:
python
Copy code
print(subtract(b=5, a=10)) # Output: 5
Explanation:
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
Explanation:
Variable-Length Arguments
Allows a function to accept an arbitrary number of arguments.
python
Copy code
def sum_all(*args):
"""Returns the sum of all arguments."""
total = 0
for num in args:
total += num
return total
Explanation:
Python 32
You can iterate over args to process each argument.
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}")
Output:
vbnet
Copy code
name: Alice
age: 30
city: New York
Explanation:
Order of Arguments
When combining different types of arguments, the order must be:
2. Default arguments
3. args
Python 33
4. *kwargs
Example:
python
Copy code
def func(a, b=2, *args, **kwargs):
pass
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:
Python 34
Lambda functions are often used for short, simple functions.
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:
With filter() :
python
Copy code
even_numbers = list(filter(lambda x: x % 2 == 0, number
s))
print(even_numbers) # Output: [2, 4]
Explanation:
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:
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:
python
Copy code
display = decorator_function(display)
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.
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}"
Explanation:
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
Explanation:
Python 39
Decorators are applied from the bottom up ( italic_decorator first, then
bold_decorator ).
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:
python
Copy code
from my_module import greet, pi
Explanation:
Now you can use greet and pi directly without the module prefix.
python
Copy code
import my_module as mm
Explanation:
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.
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
Explanation:
You can import modules from a package using from package import module .
python
Copy code
from my_package.module1 import module1_func
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
def module2_func():
module1_func()
Python 43
print("This is module2 function")
Explanation:
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]
# 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 .
Slicing:
python
Copy code
sub_list = numbers[1:4] # Output: [2, 3, 4]
Explanation:
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)
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)
Accessing Elements
Same as lists, using indexing and slicing.
Python 47
python
Copy code
x = coordinates[0] # Output: 10
Tuple Unpacking
You can assign tuple elements to variables.
python
Copy code
x, y = coordinates
print(x) # Output: 10
print(y) # Output: 20
Explanation:
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()
Python 48
print(unique_numbers) # Output: {1, 2, 3}
Explanation:
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 = {}
Accessing Values
python
Copy code
print(person["name"]) # Output: Alice
Explanation:
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
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.
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."""
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:
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)
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(Car.wheels) # Output: 4
print(car1.wheels) # Output: 4
Explanation:
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:
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 describe_battery(self):
"""Prints information about the battery size."""
print(f"This car has a {self.battery_size}-kWh batt
ery.")
Explanation:
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:
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:
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}")
python
Copy code
emp = Employee("John Doe", 50000)
emp.display_employee()
# Output:
# Name: John Doe
# Salary: 50000
Explanation:
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
python
Copy code
class Employee:
# ... (same as before)
def get_salary(self):
return self.__salary
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")
# Using polymorphism
sparrow = Sparrow()
ostrich = Ostrich()
Explanation:
Python 60
Polymorphic Behavior: The fly() method behaves differently depending
on the object.
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:
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:
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
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.
Reading Files
python
Copy code
with open("example.txt", "r") as file:
content = file.read()
print(content)
Explanation:
python
Copy code
Python 65
with open("example.txt", "r") as file:
for line in file:
print(line.strip())
Explanation:
python
Copy code
with open("example.txt", "r") as file:
content = file.read(10)
print(content)
Explanation:
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.
Appending Data
Python 66
python
Copy code
with open("output.txt", "a") as file:
file.write("This line is appended.\n")
Explanation:
File Methods
file.tell() : Returns the current position in the file.
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.
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:
__next__() : Returns the next value from the iterator. If there are no more
items, it raises a StopIteration exception.
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
Output:
Copy code
1
2
3
Python 68
4
5
Explanation:
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
Output:
Copy code
1
2
3
Explanation:
Python 69
yield : Pauses the function and returns a value; resumes on the next call.
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
Output:
Copy code
0
1
1
2
3
5
8
13
21
34
Advantages of Generators:
Python 70
Represent Infinite Streams: Can model sequences without predefined
limits.
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.
python
Copy code
class MyContextManager:
def __enter__(self):
print("Entering the context")
return self # Can return any object to be used in
the context
Python 71
with MyContextManager() as manager:
print("Inside the context")
Output:
scss
Copy code
Entering the context
Inside the context
Exiting the context
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:
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
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
Explanation:
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:
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:
No GIL Limitation: Each process has its own Python interpreter and
memory space.
Asyncio Module
Introduced in Python 3.4, asyncio provides infrastructure for writing single-
threaded concurrent code.
Example:
python
Copy code
import asyncio
asyncio.run(main())
Output:
Python 76
Copy code
Hello
World
Explanation:
Basic Syntax
Literal Characters: Match themselves.
Example:
python
Copy code
import re
pattern = r'hello'
text = 'hello world'
Python 77
Output:
sql
Copy code
Match found!
Common Functions
re.match() : Checks for a match at the beginning of the string.
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:
Python 78
SQLite with sqlite3 Module
SQLite is a lightweight, disk-based database.
Example:
python
Copy code
import sqlite3
# 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:
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()
# Query users
users = session.query(User).all()
for user in users:
print(user.name, user.age)
Output:
Copy code
Alice 30
Bob 25
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:
bash
Copy code
pip install Flask
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:
bash
Copy code
python app.py
python
Copy code
import unittest
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:
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.
c : Continue execution.
q : Quit debugger.
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)
# 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')
Python 87
Key Points:
Indentation: Use 4 spaces per indentation level.
Naming Conventions:
Constants: UPPERCASE_WITH_UNDERSCORES
Example:
python
Copy code
def calculate_area(radius):
"""Calculate the area of a circle given its radius."""
import math
return math.pi * radius ** 2
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"
bash
Copy code
git push origin main
Books:
Online Tutorials:
Real Python
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:
Next Steps:
Stay Updated: Keep learning about new Python features and updates.
Python 90