You are on page 1of 51

Basics of Python Programming

Operators: Symbols or special characters that perform operations on one or more


operands.

List of Commonly-used Operators:

1. Arithmetic Operators: Used to perform mathematical operations like addition,


subtraction, multiplication, division, modulo, and exponentiation.

# Arithmetic Operators
a = 10
b = 3
c = a + b # Addition
d = a – b # Subtraction
e = a * b # Multiplication
f = a / b # Division
g = a // b # Floor division (quotient/integer, rounded down)
h = a % b # Modulo
i = a ** b # Exponentiation

2. Comparison Operators: Used to compare two values and return a Boolean


value of ‘True’ or ‘False’

# Comparison Operators
a = 10
b = 3
c = a == b # Equal to
d = a != b # Not equal to
e = a < b # Less than
f = a > b # Greater than
g = a <= b # Less than or equal to
h = a >= b # Greater than or equal to

3. Logical Operators: Used to combine multiple conditions and return a Boolean


value of ‘True’ or ‘False’

# Logical Operators
a = 10
b = 3
c = (a > 5) and (b < 7) # Logical AND
d = (a > 5) or (b > 7) # Logical OR
e = not (a > b) # Logical NOT
4. Assignment Operators: Used to assign values to variables.

# Assignment Operators
a = 10 # Simple assignment
b += 3 # Add and assign
c -= 5 # Subtract and assign
d *= 2 # Multiply and assign
e /= 4 # Divide and assign
e //= 4 # Floor divide and assign
g %= 2 # Modulo and assign
h **= 2 # Exponentiate and assign

5. Bitwise Operators: Perform operations on individual bits of binary numbers.


These operators work on binary representation of numbers and perform bit-
by-bi operations.

# Bitwise Operators
a & b # Bitwise AND
a | b # Bitwise OR
a ^ b # Bitwise XOR
~a # Bitwise NOT
a << b # Bitwise left shift
a >> b # Bitwise right shift

6. Identity Operators: Used to compare the memory location of two objects.

# Identity Operators
a = [1, 2, 3]
b = a
c = [1, 2, 3]

d = (a is b) # Returns True if both variables are the same


object i.e. Returns True if a is the same object as b
e = (a is not ) # Returns True if both variables are not the
same object i.e. if a is not the same object as b

7. Membership Operators: Used to check if a value is present in a sequence.

# Membership Operators
a = [1, 2, 3]
b = a
c = [1, 2, 3]

d = (2 in a) # Returns True if a sequence with the


specified value is present in the object (Example with number)

a in b # Returns True if a is a member of b

e = (2 not in a) # Returns True if a sequence with the


specified variables is not present in the object (Example with
number)

a not in b # Returns True if a is not a member of b

Operands: Values or variables that operators act upon.

List of Operands

# Built-in Functions

# Numeric literals
x = 10 # Integers
y = -5 # Integers
x = 3.14 # Floats
y = 2.0 # Floats
x = 1e-10 # Floats

# Strings
name = “John”
y = “Hello, world!”
“””multi-line string”””

# Variables
x
y
result

# Booleans
is_raining = True
is_sunny = False

# Lists (A data structure)


my_list = [1, 2, 3]

# Tuples (A data structure)


my_tuple = (5, 6, 7, 8)

# Sets
my_set = {9, 10, 11, 12}
# Dictionaries (A data structure)
my_dict = {“key1”: “value1”, “key2”: “value2”, “Age”: 30}

# None
nothing = None

# Range object
range(5)

# Lambda function
lambda x: x + 1

# Functions
print() # Input and Output Functions
input()

abs() # Numeric Functions


pow()
round()
min()
max()

len() # String Functions


upper()
lower()
capitalize()
replace()
split()
join()
ord(c)
chr(i)
len(s)
str(object=’’)
bytes([source[, encoding[, errors]]])
format(value[, format_spec])
ascii(object)
eval(expression)
bytearray([source[, encoding[, errors]]])
locals()
globals()

len() # List Functions


append()
extend()
insert()
remove()
pop()
sort()
reverse()
all(iterable)
any()
enumerate(iterable, start=0)
max(iterable)
min(iterable)
sorted(iterable, *, key=None, reverse=False)
sum(iterable[, start])

len() # Dictionary Functions


keys()
values()
get()
pop()
popitem()
dict()
dict(mapping)
dict(iterable)
len(d)
sorted(d, *, key=None, reverse=False)
all([False, True, 1])
any([False, True, 1])
zip(*iterables)

if # Conditional Statements Functions


elif
else

for # Loops Functions


range()
break
continue

open() # File Handling Functions


read()
write()
close()

abs(x) # Numeric Functions


divmod(x, y)
pow()
round(number, ndigits)

dir([object]) # Miscellaneous Functions


help([object])
id(object)
type(object)
isinstance(object, classinfo)

bin() # Type conversion Functions


bool()
chr()
float()
hex()
int()
oct()
str()

enumerate() # Iteration Functions


filter()
iter()
map()
next()
range()
reversed()
slice()
sorted()
zip()

dir() # Object introspection Functions


getattr()
hasattr()
isinstance()
id()
type()

assert() # Error handling Functions


raise()

# Methods
‘hello’.upper()
[1, 2, 3].upper(4)

append() - # adds an element to the end of a list


my_list.append(element)

clear() - # removes all elements from a list


my_list.clear()

copy() - # returns a shallow copy of a list


new_list = my_list.copy()

count() - # returns the number of occurrences of an element in


a list
count = my_list.count(element)

extend() - # adds elements from one list to another list


my_list.extend(other_list)

index() - # returns the index of the first occurrence of an


element in a list
index = my_list.index(element)
insert() - # inserts an element at a specified position in a
list
my_list.insert(position, element)

pop() - # removes and returns the last element of a list, or


an element at a specified index
last_element = my_list.pop()
element = my_list.pop(index)

remove() - # removes the first occurrence of an element from a


list
my_list.remove(element)

reverse() - # reverses the order of the elements in a list


my_list.reverse()

sort() - # sorts the elements in a list


my_list.sort()

Functions: Named blocks of code that perform a specific task. They are defined
using the ‘def’ keyword followed by the function name and any parameters e.g.
(a, b) that the function takes.

def add(a, b):


return a + b

# Numeric Functions
# Built-in Functions

abs()
round()
max()
min()
pow()
divmod()
sum()
bin()
oct()
hex()

#External Library Functions


math.ceil()
math.floor()
math.fabs()
math.factorial()
math.isclose()
math.isfinite()
math.isnan()
math.modf()
math.trunc()
numpy.ceil()
numpy.floor()
numpy.round()
numpy.exp()
numpy.log()
numpy.log10()
numpy.sqrt()

#Lambda Functions
lambda x: x**2
lambda x: x, y: x + y
lambda x: x.strip()

# String Functions
# Built-in Functions
len()
str()
format()
ord()
chr()
capitalize()
casefold()
center()
count()
endswith()
find()
index()
isalnum()
isalpha()
isdecimal()
isdigit()
isidentifier()
islower()
isnumeric()
isprintable()
isspace()
istitle()
isupper()
join()
ljust()
lower()
lstrip()
replace()
rfind()
rindex()
rjust()
rstrip()
split()
splitlines()
startswith()
strip()
swapcase()
title()
upper()
zfill()

#Lambda Functions
lambda x: x.upper()
lambda x: x.lower()
lambda x: x.strip()

# External Library Functions


re.match()
re.search()
re.findall()
re.finditer()
re.sub()
re.split()

# List Functions
list()
len()
max()
min()
sum()
sorted()
reversed()
enumerate()
zip()
all()
any()
filter()
map()

# Lambda Functions
lambda x: len(x)
lambda x: sorted(x)
lambda x: reversed(x)
lambda x: [i for i in x if i % 2 == 0]
lambda x: [i**2 for i in x]

# External Library Functions


numpy.array()
numpy.arange()
numpy.linspace()
numpy.sort()
numpy.concatenate()
numpy.split()

# Dictionary Functions
# Built-in Functions
dict()
len()
keys()
values()
items()
clear()
copy()
fromkeys()
get()
pop()
popitem()
setdefault()
update()

# Lambda Functions
lambda x: len(x)
lambda x: sorted(x.keys())
lambda x: sorted(x.values())
lambda x: {k: v for k, v in x.items() if v > 0)

# External Library Functions


collections.defaultdict()

# File Input/Output (I/O) Functions


# Built-in Functions
open()
close()
read()
readline()
readlines()
write()
writelines()

# External Library Functions


os.chdir()
os.getcwd()
os.listdir()
os.mkdir()
os.remove()
os.rename()
shutil.copy()
shutil.move()

# No Lambda Functions included

# Functional Programming Functions


# Built-in Functions
map()
filter()
reduce()
# External Library Functions
functools.partial()

# Object Introspection Functions


# Built-in Functions
dir()
vars()
locals()
globals()
hasattr()
getattr()
setattr()
delattr()
type()
id()
isinstance()
issubclass()
callable()

# External Library Functions


inspect.getmembers()
inspect.getfile()
inspect.getmodule()

# No Lambda Functions included

# Iteration Functions
# Built-in Functions
range()
iter()
next()
reversed()

# External Library Functions


itertools.combinations()
itertools.permutations()
itertools.product()
itertools.count()
itertools.islice()
itertools.takewhile()
itertools.dropwhile()

# Lambda Functions
lambda x: x**2
lambda x: x.upper()

# Error Handling Functions


# Built-in Functions
try:
pass
except Exception as e:
pass
finally:
raise()
assert()

# External Library Functions


None
logging.debug()
logging.info()
logging.warning()
logging.error()
logging.critical()

# No Lambda Functions included

# Type Conversion Functions


# Built-in Functions
int()
float()
bool()
str()
list()
tuple()
set()
bool()
dict()

# External Library Functions


numpy.array()
pandas.DataFrame()

# No Lambda Functions included

# Loops Functions
# Built-in Functions
for item in iterable
pass
while condition:
pass
range()
enumerate()

# Lambda Functions
lambda x: [i for i in x if i % 2 == 0]
lambda x: [i**2 for i in x]
lambda x: [(i, i**2 for i in x]

# External Library Functions


itertools.cycle()
itertools.repeat()
itertools.chain()

# File Handling Functions


# Built-in Functions
open()
close()
read()
write()
seek()
tell()

# External Library Functions


os.chdir()
os.getcwd()
os.listdir()
os.mkdir()
os.rmdir()
os.remove()
os.rename()
shutil.move()
shutil.copy()

# Miscellaneous Functions
# Built-in Functions
print()
input()
globals()
locals()
zip()
sorted()
reversed()
hash()
sum()
sum()
len()

# External Library Functions


datetime.datetime()
math.pi()

# No Lambda Functions included

# Conditional Statements Functions


# Built-in Functions
if condition:
pass
elif condition:
pass
else:
pass
# Lambda Functions
lambda x: ’even’ if x % 2 == 0 else ’odd’]

# External Library Functions


None

# Concurrency Functions
# Built-in Functions
threading.Thread()
threading.Lock()
threading.RLock()
threading.Condition()
threading.Event()
threading.Semaphore()
threading.Barrier()
queue.Queue()
queue.LifoQueue()
queue.PriorityQueue()

import threading # Threading module


t = threading.Thread(target=my_function) # Create a new thread
t.start() # Start the thread
t.join() # Wait for the thread to finish
from multiprocessing import Process # Multiprocessing module
p = Process(target=my_function) # Create a new process
p.start() # Start the process
p.join() # Wait for the process to finish

import asyncio # ‘asyncio.run()’: This function runs a


coroutine and returns the result.

async def my_coroutine():


print(“Coroutine started”)
await asyncio.sleep(1)
print(“Coroutine ended”)

asyncio.run(my_coroutine())

import threading # ‘threading.Thread()’: This function creates


a new thread to run a function.

def my_function():
print(“Function started”)
for i in range(5):
print(f”Function: {i}”)
print(“Function ended”)

my_thread = threading.Thread(target=my_function)
my_thread.start()
import multiprocessing # ‘multiprocessing.Process()’: This
function creates a new process to run a function.

def my_function():
print(“Function started”)
for i in range(5):
print(f”Function: {i}”)
print(“Function ended”)

my_process = multiprocessing.Process(target=my_function)
my_process.start()

import concurrent.futures #
‘concurrent.futures.ProcessPollExecutor()’: This function
creates a pool of processes to execute functions.

def my_function():
print(“Function started”)
for i in range(5):
print(f”Function: {i}”)
print(“Function ended”)
return x * 2

with concurrent.futures.ProcessPoolExecutor() as executor:


results = [executor.submit(my_function, i) for i in
range(4)]
for future in concurrent.futures.as_completed(results):
print(future.result())

# External Library Functions


concurrent.futures.ThreadPoolExecutor()
concurrent.futures.ProcessPoolExecutor()
concurrent.futures.as_completed()
concurrent.futures.wait()
multiprocessing.Process()
multiprocessing.Pool()
asyncio.run()
asyncio.create_task()
asyncio.gather()

import threading # Threading library

def thread_function(name):
print(“Thread”, name)

x = threading.Thread(target=thread_function, args=(“Thread-
1”,))
x.start()

import multiprocessing # Multiprocessing library


def thread_function(name):
print(“Process”, name)
p = multiprocessing.Process(target=process_function,
args=(“Process-1”,))
p.start()

import asyncio # AsyncIO library

import concurrent.futures # Concurrent.futures library

from joblib import Parallel, delayed # Joblib library

# Lambda Functions
import concurrent.futures

def square(x): # Define a function to square a number


return x ** 2

inputs = [1, 2, 3, 4, 5] # Use a ThreadPoolExecutor to run the


square function on multiple inputs concurrently
with concurrent.futures.ThreadPoolExecutor() as executor;
results = list(executor.map(lambda x: square(x), inputs))

print(results) # Output: [1, 4, 9, 16, 25]

# Decorator Functions
# Built-in Functions
staticmethod()
classmethod()
property()
@staticmethod
@classmethod
@property
@abstractmethod

def my_function(): # Function to be decorated


print(“Hello, World!”)

def my_function(): # Decorator function


def wrapper():
print(“Before the function is called.”)
func()
print(“After the function is called.”)
return wrapper

my_function = my_decorator(my_function) # Decorating the


function
my_function() # Calling the decorated function

@my_decorator # Function to be decorated


def my_function():
print(“Hello, World!”)

my_function() # Calling the decorated function

# External Library Functions


functools.lru_cache()
functools.partial()
@functools.wraps()
@functools.contextmanager()

From flask import Flask, jsonify, request # Flask library

app = Flask(__name__)

@app.route(‘/’)
def hello():
return ‘Hello, World!’

from django.views.decorators.csrf import csrf_exempt # Django


library

@csrf_exempt
def my_view(request):
return HttpResponse(‘Hello, World!’)

import wrap

@wrap.decorator
def my_decorator(wrapped, instance, args, kwargs):
# code to execute before the function call
result = wrapped(*args, **kwargs)
# code to execute after the function call
return result

from loguru import logger

@logger.catch
def my_function():
# code to execute
pass

from memoization import cached

@cached(ttl=60)
def my_function():
# code to execute
pass

from ratelimit import limits, sleep_and_retry

@sleep_and_retry
@limits(calls=10, period=60)
def my_function():
# code to execute
pass

# Lambda Functions
def log_function_calls(func): # A decorator function that logs
the function call
def wrapper(*args, **kwargs)
print(f”Calling function {func.__name__}”)
return func(*args, **kwargs)
return wrapper

add_one = lambda x: x + 1 # A lambda function that adds 1 to a


number

decorated_add_one = log_function_calls(add_one) # Using the


log_function_calls decorator with the add_one lambda function

print(decorated_add_one(5)) # Calling the decorated_add_one


function

# Output
Calling function <lambda>
6

# Networking Functions
# Built-in Functions
socket.socket()
socket.create_connection()
socket.AF_INET()
socket.SOCK_STREAM()
socket.gethostmame()
socket.gethostbyname()
socket.gethostbyaddr()
socket.ntohs()
socket.ntohl()

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #
Creating a socket

s.connect((‘www.example.com’,80)) # Connecting to a server

s.send(b’GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n’) #


Sending data to a server

data = s.recv(1024) # Receiving data from a server

s.close() # Closing the connection


import requests

response = requests.get(‘https://www.example.com’) # HTTP


client library

import paramiko

client = paramiko.SSHClient() # SSH library


client.load_system_host_keys()
client.connect(‘hostname’, username=’user’,
password=’password’)

import pycurl

c = pycurl.Curl() # Library for transferring data using


various protocols, such as HTTP, FTP, etc.
c.setopt(c.URL, ‘https://www.example.com’)
c.perform()
c.close()

- requests.get()
- requests.post()
- requests.put()
- requests.delete()
- urllib.request.urlopen()
- httplib2.Http()
- http.client.HTTPConnection()
- http.client.HTTPSConnection()
- ftplib.FTP()
- paramiko.SSHClient()

# External Library Functions


requests.get()
requests.post()
requests.put()
requests.delete()
urllib.request.urlopen()

import requests

response = request.get(‘https://www.example.com’) # HTTP


client library for making requests

import aiohttp

aysnc with aiohttp.ClientSession() as session: #Async version


of HTTP client library for making requests
async with session.get(‘https://www.example.com) as
response:
print(await response.text())
import smtplib

server = smtplib.SMTP(‘smtp.gmail.com, 587) # Library for


sending email messages
server.starttls()
sever.login(‘myemail@gmail.com’, ‘mypassword’)
sever.login(‘myemail@gmail.com’, ‘recepient@example.com’,
‘Hello, this is a test message’)
server.quit

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((‘www.google.com’, 80))
s.sendall(b’GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n’)
response = s.recv(4096)
s.close()

import paramiko

ssh = paramiko.SSHClient() # SSH library for secure remote


command execution and file transfer
ssh.net_missing)host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘example.com’, username=‘myusername’,
password=’mypassword’)
stdin, stdout, stderr = ssh.exec_command(‘Is’)
print(stdout,read())
ssh.close()

# Lambda Functions
import asyncio

loop = asyncio.get_event_loop()

async def async_lambda():


print(‘This is an async lambda function’)

async_lambda_call = lambda:
loop.run_until_complete(async_lambda())

async_lambda_call()

import socket

tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


tcp_server.setsockopt.(socket.SOL_SOCKET, socket.SO_REUSEADDR,
1)
tcp.server.bind((‘localhost’, 8000))
tcp.server.listen(1)
client_handler = lambda client_socket:
client_socket.send(b’Hello, World!’)

while True:
client, addr = tcp_server.accept()
client_handler(client)

# Regular Expression Functions


# Built-in Functions
import re

re.match()
re.search()
re.findall()
re.finditer()
re.sub()
re.split()

import re

# search for a pattern in a string


string = “The quick brown fox jumps over the lazy dog.”
Pattern = r”fox”
match = re.search(pattern, string)

# replace a pattern in a string


new_string = re.sub(pattern, “cat”, string)

# find all occurances of a pattern in a string


matches = re.findall(pattern, string)

# split a string based on a pattern


string = “The quick brown fox, jumps over the lazy dog.”
pattern = r”[,.\s]+

# compile a regular expression pattern for reuse


pattern = re.compile(r”\d+”)

# match a pattern at the beginning of a string


string = “42 is the answer to the ultimate question of life,
the universe, and everything.”
pattern = r”^\d+
match = re.match(pattern, string)

# External Library Functions


re.compile()
re.escape()
re.fullmatch()
re.subn()
re.groupindex()
re.scanner()
import re
# Finding a match in a string
match = re.search(r’\d+,’foo123bar’)
print(match.group()) # Output: 123

# Replacing a pattern with a string


text = ‘foo123bar’
new_text = re.sub(r’\d+, ‘456’, text)
print(new_text) # Output foo456bar

# Splitting a string by a pattern


text = ‘foo,bar,baz’
parts = re.split(‘,’, text)
print(parts) # Output: [‘foo’, ‘bar’, ‘baz’]

# Matching a string against a pattern and returning all


matches
text = ‘foo123bar456baz789’
matches = re.findall(r’\d+, text)
print(matches) # Output: [‘123’, ‘456’, ‘789’]

# Compiling a regular expression for repeated use


pattern = re.compile(r’\d+)
match = pattern.search(‘foo123bar’)
print(match.group()) # Output: 123

# Lambda Functions
import re

# Find all matches of a regular expression in a list of


strings using a lambda function
strings = [‘apple’, ‘banana’, ‘cherry’, ‘date’]
pattern = re.compile(r’a\w+’)
matches = list(filter(lambda s: pattern.match(s), strings))
print(matches) # Output: [‘apple’]

# Replace all matches of a regular expression in a string usin


a lambda function
text = ‘The quick brown fox jumps over the lazy dog’
pattern = re.compile(r’\b\w{4}\b’)
new_text = re.sub(pattern, lambda m: m.group(0).upper(), text)
print(new_text) # Output: ‘The QUICK brown JUMPS over the
LAZY’

# Check if a string matches a regular expression using a


lambda function
email_regex = re.compile(r’^[a-zA-Z0-9._%+-]@[a-zA-Z0-9.-]+\.
[a-zA-Z]{2,}$’)
is_valid_email = lambda email: bool(email_regex.match(email))
print(is_valid_email(‘user@example.com’)) # Output: True
# Date and Time Functions
# Built-in Functions
datetime.datetime.now()
datetime.datetime.combine()
datetime.datetime.strptime()
datetime.datetime.date()
datetime.datetime.time()
datetime.timedelta()
date()
time()
datetime()
timedelta()
tzinfo()

import datetime

# Returns a datetime object representing the current date and


time
now = datetime.datetime.now()
print(now)

# Returns a date object representing the specified year,


month, and day
date_obj = datetime.date(2022, 2, 4)
print(date_obj)

# Returns a time object representing the specified hour,


minute, and second
time_obj = datetime.date(14, 30, 0)
print(time_obj)

# Returns a datetime object representing the specified year,


month, day, hour, minute, and second
dt_obj = datetime.datetime(2022, 2, 4, 14, 30, 0)
print(dt_obj)

# Represents a duration of time as a timedelta object


tdelta = datetime.timedelta(days=7)
new_date = datetime.date.today() + tdelta
print(new_date)

# get the current date and tiime


current_datetime = datetime.datetime.now

# get the current date


current_date = datetime.date.today()

# get the date of a specified year, month and day


specific_date = datetime.date(2022, 2, 4, 14, 30, 0)
# get the time of day
current_time = datetime.datetime.now().time()

# format a data and time as a string


formatted_datetime = current_datetime.strftime(“%Y-%m-%d %H:
%M:%S”)

# format a data and time as a string


formatted_datetime = current_datetime.strftime(“2022-06-01
12:00:00”, “%Y-%m-%d %H:%M:%S”)

# parse a string into a datetime object


parsed_datetime = datetime.datetime.strptime(“2022-06-01
12:00:00”, “%Y-%m-%d %H:%M:%S”)

# perform arithmetic on dates and times


next_month = current_datetime + datetime.timedelta(days=30)
previous_month = current_datetime –
datetime.timedelta(days=30)

# External Library Functions


dateutil.parser.parse()
dateutil.relativedelta.relativedelta()
dateutil.tz.gettz()
pytz.timezone()
arrow.get()
arrow.utcnow()

# arrow
import arrow

utc = arrow.utcnow()
local = utc.to(‘US/Pacific’)
print(f”UTC: {utc}\nLocal: {Local}”)

# pendulum
import pendulum

now = pendulum.now(‘Europe/Paris’)
tomorrow = now.add(days=1)
print(f”Tomorrow: {tomorrow}\nTimezone:
{tomorrow.timezone_name}”)

# pytz
from datetime import datetime
import pytz

utc = pytz.utc
eastern = pytz.timezone('US/Eastern')

# Set time with UTC timezone


date = datetime(2023, 2, 5, 10, 30, tzinfo=utc)
print(f"UTC time: {date}")

# Convert UTC time to Eastern timezone


date_eastern = date.astimezone(eastern)
print(f"Eastern time: {date_eastern}")

# Lambda Functions
# Get current date
import datetime

get_current_date = lambda: datetime.date.today()

print(get_current_date()) # Output: 2022-02-03

# Get current time


import datetime

get_current_time = lambda: datetime.datetime.now().time()


print(get_current_date()) # Output: 14:23:42.521289

# Get the difference between two dates


import datetime

get_date_diff = lambda date1, date2: abs((date2 – date1).days)

date1 = datetime.date(2022, 1, 1)
date2 = datetime.date(2022, 2, 3)

print(get_date_diff(date1, date2)) # Output: 33

# Convert a string to a date:


import datetime

str_to_date = lambda date_str:


datetime.datetime.strptime(date_str, ‘%Y-%m-%d’).date()

date_str = ‘2022-02-03’

print(str_to_date(date_str)) # Output: 2022-02-03

# Math Functions
# Built-in Functions
math.ceil()
math.floor()
math.fabs()
math.factorial()
math.isclose()
math.isfinite()
math.isnan()
math.modf()
math.trunc()
math.gcd()

import math

# Basic operations
print(math.sqrt(16)) # square root: 4.0
print(math.pow(2, 3)) # power: 8.0
print(math.exp(1)) # exponential: 2.718281828459045

# Trigonometric functions
print(math.sin(math.pi / 2)) # sine 1.0
print(math.cos(math.pi / 2)) # cosine 6.123233995736766e-17
print(math.tan(math.pi / 4)) # tangent 0.9999999999999999
print(math.radians(90)) # degrees to radians:
1.5707963267948966

# Logarithmic functions
print(math.log(10)) # natural logarithm: 2.302585092994046
print(math.log(100)) # base 10 logarithm: 2.0

# Constants
print(math.pi) # pi: 3.14592653589793
print(math.e) # Euler’s number: 2.718281828459045

# Rounding functions
# Round a floating-point number to the nearest integer
print(round(3.14)) # 3

# Round a floating-number to 2 decimal places


print(round(3.14159)) # 3.14

# Round up to the nearest integer


print(math.ceil(3.14)) # 4

# Round down to the nearest integer


print(math.floor(3.99)) # 3

# Factorial Functions
import math

# Calculate the factorial of a number


print(math.factorial(5)) # 120

# Round down to the nearest integer


n = 10
r = 3
binom_coef = math.factorial(n)) // (math.factorial(r) *
math.factorial(n – r))
print(binom_coef) # 120
# Statistical Functions

# Calculate the mean of a list of numbers


numbers = [2, 4, 6, 8]
mean = statistics.mean(numbers)
print(mean) # 5.0

# Calculate the median of a list of numbers


numbers = [2, 4, 6, 8, 10]
median = statistics.median(numbers)
print(median)

# Calculate the mode of a list of numbers


numbers = [2, 4, 4, 6, 6, 6, 8]
mode = statistics.mode(numbers)
print(mode) # 6

# Calculate the standard deviation of a list of numbers


numbers = [2, 4, 6, 8]
stdev = statistics.stdev(numbers)
print(stdev) # 2.581988897471611

# External Library Functions


numpy.ceil()
numpy.floor()
numpy.round()
numpy.exp()
numpy.log()
numpy.log10()
numpy.sqrt()
numpy.power()
numpy.sin()
numpy.cos()
numpy.tan()
numpy.absolute()
numpy.add()
numpy.arccos()
numpy.arcsin()
numpy.arctan()
numpy.arctan2()
numpy.clip()
numpy.cos()
numpy.cosh()
numpy.deg2rad()
numpy.exp()
numpy.floor_divide()
numpy.fmod()
numpy.lcm()
numpy.log()
numpy.log10()
numpy.log2()
numpy.multiply()
numpy.negative()
numpy.power()
numpy.rad2deg()
numpy.reciprocal()
numpy.remainder()
numpy.round()
numpy.sign()
numpy.sin()
numpy.sinh()
numpy.sqrt()
numpy.subtract()
numpy.sum()
numpy.tan(0
numpy.tanh()
numpy.true_divide()
scipy.special.erf()
scipy.special.erfc()
scipy.special.erfinv()
scipy.special.gamma()
scipy.special.gammainc()
scipy.special.gammaincc()
scipy.special.gammaln()
scipy.special.logit()

# 1. NumPy: a library for scientific computing in Python that


provides support for arrays, and numerical operations

# Example: Finding the mean of a list using NumPy


import numpy as np

my_list = [1, 2, 3, 4, 5]
mean = np.mean(my_list)
print(mean)

# 2. SciPy: a library for scientific and technical computing


that provides support for optimization, integration,
interpolation, and other numerical operations

# Example: Finding the roots of a polynomial using SciPy


import numpy as np
from scipy import optimize

# Define a polynomial function


def polynomial(x):
return x**3 – 6*x**2 + 11*x – 6

# Find the roots of the polynomial


roots = optimize.root(polynomial, [0, 1, 2])
print(roots.x)
# SymPy: a library for symbolic mathematics that provides
support for algebraic operations, calculus, and other
mathematical operations

# Example: Simplifying a trigonometric expression using SymPy


import sympy

# Define a trigonometric expression


x = sympy.Symbol(‘x’)
expr = sympy.sin(x)**2 + sympy.cos(x)**2

# Simplify the expression


simplified_expr = sympy.simplify(expr)
print(simplified_expr)

# 4. ‘numpy’: provides functions for array manipulation,


linear algebra, statistical analysis, and other mathematical
operations
a = np.random.rand(3, 3)
print(a)

# Calculate the mean of the array


mean = np.mean(a)
print(mean)

# Calculate the determinant of the array


det = np.linalg.det(a)
print(det)

# 5. ‘scipy’: provides a wide range of functions for


scientific and technical computing, including optimization,
integration, interpolation, signal processing, and more

import scipy.integrate as spi


# Define a function to integrate
f = lambda x: X**2 + 2*x + 1

# Integrate the function over the range [0, 1]


result, error = spi.quad(f, 0, 1)
print(result)

# 6. ‘matplotlib’: provides functions for catering


visualizations and plots of data
import matplotlib.pyplot as plt

# Create data to plot


x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

# Plot the data as a line graph


plt.plot(x, y)

# Add labels and a title to the graph


plt.xlabel(‘x’)
plt.ylabel(‘y’)
plt.title(‘y = x^2’)

# Display the graph


plt.show()

# Lambda Functions
# 1. Squaring a number using a lambda function
square = lambda x: x**2
print(square(5)) # Output: 25

# 2. Generating a list of squares using a lambda function and


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

# 3. Using a lambda function to calculate the area of a


rectangle
area = lambda length, width: length * width
print(area(5, 7)) # Output: 35

# 4. Using a lambda function with the ‘reduce’ function to


calculate the factorial of a number
from functools import reduce

factorial = lambda n: reduce(lambda x, y: x * y, range(1,


n+1))
print(factorial(5)) # Output: 120

# 5. Using a lambda function to round a number to a certain


number of decimal places
round_to_two_decimal_places = lambda x: round(x, 2)
print(round_to_two_decimal_places(3.14159)) # Output: 3.14

# Statistics Function
# Built-in Functions
numbers = [1, 2, 2, 3, 4, 4, 4, 5] # calculate mean, median,
mode of a list of numbers
mean = statistics.mean(numbers)
median = statistics.median(numbers)
mode = statistics.mode(numbers)

stdev = statistics.stdev(numbers) # calculate standard


deviation and variance of a list of numbers
variance = statistics.variance(numbers)
x = [1, 2, 3, 4, 5] # Calculate the correlation coefficient
between two lists of numbers
y = [3, 4, 5, 6, 7]
correlation = statistics.correlation(x, y)

sample1 = [1, 2, 3, 4, 5] # Perform a t-test on two lists of


numbers
sample2 = [3, 4, 5, 6, 7]
t_stat, p_value = statistics.ttest_ind(sample1, sample2)

import statistics

# Mean
data = [1, 2, 3, 4, 5]
mean = statistics.mean(data)
print(“Mean:”, mean)

# Median
data = [1, 2, 3, 4, 5]
mean = statistics.median(data)
print(“Median:”, median)

# Mode
data = [1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
mode = statistics.mode(data)
print(“Mode:”, mode)

# Variance
data = [1, 2, 3, 4, 5]
variance = statistics.variance(data)
print(“Variance:”, variance)

# Standard deviation
data = [1, 2, 3, 4, 5]
stdev = statistics.stdev(data)
print(“Standard deviation:”, stdev)

# Output
Mean: 3
Median: 3
Mode: 5
Variance: 2.5
Standard deviation: 1.581138800841898

# External Library Functions


import statistics
import numpy

statistics.mean()
statistics.median()
statistics.stdev()
statistics.variance()
statistics.mode()
numpy.mean()
numpy.median()
numpy.std()
numpy.var()
numpy.corrcoef()
numpy.cov()
scipy.stats.mode()
scipy.stats.skew()
scipy.stats.kurtosis()
scipy.stats.ttest_1samp()
scipy.stats.ttest_ind()
scipy.stats.ttest_rel()
scipy.stats.chisquare()
scipy.stats.normaltest()
scipy.stats.shapiro()

# 1. NumPy library for many functions for numerical operations


including statistical functions such as mean, standard
deviation, variance, correlation, and histogram
import numpy as np

data = [1, 2, 3, 4, 5]
mean = np.mean(data)
std_dev = np.std(data)
variance = np.var(data)
correlation = np.corrcoef(data)
hisogram = np.histogram(data)

# 2. SciPy for advanced statistical functions for data


analysis, optimization and more. Examples of statistical
functions such as probability distributions, hypothesis
testing, and regression analysis
from scipy.stats import norm, ttest_ind, linregress

data1 = [1, 2, 3, 4, 5]
data2 = [6, 7, 8, 9, 10]
t_stat, p_value = ttest_ind(data1, data2)
slope, intercept, r_value, p_value, std_err =
linregress(data1, data2)
normal_pdf = norm.pdf(data1)

# 3. Pandas for data manipulation and analysis, including


functions for handling statistical data, such as dataframes
and series e.g. groupby, pivot tables, and rolling functions
import pandas as pd
data = {‘name’: [‘John’, ‘Jane’, ‘Jack’, ‘Jill’],
‘age’: [25, 30, 35, 40],
‘salary’: [50000, 60000, 70000, 80000]}
df = pd.DataFrame(data)
mean_salary = df[‘salary’].mean()
groupby_age = df.groupby(‘age’).mean()
rolling_mean = df[‘salary’].rolling(window=2).mean()

# Lambda Functions (Not common)


mean = lambda nums: sum(nums) / len(nums) # Lambda function to
find the mean of a list of numbers
median = lambda nums: sorted(nums)[len(nums) // 2] # Lambda
function to find the median of a list of numbers
mode = lambda nums: max(set(nums), key=nums.count) # Lambda
function to find the mode of a list of numbers

import statistics

# Mean
mean = lambda data: statistics.mean(data)

# Median
median = lambda data: statistics.median(data)

# Mode
mode = lambda data: statistics.mode(data)

# Variance
variance = lambda data: statistics.variance(data)

# Standard deviation
stdev = lambda data: statistics.stdev(data)

# Correlation
correlation = lambda x, y: statistics.correlation(x, y)

# These lambda functions can be used like regular functions:


data = [1, 2, 3, 4, 5]

# Calculate the mean using the lambda function


mean_value = mean(data)

# Calculate the median using the lambda function


median_value = median(data)

# Calculate the variance using the lambda function


variance_value = variance(data)

# Calculate the standard deviation using the lambda function


stdev_value = stdev(data)

# Calculate the correlation using the lambda function


x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
correlation_value = correlation(x, y)
# Cryptographic Functions
# Built-in Functions
import hashlib

# Hashing functions:
haslib.sha256(b’my_password’).hexdigest() # returns the SHA-
256 of the password as a hexadecimal string
haslib.md5(b’my_data).hexdigest() # returns the MD5 hash of
the data as a hexadecimal string

# Compute the SHA-256 hash of a string


hash_object = hashlib.sha256(b’Hello, world!’)
hash_dig = hash.object.hexidigest()
print(hex_dig)

# Compute the MD5 hash of a file


hash_md5 = hashlib.md5()
with open(“example.txt”,”rb”) as f:
for chunk in iter(lambda: f.read(4096), b””):
hash_md5.update(chunk)
print(hash_md5.hexidigest())

# Encryption/decryption:
import base64
from cryptography.fernet import Fernet

# Generate a secret key


key = Fernet.generate_key()

# Encrypt a message using the secret key


cipher_suite = Fernet(key)
cipher_text = cipher_suite_encrypt(b”Hello, world!”)
print(cipher_text)

# Decrypt a message using the secret key


cipher_suite = Fernet(key)
cipher_text = cipher_suite_decrypt(cipher_text)
print(plain_text)

# Digital signatures:
import hashlib
from cryptography.hazmat.primitives.asymmetric import rsa,
padding
from cryptography.hazmat.primitives import serialization

# Generate an RSA key pair:


private_key = rsa.generate_private_key(public_exponent=65537,
key_size=2048)
# Sign a message using the private key
message = b”Hello, world!”
hash = hashlib.sha256(message).digest()
signature = private_key.sign(
hash,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
print(signature)

# Verify the signature using the public key


public_key = private_key.public_key()
try:
public_key.verify()
signature,
hash,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
print(“Signature is valid”)
except:
print(“Signature is not valid”)

import secrets
# Cryptographically secure random number generation functions
secrets.token_bytes(16) # returns a 16-byte (128-bit) random
number as bytes
secrets.token_hex(16) # returns a 32-character hexidecimal
string representing a 16-byte (128-bit) random number
secrets.token_urlsafe(16) # returns a URL-safe string
representing a 16-byte (128-bit) random number

import hmac
hmac.new(b’secret_key’, b’my_message’
,hashlib.sha256).hexdigest() # returns the HMAC of the message
using SHA-256 as the hash function

# External Library Functions


import hashlib
import hmac

hashlib.md5()
hashlib.sha1()
haslib.sha224()
hashlib.sha256()
hashlib.sha384()
hashlib.sha512()
hmac.new()
cryptography.hazmat.primitives.asymmetric.rsa.generate_private
_key()
cryptography.hazmat.primitives.asymmetric.rsa.RSASignatureAlgo
rtithm()
cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey()
cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey()
cryptography.hazmat.primitives.ciphers.modes.ECB()
cryptography.hazmat.primitives.ciphers.modes.CBC()
cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC()
cryptography.hazmat.primitives.serialization.load_der_private_
key()
cryptography.hazmat.primitives.serialization.load_der_public_k
ey()
cryptography.hazmat.primitives.serialization.load_pem_private_
key()
cryptography.hazmat.primitives.serialization.load_pem_public_k
ey()

# 1. PyCrypto:
from Crypto.Cipher import AES

# Create an AES cipher object


cipher = AES.new(key, AES.MODE_CBC, iv)

# Encrypt plaintext
ciphertext = cipher.encrypt(plaintext)

# Decrypt ciphertext
decrypted_text = cipher.decrypt(ciphertext)

# Create an AES cipher object


cipher = AES.new(key, AES.MODE_CBC, iv)

# 2. PyCryptodome:
from Crypto.Hash import SHA256
from Crypto.Random import get_random_bytes

# Generate a random salt


salt = get_random_bytes(16)

# Create a SHA-256 hash object


hash_object = SHA256.new(data=plaintext)

# Add the salt to the hash object


final_hash = hash_object.digest()
# 3. cryptography:
from cryptography.fernet import Fernet

# Generate a new key for encryption


key = Fernet.generate_key()

# Create a Fernet cipher object


cipher = Fernet(key)

# Encrypt plaintext
ciphertext = cipher.encrypt(plaintext)

# Decrypt ciphertext
decrypted_text = cipher.decrypt(ciphertext)

# 1. ‘cryptography’ library for cryptography including various


ciphers, hashes, key derivation functions, and more
from cryptography.hazmat.primitives.ciphers import Cipher,
algorithms, modes
from cryptography.hazmat.primitives.asymmetric import rsa,
padding
from cryptography.hazmat.primitives.kdf.pbkdf2 import
PBKDF2HMAC
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes

# Example of symmetric encryption


key = b'mysecretkey' # symmetric key
iv = b'myivvector' # initialization vector
plaintext = b'hello world'
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
encryptor = cipher.encryptor()
ciphertext = encryptor.update(plaintext) +
encryptor.finalize()

# Example of asymmetric encryption


message = b'hello world'
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
public_key = private_key.public_key()
ciphertext = public_key.encrypt(
message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
# Example of key derivation using PBKDF2
password = b'mypassword'
salt = b'salt'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
)
key = kdf.derive(password)

# 2.‘pycryptodome’: a self-contained Python package of low-


level cryptographic primitives that supports both Python 2 and
3
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Hash import SHA256
from Crypto.Signature import pkcs1_15

# Example of symmetric encryption


key = get_random_bytes(16) # 128-bit key
iv = get_random_bytes(16) # 128-bit initialization vector
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = b'hello world'
ciphertext = cipher.encrypt(plaintext)

# Example of SHA-256 hash


h = SHA256.new()
h.update(b'hello world')
digest = h.digest()

# Example of RSA signature and verification


message = b'hello world'
private_key = RSA.generate(2048)
public_key = private_key.publickey()
signature =
pkcs1_15.new(private_key).sign(SHA256.new(message))
try:
pkcs1_15.new(public_key).verify(SHA256.new(message),
signature)
print('Valid signature')
except (ValueError, TypeError):
print('Invalid signature')

# 3.‘pcryptodome’: a fork of the ‘pycrypto’ library that


provides more up-to-date cryptographic primitives and support
for both Python 2 and 3
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
from Crypto.Hash import SHA256, HMAC

# Example of symmetric encryption


key = get_random_bytes(16) # 128-bit key
iv = get_random_bytes(16) # 128-bit initialization vector

# Lambda Functions

# providing simple interface for basic cryptographic


operations or allowing for the creation of more complex
cryptographic algorithms.

# 1. Example of lambda function for generating a secure random


key using the ‘secrets’ module

# This code will generate a random hexadecimal key of length


16 using the ‘secrets’ module and the ‘token_hex’ function.
Convenient way to create a callable object that generates a
secure key whenever it is called
import secrets

generate_key = lambda: secrets.token_hex(16)

print(generate_key())

# 1. Generating random byte sequences using the ‘secrets’


module:
import secrets

random_bytes = lambda n: secrets.token_bytes(n)

# 2. Hashing a message using the ‘hashlib’ module


import hashlib

hash_message = lambda message:


hashlib.sha256(message.encode()).hexdigest()

# 3. Encrypting and decrypting a message using the


‘cryptography’ module:
from cryptography.fernet import Fernet

encrypt_message = lambda key, message:


Fernet(key).encrypt(message).encode())

decrypt_message = lambda key, ciphertext:


Fernet(key).decrypt(ciphertext).decode()

# Image Processing Functions


# Built-in Functions

# ‘Pillow’ library for image processing:


# 1. Opening and displaying an image file
from PIL import Image

image = Image.open(‘image.jpg’)
image.show()

# 2. Cropping an image
from PIL import Image

image = Image.open(‘image.jpg’)
image.crop((100, 100, 200, 200))
cropped_image.show()

# 3. Resizing an image
from PIL import Image

image = Image.open(‘image.jpg’)
image.crop((500, 500))
resized_image.show()

# 4. Converting an image to grayscale


from PIL import Image

image = Image.open(‘image.jpg’)
grayscale_image = image.convert(‘L’)
grayscale_image.show()

# 5. Rotating an image
from PIL import Image

image = Image.open(‘image.jpg’)
image.rotate(45)
rotated_image.show()

# *Assume that there is an image file named ‘image.jpg’ in


same directory as Python script

# External Library Functions


from PIL import Image
import cv2
Image.open()
Image.save()
cv2.imread
cv2.imshow()
cv2.imwrite()
Pillow.Image.open()
Pillow.Image.new()
Pillow.Image.blend()
Pillow.Image.chop()
Pillow.Image.composite()
Pillow.Image.convert()
Pillow.Image.copy()
Pillow.Image.crop()
Pillow.Image.draft()
Pillow.Image.effect_spread()
Pillow.Image.filter()
Pillow.Image.fliph()
Pillow.Image.flop()
Pillow.Image.getbands()
Pillow.Image.getbbox()
Pillow.Image.getdata()
Pillow.Image.getpixel()
Pillow.Image.histogram()
Pillow.Image.merge()
Pillow.Image.offset()

import cv2 # OpenCV

img = cv2.imread(“image.jpg”) # read an image


cv2.imshow(“Image”, img) # show the image
cv2.imwrite (“new_image.jpg”, img) # save the image
gray_img = cv2.cvtColor(img, cv2.COLOR_BG2GRAY) # convert
image to grayscale
ret, thresh = cv2.threshold(gray_img, 127, 255,
cv2.THRESH_BINARY) # apply image thresholding
blur = cv2.GaussianBlur(img, (5, 5), 0) # apply image blurring

from PIL import Image # Pillow


img = Image.open(“image.jpg”) # open an image
img.show() # show the image
img.save(“new_image.jpg”) # save the image
gray_img = img.convert(“L”) # convert the image to grayscale
thresh = gray_img.point(lambda x: 255 if x > 127 else 0, “1”)
# apply image thresholding
blur = img.filter(ImageFilter.BLUR) # apply image blurring

# 1. Pillow
from PIL import Image

# Open image file


image = Image.open('image.jpg')

# Convert image to grayscale


gray_image = image.convert('L')

# Save grayscale image


gray_image.save('gray_image.jpg')

# OpenCV
import cv2
# Load image
image = cv2.imread('image.jpg')

# Convert image to grayscale


gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Save grayscale image


cv2.imwrite('gray_image.jpg', gray_image)

# 3. scikit-image
from skimage import io, color

# Load image
image = io.imread(‘image.jpg’)

# Convert image to grayscale


gray_image = color.rgb2gray(image)

# Save grayscale image


io.imsave(‘gray_image.jpg’, gray_image)

# 4. Mahotas
import mahotas
import numpy as np

# Load image
image = mahotas.imread('image.jpg')

# Load image
gray_image = np.mean(image, axis=2)

# Save grayscale image


mahotas.imsave(‘gray_image.jpg’, gray_image)

# 5. OpenCV
import cv2

# Load image
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply a Gaussian blur to the image


blur = cv2.GaussianBlur(gray, (5, 5),0)

# Detect edges in the image using the Canny algorithm


edges = cv2.Canny(gray, (100, 200)

# Display the resulting image


cv2.imshow(‘Edges’, edges)
cv2.waitKey(0)
# 6. Pillow (fork of Python Imaging Library (PIL)); provides
API for working with images. Can read & write various image
formats, manipulate images, apply filters, and more
from PIL import Image, ImageFilter

# Load an image from file


img = Image.open('image.jpg')

# Convert the image to grayscale


gray = img.convert('L')

# Apply a Gaussian blur to the image


blur = gray.filter(ImageFilter.GaussianBlur(radius=5))

# Detect edges in the image using the Laplacian filter


edges = blur.filter(ImageFilter.Kernel((3, 3), (-1, -1, -1, -
1, 8, -1, -1, -1, -1)))

# Display the resulting image


edges.show()

# 67 scikit-image library for image processing and computer


vision with wide range of functions for image filtering,
segmentation, feature detection, and more

from skimage import io, filters

# Load an image from file


img = io.imread('image.jpg', as_gray=True)

# Apply a Gaussian blur to the image


blur = filters.gaussian(img, sigma=2)

# Detect edges in the image using the Canny algorithm


edges = filters.sobel(blur)

# Display the resulting image


io.imshow(edges)
io.show()

# Lambda Functions

# Pillow Library:
# 1. Applying a filter to an image using a lambda function
from PIL import Image, ImageFilter

image = Image.open('image.jpg')

# Define the filter function using a lambda function


filter_func = lambda x: x.filter(ImageFilter.BLUR)
# Apply the filter function to the image
filtered_image = filter_func(image)

# Show the filtered image


filtered_image.show()

# 2. Resizing an image using a lambda function


from PIL import Image

image = Image.open('image.jpg')

# Define the resize function using a lambda function


resize_func = lambda x: x.resize((800, 800))

# Apply the resize function to the image


resized_image = resize_func(image)

# Show the resized image


resized_image.show()

# 3. Converting an image to grayscale using lambda function


from PIL import Image

image = Image.open('image.jpg')

# Define the grayscale function using a lambda function


grayscale_func = lambda x: x.convert('L')

# Apply the grayscale function to the image


grayscale_image = grayscale_func(image)

# Show the grayscale image


grayscale_image.show()

# NumPy Library:
# Import NumPy library
import numpy as np

# Create a NumPy array with shape (3, 3) and values (1, 2, 3)


in the first row
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Compute the transpose of the array


transpose = np.transpose(array)

# Compute the dot product of the array with its transpose


dot_product = np.dot(array, transpose)

# Compute the eigenvalues and eigenvectors of the dot product


eigenvalues, eigenvectors = np.linalg.eig(dot_product)
# Pandas Library:
# Import Pandas library
import pandas as pd

# Read in a CSV file as a Pandas DataFrame


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

# Group the data by the 'category' column and compute the mean
of the 'value' column for each group
grouped = df.groupby('category')['value'].mean()

# Filter the data to include only rows where the 'value'


column is greater than 10
filtered = df[df[‘value’] > 10]

# Sort the data by the 'date' column in descending order


sorted = df.sort_values('date', ascending=False)

# Matplotlib Library:
# Import Matplotlib library
import matplotlib.pyplot as plt

# Create a list of x-coordinates


x = [1, 2, 3, 4, 5]

# Create a list of y-coordinates


y = [2, 4, 6, 8, 10]

# Create a line plot of the data


plt.plot(x, y)

# Add labels to the plot


plt.xlabel('X')
plt.ylabel('Y')
plt.title('Line Plot')

# Show the plot


plt.show()

# OpenCV Library:
# Import OpenCV library
import cv2

# Load an image from a file


img = cv2.imread('image.jpg')

# Convert the image to grayscale


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply a Canny edge detection filter to the image


edges = cv2.Canny(gray, 100, 200)

# Display the original image and the edge-detected image side-


by-side
cv2.imshow('Original Image', img)
cv2.imshow('Edge-Detected Image', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Resizing an image
import cv2

resize = lambda img, width, height: cv2.resize(img,(width,


height))

image = cv2.imread(‘image.jpg’)
resized_image = resize(image, 500, 500)

# Drawing a rectangle on an image


import cv2

draw_rectangle = lambda img, pt1, pt2, color, thickness:


cv2.rectangle(img, pt1, pt2, color, thickness)

image = cv2.imread('image.jpg')
draw_rectangle(image, (50, 50), (100, 100), (255, 0, 0), 2)

# scikit-image Library:
import numpy as np
from skimage import io, filters, transform

# Load image using skimage


img = io.imread('path/to/image')

# Apply gaussian filter to the image


gaussian = lambda img, sigma: filters.gaussian(img, sigma)
img_gaussian = gaussian(img, 3)

# Apply sobel filter to the image


sobel = lambda img: filters.sobel(img)
img_sobel = sobel(img)

# Rotate the image by 45 degrees


rotate = lambda img, angle: transform.rotate(img, angle)
img_rotated = rotate(img, 45)

# Rescale the image to half of its size


rescale = lambda img, scale: transform.rescale(img, scale)
img_rescaled = rescale(img, 0.5)

# Web Scraping Functions


# External Library Functions
import requests
from bs4 import BeautifulSoup

requests.get()
requests.post()
requests.put()
requests.delete()
BeautifulSoup()

# Machine Learning Functions


# Built-in Functions
from math import exp, log # math functions
import random # random module
from statistics import mean, stdev # statistics functions
import statistics # built-in machine learning modules
import math

# 1. scikit-learn’s ‘train_test_split’ function for splitting


data into training and testing sets

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.3, random_state=42)

# 2. TensorFlow’s ‘tf.keras.Sequential’ function for defining


a sequential model
import tensorflow as tf

model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu',
input_shape=(32,)),
tf.keras.layers.Dense(10, activation='softmax')
])

# 3. PyTorch’s ‘nn.Module’ function for defining a neural


network module
import torch.nn as nn

class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 50, 5)
self.fc1 = nn.Linear(4*4*50, 500)
self.fc2 = nn.Linear(500, 10)

# 4. Keras’ ‘keras.layers.Dense’ function for adding a fully


connected layer to a neural network
from keras.layers import Dense
model.add(Dense(64, activation='relu'))

# External Library Functions


from sklearn import svm
from sklearn.linear_model import LinearRegression

svm.SVC()
LinearRegression()

import tensorflowsdrsx
import scikit-learn

tensorflow.keras.models.Sequential()
tensorflow.keras.layers.Dense()
tensorflow.keras.layers.Conv2D()
tensorflow.keras.layers.MaxPooling2D()
tensorflow.keras.layers.Flatten()
scikit-learn.model_selection.train_test_split()
scikit-learn.preprocessing.StandardScaler()
scikit-learn.cluster.KMeans()

import numpy as np # numpy – arrays, linear algebra, random


number generation
import pandas as pd # pandas – data manipulation and analysis
from sklearn import datasets, linear_model, metrics,
model_selection # scikit-learn – machine learning models and
tools
import tensorflow as tf # TensorFlow – machine learning
models and tools
from tensorflow import keras # Keras – neural network library

# Lambda Functions
linreg = lambda X, y: np.linalg.inv(X.T @ X) @ X.T @ y #lambda
function for linear regression
r_squared = lambda y_true, y_pred: 1 – (np.sum((y_true –
y_pred)**2)/np.sum((y_true – np.mean(y_true))**2)) # lambda
function for calculating R^2 value
sigmoid = lambda z: 1 / (1 + np.exp(-z)) # lambda function for
sigmoid function
softmax = lambda z: np.exp(z) / np.sum(np.exp(z), axis=1,
keepdims=True) # lambda function for softmax function

# Natural Language Processing Functions


# Built-in Functions
text = “This is a sample text for NLP.” # String methods for
natural language processing
text.lower() # convert to lowercase
text.upper() # convert to uppercase
text.title() # convert to titlecase
text.strip() # remove leading and trailing whitespace
text.split() # split into a list of words

# External Library Functions


import nltk
import spacy

nltk.word_tokenize()
nltk.sent_tokenize()
nltk.pos_tag()
nltk.ne_chunk()
nltk.download()
spacy.load()
spacy.Doc()

import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer, WordNetLemmatizer

nltk.download(‘stopwords’) # download the stopword corpus


nltk.download(‘punkt’) # download the word tokenizer
nltk.download(‘wordnet’) # download the WordNet lexical
database

stop_words = set(stopwords.words(‘english’)) # set of English


stop words
tokens = word_tokenize(text) # tokenize the text into words
filtered_words = [word for word tokens if word not in
stop_words] # remove stop words
stemmer = PorterStemmer() # Word Stemming and lemmatization
lemmatizer = WordNetLemmatizer()
stemmed_words = [stemmer.stem(word) for word in
filtered_words]
lemmatized_words = [lemmatizer.lemmatize(word) for word in
filtered _words]

# Lambda Functions
remove_punctuation = lambda text: ‘’.join(char for char in
text if not char.isalnum())
remove_numbers = lambda text: ‘’.join(char for char in text if
not char.isdigit())
remove_whitespace = lambda text: ‘’.join(text.split())

# Data Visualization Functions


# External Library Functions
import matplotlib.pyplot as plt
import seaborn as sns
plt.plot()
plt.scatter()
plt.hist()
sns.lineplot()
sns.scatterplot()
sns.histplot()

import matplotlib
import seaborn
import plotly

matplotlib.pyplot.plot()
matplotlib.pyplot.scatter()
matplotlib.pyplot.hist()
seaborn.lineplot()
seaborn.scatterplot()
seaborn.distplot()
plotly.express.scatter()
plotly.express.line()
plotly.express.bar()

from PTL import Image


from skimage import io
from skimage.filters import sobel, threshold_otsu
from skimage.color import rgb2gray
import cv2
import numpy as np

img = Image.open(‘image.jpg’) # read image


img.show() # display image

img = io.imread(‘image.jpg’) # read and display image


using scikit-image
io.imshow(img)
io.show()

gray_img = rgb2gray(img)) # convert image to grayscale


using OpenCV

edge_sobel = sobel(gray_img) # apply Sobel filter to detect


edges in image using using scikit-image

threshold_value = threshold_otsu(gray_img) # apply 0tsu


thresholding to segment image using scikit-image
binary_img = gray_img > threshold_value

img = cv2.imread(‘image.jpg’) ) # read and display image using


OpenCV
cv2.imshow(‘image’, img)
cv2.waitkey(0)
cv2.destroyAllWindows()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert
image to grayscale using OpenCV

edge_canny = cv2.Canny(gray_img, 100, 200) # apply Canny edge


detection to detect images in image using OpenCV

ret, binary_img = cv2.threshold(gray_img, 0, 255,


cv2.THRESH_BINARY+cv2.THRESH_OTSU) # apply thresholding to
segment image using OpenCV

blurred_img = cv2.GaussianBlur(img, (5,5), 0) # apply Gaussian


blur to smooth image using OpenCV

result = np.subtract(img, blurred_img) # perform image


arithmetic using NumPy

Variables: Names that refer to values or objects in Python. You can assign a value
to a variable using the ‘=’ operator.

x = 2
y = 3

Data Types: Integers, Floating-point numbers, strings, and Booleans. You can use
operators and functions to manipulate and combine these data types as needed.

Booleans: Values representing logical ‘True’ and ‘False’ values.


Point Numbers (Floats): Values representing decimals numbers e.g. ‘3.14’
Integers: Whole numbers like ‘0, 1, 2, -3’
Strings: A sequence of characters representing text. Strings are usually enclosed in
quotation marks, like “Hello World”

You might also like