- Data Types
- Data Structures
- Conditional Statements
- Comprehension
- Exception Handling
- Working with Files
- Arguments
- Classes
- Python Standard Libraries
- SQL Alchemy
num = 123
string = str(num)
a = "123"
b = int(a)
string = "abc"
string[0] = "d"
TypeError: 'str' object does not support item assignment
num = 123
num[0] = 4
TypeError: 'int' object is not subscriptable
string1 = "hello"
string2 = "world"
string3 = string1 + ' ' + string2
string = "I like Python!"
string[2:6] will produce 'like'
my_string = "I like %s" % "Python" produces 'I like Python'
var = "cookies"
my_string = "I like %s" % var produces 'I like cookies'
int_string = "%i + %i = %i" % (1,2,3) produces 1 + 2 = 3
float_string = "%f" % (1.23) produces 1.230000
float_string2 = "%.2f" % (1.23) produces 1.23
float_string3 = "%.2f" % (1.237) produces 1.24
f: formatted string literal (a way to embedded expressions)
greeting = f"Hello, {name}. You are {age} years old."
r: raw string (to avoid \\ being taken literal)
raw_path = r"C:\Users\Alice\Documents"
b: byte string (handle binary data)
byte_data = b"\x48\x65\x6c\x6c\x6f" # Hexadecimal representation of "Hello"
fr: combined raw and formatted string literal
message = fr"{user}, your folder is located at {path}\Documents"
my_list = [1,2,4]
my_list[2] = 3
my_list.append(4)
[] or list([])
my_list = [1, "yes", 2, 2.70]
my_list = list((1, "yes", 2, 2.70))
list1 = [1,2,3]
list2 = ["a", "b", "c"]
nested = [list1, list2]
concatination = list1 + list2
() or tuple([])
my_tuple = (1,2,3)
my_tuple[0]
TypeError: 'tuple' object does not support item assignment
x = (1) produces a int 1
x = (1,) produces a tuple due to the comma
Unlike lists and tuples, sets cannot contain duplicate and unordered values.
my_list = [1,2,4,3,5,5]
my_set = set(my_list)
{} or dict()
my_dict = {1: "one", 2: "two", 3: "three"}
for i in range my_dict .values .items .keys
for key in my_dict
my_dict[4] = "four"
my_dict.pop(4)
print(2 in my_dict)
if, elif, else
if x not in
if x in
- This tells Python that you only want to run the following code if this program is executed as a standalone file.
- Whenever you create a Python script, you create a Python module.
- When you do import a module, it will not run the code that’s under the conditional because name will no longer equal “main”.
if __name__ == "__main__":
x = [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x = ['1', '2', '3', '4', '5']
y = [int(i) for i in x]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
x = [[1,2,3], [4,5,6], [7,8,9]]
y = [num for elem in x for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
my_dict = {i: i for i in range(10)}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
my_dict = {1: "yes", 2: "no", 3: "ok"}
swap_key_val_dict = {v: k for k, v in my_dict.items()}
filtered_dict = {k: v for k, v in my_dict.items() if k > 2}
{3: 'ok'}
calculation_dict = {k: v**2 for k, v in my_dict.items()}
my_list = [1,2,3,4,4]
my_set = {x for x in my_list}
{1, 2, 3, 4}
Exception (all built off of)
AttributeError (attribute refrence or assignment fails)
IOError (raised when an I/O fails)
ImportError (rasied when a module is not found)
IndexError (raised when accessing an out of range index)
KeyError (raised when a key in dict is not found)
KeyboardInterrupt (raied when the user hits the intterupt key (CRTL C))
NameError (raised when a local or global name is not found)
OSError (raised when a function returns a system-related error)
SyntaxtError (raised when incorrect syntax is used)
TypeError (raised when an opperation or function is applied to an innapropriate type)
ValueError (raised when a function has an innapropriate value)
ZeroDivisionError (raised when the second argument of a division or modulo operation is zero) e.g. 1/0
try:
something
except (errortype):
do something if try didnt work
my_dict = {"a":1, "b":2, "c":3}
try:
value = my_dict["d"]
except KeyError:
print("this key does not exist")
try:
value = my_dict["d"]
except (KeyError, IndexError):
print("An KeyError or IndexError occured")
handle = open("test.txt", "r")
or
handle = open(r"C:\User\etc...", "r")
data = handle.read()
print(data)
handle.close()
while True:
data = handle.read(1024)
print(data)
if not data:
break
handle = open("test.txt", "w")
handle.write ("changing the text in the file")
handle.close()
handle = open("test.txt")
is the same as
with open("test.txt") as file handler:
try:
with open("test.txt") as file_handler:
for line in file_handler:
print(line)
except IOError:
print("An IOError occured")
def fun(*args):
return sum(args)
print(fun(5,10,15))
def fun(**kwargs):
for k, val in kwargs.items():
print(k, val)
fun(a=1, b=2, c=4)
def function_a():
global a
a = 1
b = 2
return a+b
def function_b():
c = 3
return a+c
print(function_a())
print(function_b())
- Everything in python is an object as it has methods and values
- Lets take this string for example
- Using dir() we can find the methods available for the string
- A string is based on a class and x is an instance of that class
x = "mike"
dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count',
'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum',
'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']
- Class name should start with capital
- A function changes to a method when in a class
- Every method must have at least one argument (i.e self)
class Vehicle:
def __init__(self):
"""constructor"""
pass
- Attributes belong to the class
- They describe the vehicle
- There are also two methods which belong to the class
class Vehicle:
def __init__(self, colour):
"""constructor / initialised"""
self.colour = colour
self.doors = doors
self.tires = tires
def brake(self):
"""stop the car"""
return "Braking"
def drive(self):
"Drive the car"
return "Driving"
- self is used in a class to refer to itself
- Its a way to tell one instance from another
class Vehicle:
"""docstring"""
def __init__(self, color, doors, tires, vtype):
"""Constructor"""
self.color = color
self.doors = doors
self.tires = tires
self.vtype = vtype
def brake(self):
"""
Stop the car
"""
return "%s braking" % self.vtype
def drive(self):
"""
Drive the car
"""
return "I'm driving a %s %s!" % (self.color, self.vtype)
if __name__ == "__main__":
car = Vehicle("blue", 5, 4, "car")
truck = Vehicle("red", 3, 6, "truck")
- You inherit the attributes from the parent class
class Car(Vehicle):
"""
The Car class
"""
def brake(self):
"""
Override brake method
"""
return "The car class is breaking slowly!"
if __name__ == "__main__":
car = Car("yellow", 2, 4, "car")
car.brake()
'The car class is breaking slowly!'
car.drive()
"I'm driving a yellow car!"
- A module is a set of pre-written code with instructions
type() - object type
type(123)
dir() - tells what attributes and methods there are in the object
dir("test")
help() - provides instructions and options which you can then search
import csv
csv_path = "TB_data_dictionary_2014-02-26.csv"
with open(csv_path, "r") as f_obj:
for line in f_obj:
print(line)
Used for storing your application’s settings or even your operating system’s settings
import configparser
def createConfig(path):
"""
Create a config file
"""
config = configparser.ConfigParser()
config.add_section("Settings")
config.set("Settings", "font", "Courier")
config.set("Settings", "font_size", "10")
config.set("Settings", "font_style", "Normal")
config.set("Settings", "font_info",
"You are using %(font)s at %(font_size)s pt")
with open(path, "w") as config_file:
config.write(config_file)
if __name__ == "__main__":
path = "settings.ini"
createConfig(path)
Level selects where the logging message will commence from
import logging
# add filemode="w" to overwrite
logging.basicConfig(filename="sample.log", filemode="w", level=logging.DEBUG)
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")
import os
os.name
'nt'
os.environ
{'ALLUSERSPROFILE': 'C:\\ProgramData',
'APPDATA': 'C:\\Users\\mike\\AppData\\Roaming',
'CLASSPATH': '.;C:\\Program Files\\QuickTime\\QTSystem\\QTJava.zip',
'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files',
'COMPUTERNAME': 'MIKE-PC' ......
os.getenv('APPDATA')
os.getcwd()
C:\Users\Kyle\Documents\Sparta\pre-assignment\python
os.mkdir('test_folder')
os.remove("test.txt")
os.rmdir('test_folder')
os.rename("test.txt", "pytest.txt")
os.path.exists(path)
- self-contained
- server-less
- config free
- can create any sqllite database using python
import sqlite3
connection = sqlite3.connect("mydb.db")
cursor = connection.cursor()
# create table
cursor.execute("""CREATE TABLE albums
(title text, artists text, release_date text,
publisher text, media_type text)
""")
# insert some data
cursor.execute("""INSERT INTO albums
VALUES ('Glow', 'Andy Hunter', '7/24/2012',
'Xplore Records', 'MP3')"""
)
# save data to database
conn.commit()
- Paramatarised query (?,?,?,?)
- handles the variable substitution
- safe way to avoid sql injection attacks by giving the variable name
# insert multiple records using the more secure "?" method
albums = [('Exodus', 'Andy Hunter', '7/9/2002', 'Sparrow Records', 'CD'),
('Until We Have Faces', 'Red', '2/1/2011', 'Essential Records', 'CD'),
('The End is Where We Begin', 'Thousand Foot Krutch', '4/17/2012', 'TFKmusic', 'CD'),
('The Good Life', 'Trip Lee', '4/10/2012', 'Reach Records', 'CD')]
cursor.executemany("INSERT INTO albums VALUES (?,?,?,?,?)", albums)
conn.commit()
sql = """
UPDATE albums
SET artist = 'John Doe'
WHERE artist = 'Andy Hunter'
"""
cursor.execute(sql)
conn.commit()
sql = """
DELETE FROM albums
WHERE artist = 'John Doe'
"""
cursor.execute(sql)
conn.commit()
sql = "SELECT * FROM albumns WHERE artist = 'Ice Cube'"
cursor.execute(sql)
print(cursor.fetchall() or fetchone())
import datetime
current_date = datetime.date(2025, 10, 8)
or
current_date = datetime.date.today()
current_date_time = datetime.datetime(2025, 10, 8, 12, 37, 24)
or
current_date_time = datetime.datetime.today()
or
current_date_time = datetime.datetime.now()
current_day.year
current_day.month
current_day.day
current_day.hour
current_day.minute
current_day.second
now = datetime.datetime.now()
then = datetime.datetime(2014, 4, 30)
delta = now - then
import time
time.gmtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=33, tm_sec=20, tm_wday=3, tm_yday=1, tm_isdst=0)
time.ctime(19535636343)
Wed Jan 21 21:39:03 2589
print("second 1")
time.sleep(5)
print("second 6")
time.time()
1759928406.8725529
import pdb
pdb.run('testing.main()')
import urlib.request
request_url = urlib.request.urlopen("mywebsite.com")
import requests
r = requests.get("mywebsite.com")
dir(r)
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close',
'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history',
'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw',
'reason', 'request', 'status_code', 'text', 'url']
- Low level API
- Defines usage pattern for all database connection packages
- SQLAlchemy’s dialect system is constructed around the operation of the DBAPI
postgresql+psycopg2://@localhost/test refers to the psycopg2 DBAPI/dialect combination
mysql+mysqldb://@localhost/test refers to the MySQL for Python DBAPI/dialect combination
- A single engine manages many individual DBAPI connections
- The engine is most effecient when created just once at the module level
from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://scott:tiger@hostname/dbname")
- most basic function of the engine is to provide access to a connection
- this can then invoke SQL statements
from sqlalchemy import text
with engine.connect() as connection:
result = connection.execute(text("SELECT song FROM albums"))
for row in result:
print(row)