Sunday, July 3, 2022
Python - File handling
try:
f = open("demofile.txt")
f.close()
print("Accessed file OK")
except FileNotFoundError:
print("File does not exist")
finally:
print("All done.")
"""
Syntax:
open("filename", "mode")
Default mode values:
r = read
t = text
These are the same
f = open("demofile.txt")
f = open("demofile.txt", "rt")
Open Modes:
r = read
a = append = Open for appending, creates if file does not exist
w = write = Open for writing, creates if file does not exist
x = create = Creates the file for writing, returns error if already exists
Handling modes:
t = text
b = binary
"""
# Open, read, and display a file
f = open("c:\\temp\\demofile.txt", "r")
print(f.read())
f.close()
# Open and read 5 characters
f = open("demofile.txt", "r")
# "This "
print(f.read(5))
f.close()
# Open and read a line
f = open("demofile.txt", "r")
# "This is"
print(f.readline())
f.close()
# Open and read two lines
f = open("demofile.txt", "r")
# "This is"
print(f.readline())
print(f.readline())
f.close()
# Open and loop through each line of the file
f = open("demofile.txt", "r")
for x in f:
print(x)
f.close
# Append to a file
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close
# Open and loop through each line of the file
f = open("demofile2.txt", "r")
for x in f:
print(x)
f.close
# Overwrite a file if it exists
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
# Delete a file
import os
os.remove("demofile2.txt")
# If the file exists, delete it
if os.path.exists("demofile3.txt"):
os.remove("demofile3.txt")
else:
print("The file does not exist")
Saturday, July 2, 2022
Python - String formatting
txt = "The price is {} dollars"
# The price is 49 dollars
print(txt.format(price))
name = "Fred"
txt = "The price is {} dollars, {}."
# The price is 49 dollars, Fred.
print(txt.format(price, name))
txt = "The price is {:.2f} dollars"
#The price is 49.00 dollars
print(txt.format(price))
quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
# I want 3 pieces of item number 567 for 49.00 dollars.
print(myorder.format(quantity, itemno, price))
myorder = "I have a {carname}, it is a {model}."
# I have a Ford, it is a Mustang.
print(myorder.format(carname = "Ford", model = "Mustang"))
Python - User input
# User input for a string
username = input("Enter username:")
print("Hello, " + username)
# Note that this only gets a string.
# It does not scan for key presses
Python - Error handling (try, except, else, finally)
try:
print(x)
# This will cause an error
#y = 3 / 0
except NameError:
# Catch a specific error type
print("Forgot to define x")
except:
# Catch all other errors
print("An exception occured")
else:
# Run this if there was no error
print("All is well")
finally:
# Do this regardless of error status
print("That's all folks")
# Throw an error on purpose
x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero")
Python - Regular Expressions
import re
txt = "The rain in Spain"
# This searches for:
# ^The = "The" at the beginning of the line (^)
# Spain$ = "Spain" at the end of the line ($)
x = re.search("^The.*Spain$", txt)
print(x)
# Pretty much any populated variable evaluates to True
# So if we got a match, that value is tucked into x
# Which means this will evaluate to True if we got a match
if bool(x) == True:
print("We got a match")
else:
print("We did not geta match")
# Split returns a list where the string has been split at each match
# So the next line returns all the words separated by a space:
# The, rain, in, Spain
words = re.split(" ", txt)
print(words)
# Sub replaces one or many matches with a string
new = re.sub("Spain", "England", txt)
# The rain in England
print(new)
# Review regular expression matching
# A quick review sheet is here:
# https://www.w3schools.com/python/python_regex.asp
Python - Math
myValues = (5, 10, 25)
# minimum, maximum
x = min(myValues)
y = max(myValues)
# 5, 25
print(x)
print(y)
# Absolute value
x = abs(-7.25)
# 7.25
print(x)
# x to the power of y
z = pow(4,3)
# 64
print(z)
print("----")
# Imported math functions
import math
x = math.sqrt(64)
print(x)
# Ceiling = round up to nearest integer
# Floor = round down to nearest integer
x = math.ceil(1.4)
y = math.floor(1.4)
# 2, 1
print(x)
print(y)
# PI is a constant (3.14....)
x = math.pi
# 3.14....
print(x)
Python - Dates
x = datetime.datetime.now()
# 2022-07-02 18:23:57.824673
print(x)
# The datetime module has many methods, here are some
# 2022
print(x.year)
# Saturday
print(x.strftime("%A"))
# Create a date object
x = datetime.datetime(2020, 5, 17)
# 2020-05-17 00:00:00
print(x)
# Format date objects into readable strings via strftime()
# Good listing at w3schools:
# https://www.w3schools.com/python/python_datetime.asp
x = datetime.datetime(2018, 6, 1)
# %B = Month name
# June
print(x.strftime("%B"))
# %Y = year
# 2018
print(x.strftime("%Y"))
# %m = month as a two digit number
# 06
print(x.strftime("%m"))
# %d = day as a two digit number
# 01
print(x.strftime("%d"))
# There's probably a better way to do it than this
myFormattedDate = x.strftime("%Y") + "-" \
+ x.strftime("%m") + "-" \
+ x.strftime("%d")
# 2018-06-01
print(myFormattedDate)
# This works the same way
myFormattedDate = x.strftime('%Y-%m-%d')
print(myFormattedDate)
# Today's date
# 2022-07-02 18:42:02.671457
print(datetime.datetime.today())
# 2022-07-02
print(datetime.datetime.today().strftime('%Y-%m-%d'))
Python - Modules
Modules are like libraries.
They let you tap into other people's code and packages.
For example, say there is a file named "mymodule.py" with these contents (between the dashed lines):
---------
def greeting(name):
print("Hello, " + name)
---------
We would then access the module with the code below:
import mymodule
# Hello, John
mymodule.greeting("John")
Here is how to access variables stored in a module:
Filename: mymodule.py:
---------
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
---------
How I would use it:
import mymodule
a = mymodule.person1["age"]
#36
print(a)
There are built-in modules. For example, platform
We can display a list of functions in a module like this:
import platform
x = dir(platform)
print(x)
Python - Scope
# It is only available from inside region it is created
def myfunc():
print("Inside function")
x = 300
print(x)
print("Exiting function")
# 300
myfunc()
# Next line fails
#print(x)
print("----")
# This sequence sets and displays a variable
# It then calls a function which *looks* like it messes with same variable
# But it shows that when we exit the function,
# the variable is intact
x = 7
# 7
print(x)
# From inside function, we will see 300
myfunc()
# 7
print(x)
print("=======")
# This shows that outer variables can be accessed from inner regions
# Define a global variable
outer = "my outer variable"
def myfunc():
inner = "my inner variable"
# Works as expected - we can see both
print(outer)
print(inner)
myfunc()
print("<><><><><><><>")
# Use global keyword to declare a variable global from within an inner region
def myfunc():
global surprise
surprise = 777
myfunc()
# the surprise variable is now treated as a global
# 777
print(surprise)
print("<><><><><><><>")
# We can change global variables from within an inner region
# by using the global keyword
x = 300
def myfunc():
global x
x = 200
myfunc()
# 200
print(x)
Python - Classes and Inheritance
x = 5
p1 = MyClass()
# 5
print(p1.x)
print("---")
# built-in __init__() function is called on object instatiation
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
# John, 36
print(p1.name)
print(p1.age)
print("===")
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
# Hello my name is John
p1.myfunc()
#36
print(p1.age)
# Set property
p1.age = 40
# 40
print(p1.age)
# <class '__main__.Person'>
# So this tells us it is an object of type Person
print (type(p1))
# Delete an object
del p1
# A class with no definition
class Person:
pass
print("******")
# Inheritance, Parents, Children
# First define a class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
# John Doe
x.printname()
# Now create a child of the parent
class Student(Person):
pass
y = Student("Sarah", "Smith")
# Sarah Smith
y.printname()
print("------------")
# Notice that we never defined a method called printname() in the child class
# But the Student inherited the methods of the parent class (Person)
class Student(Person):
def __init__(self, fname, lname):
#add properties here
pass
y = Student("Sarah", "Smith")
# This next line breaks because we over-rode the inheritance of the parent
# by defining our own __init__() function in the chil
# In other words, the child's __init__() function overrides the inheritance
# of the parent's __init()__ function
#y.printname()
# We fix it by adding a call to the parent's __init__() function
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
y = Student("Sarah", "Smith")
# Now this works
# Sarah Smith
y.printname()
print("------------")
# By using super() we automatically inherit the methods and properties from parent
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019
y = Student("Sarah", "Smith")
# Sarah Smith
y.printname()
# 2019
print (y.graduationyear)
print("============")
# Make graduationyear a variable
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
y = Student("Sarah", "Smith", 2022)
# Sarah Smith
y.printname()
# 2022
print (y.graduationyear)
print("<><><><><><>")
# Add a method
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
Python - Functions
def my_function():
print("Hello from a function")
my_function()
# So apparently we can redefine functions
# Notice the function name below is the same one defined above
def my_function(fname):
print(fname + " Smith")
my_function("John")
my_function("Jane")
# Two args
def my_function(fname, lname):
print (fname + " " + lname)
my_function("John", "Doe")
my_function("Jane", "Smith")
# This will break because not right number of args
#my_function("Pat")
#my_function("Richard", "Paul", "Astley")
print ("---")
def my_function(*kids):
for kid in kids:
print(kid)
# Prints John, Jane, Richard
my_function("John", "Jane", "Richard")
print ("---")
# You can specify arguments instead of relying on order
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
# Displays Linus
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
print ("===")
# Default parameter values
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
# Displays "I am from Norway"
my_function()
my_function("Brazil")
# Return a value
def my_function(x):
return 5 * x
# Displays 15, 25, 45
print(my_function(3))
print(my_function(5))
print(my_function(9))
print ("---")
# Empty functions
def myfunction():
pass
Python - For loops
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# Prints each letter in 'banana', one on each line
for x in "banana":
print(x)
print ("---")
# Break exits the loop
# Displays apple, then banana, and then breaks out of the loop
# (So it never gets to cherry)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
print ("---")
# This does the same thing but the break is before the print statement
# So we only see "apple"
# Recall that we are breaking out of the FOR loop, not just the IF condition
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
print ("===")
# continue says stop the current iteration of the loop
# and continue with the next one
# apple
# rotten!
# cherry
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
print("rotten!")
continue
print(x)
# Displays 0 through 5
for x in range(6):
print(x)
print ("===")
# Displays 2 through 5
for x in range(2, 6):
print(x)
print ("===")
# Weird
# Start at 2
# Keep going up to right before 30
# Increment by the third argument: 3
# So it displays 2, 5, 8, 11, 14, 17, 20, 23, 26, and 29
for x in range(2, 30, 3):
print(x)
print("<><><><>")
# Displays 0 through 5 and then executes the else statement
# The else keyword says run this block of code when the loop statement is finished
for x in range(6):
print(x)
else:
print("Finally finished!")
print("<><><><>")
# Displays 0
for x in range(1):
print(x)
print("<><><><>")
# Displays 0, 1
for x in range(2):
print(x)
print("<><><><>")
# Displays 0, done
for x in range(1):
print(x)
else:
print("done")
print("<*><*><*><*>")
# Displays done
for x in range(0):
print(x)
else:
print("done")
# The break statement completely exits out of the for loop
# It will not execute the else block
# Displays 0, 1, 2
print ("----")
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
# Fun with nested loops
# Displays red apple, red banana, red cherry, big x3, tasty x3
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
# What you have to do if no content in a for loop
for x in [0, 1, 2]:
pass
Python - While loops
# Displays 1 through 5
i = 1
while i < 6:
print(i)
i += 1
print("---")
# break statement
# Displays 1 through 3
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
print("---")
# continue statement
# Displays 1 through 6
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
print("---------")
# Displays 1 through 5, and then executes the else statement
# While ... Else
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Python - If... Else
a = 33
b = 200
if b > a:
print("b is greater than a")
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
print("---")
a = 200
b = 33
# Short Hand If
if a > b: print("a is greater than b")
# Short Hand If ... Else
a = 2
b = 330
print("A") if a > b else print("B")
print("-----")
# AND
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
# Parentheses seems to work
if (a > b) and (c > a):
print("Both conditions are True")
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
print("<><><><>")
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
# If statements cannot be empty but we can use pass
a = 33
b = 200
if b > a:
# Do nothing
# Python won't let me just have comments here
# I have to use the pass statement
pass
Python - Dictionaries
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
print(thisdict)
# Ford
print(thisdict["brand"])
# 3
print(len(thisdict))
mymodel = thisdict["model"]
# Mustang
print (mymodel)
mykeys = thisdict.keys()
# dict_keys(['brand', 'model', 'year'])
print (mykeys)
myvalues = thisdict.values()
# dict_values(['Ford', 'Mustang', 1964])
print (myvalues)
myitems = thisdict.items()
# dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
print (myitems)
print("---")
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# Change a value
thisdict["year"] = 2018
# 2018
print(thisdict["year"])
thisdict.update({"year": 2020})
# 2020
print(thisdict["year"])
# Add an item
thisdict["color"] = "red"
# {'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'red'}
print (thisdict)
# This updates the value
# If the value does not exist, it will add it
thisdict.update({"color": "blue"})
thisdict.update({"wheels": 4})
# {'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'blue', 'wheels': 4}
print (thisdict)
# Pop removes the last inserted item and returns the item
# 5
print(len(thisdict))
x = thisdict.popitem()
# ('wheels', 4)
print(x)
# 4
print(len(thisdict))
del thisdict["color"]
del thisdict["model"]
# {'brand': 'Ford', 'year': 2020}
print(thisdict)
# Clear the dictionary of all items
thisdict.clear()
# {}
print(thisdict)
# Remove the dictionary completely
del thisdict
# Next line will cause an error
#print(thisdict)
print("---")
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# This will loop through and show the keys
# brand
# model
# year
for x in thisdict:
print(x)
# This will loop through and show the values
# Ford
# Mustang
# 1964
for x in thisdict:
print(thisdict[x])
print("---")
# Ford
# Mustang
# 1964
for x in thisdict.values():
print(x)
# brand
# model
# year
for x in thisdict.keys():
print(x)
print("---")
# Gnarly - loop through both keys and values
# brand Ford
# model Mustang
# year 1964
for x, y in thisdict.items():
print(x, y)
print("======")
# Copy a dictionary
# Truly a copy and not a reference
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
print("---")
mydict["year"] = 2020
print(mydict)
print(thisdict)
print("---")
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
# <class 'dict'>
print(type(myfamily))
# <class 'dict'>
print(type(myfamily["child1"]))
# {'name': 'Emil', 'year': 2004}
print(myfamily["child1"])
# Emil
print(myfamily["child1"]["name"])
# <class 'str'>
print(type(myfamily["child1"]["name"]))
Python - Set joins
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
# {'a', 1, 2, 3, 'c', 'b'}
print(set3)
# Add items from set2 to set1
set1.update(set2)
print (set1)
# Both union and update exclude duplicates
# intersection_update keeps only items that are present in both sets
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
# {'apple'}
print(x)
# intersection() returns a NEW set of items present in both sets
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
# {'apple'}
print(z)
# Keep all but NOT the duplicates
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
# {'cherry', 'microsoft', 'google', 'banana'}
# Notice no 'apple'
print(x)
Python - Add and remove items from a set
# Unchangeable means you cannot change the item in a set,
# but you CAN add or remove items from the set
# sets use curly brackets {}
myset = {"apple", "banana", "cherry"}
# No duplicates
myset = {"apple", "banana", "cherry", "apple"}
# {'apple', 'cherry', 'banana'}
print (myset)
# 3
print(len(myset))
# Loop through a set
for x in myset:
print(x)
# Add item
myset.add("orange")
# {'orange', 'banana', 'cherry', 'apple'}
print(myset)
# Add items from another set to myset
tropical = {"pineapple", "mango", "papaya"}
myset.update(tropical)
# {'orange', 'apple', 'mango', 'papaya', 'banana', 'pineapple', 'cherry'}
print(myset)
myset.remove("orange")
# {'pineapple', 'cherry', 'mango', 'papaya', 'banana', 'apple'}
# (notice no 'orange')
print(myset)
# If you try to remove the item and it does not exist,
# Python will raise an error
# myset.remove("orange")
# However, the discard() method will NOT raise an error
myset.discard("pineapple")
print(myset)
myset.discard("pineapple")
print(myset)
# Pop removes the last item from the set
# Its return value is the item removed
x = myset.pop()
print(myset)
print("Notice that ", x , " no longer exists in the set: " , myset)
print("---")
# Clear removes all items from the set
myset.clear()
print(myset)
del myset
# This would raise an error because the set no longer exists
#print(myset)
Friday, July 1, 2022
Python - Unpacking Tuples
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
# apple
print(green)
# banana
print(yellow)
# cherry
print(red)
print("---")
# This will cause an error because there are
# too many values to unpack
# into only three variables
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
#(green, yellow, red) = fruits
# So use an asterisk * instead
(green, yellow, *red) = fruits
# apple
print(green)
# banana
print(yellow)
# ['cherry', 'strawberry', 'raspberry']
print(red)
# All the remaining variables got shoved into a list called "red"
# <class 'list'>
print(type(red))
(green, *tropic, red) = fruits
print("---")
# apple
print(green)
# You will notice that Python assigned all the values to "tropic"
# except the last one so it matched the number of variables we provided.
# ['banana', 'cherry', 'strawberry']
print(tropic)
# raspberry
print(red)
Python - Tuples
# Tuples use parentheses ()
mylist = ["apple", "banana", "cherry", "apple"]
mytuple = ("apple", "banana", "cherry", "apple")
#<class 'list'>
#<class 'tuple'>
print (type(mylist))
print (type(mytuple))
# Length of tuple
# 4
print(len(mytuple))
# A tuple with only one item is a bit gnarly
# Notice the ending comma:
thistuple = ("apple",)
# If we forget the comma it is a string
myString = ("apple")
# <class 'str'>
print (type(myString))
# Since tuples are "unchangeable", we must jump thru some hoops
# if we *do* want to change one.
# Set up a tuple
x = ("apple", "banana", "cherry")
# Create a list type of the tuple
y = list(x)
# Make your change
y[1] = "kiwi"
# Now redefine the original tuple as a tuple
# but give it the contents of the changed list
x = tuple(y)
print(x)
# Adding items to an unchangeable tuple is similar
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
print(thistuple)
print("---")
# Removing an item from an unchangeable tuple is similar
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
print(thistuple)
Python - Join Lists
# One way
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
# Another way
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
# A third way
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Python - Copy lists
list2 = list1
# Right now these are identical
print (list1)
print (list2)
list1.append('orange')
# Notice that the new item is also added to list2
# ['apple', 'banana', 'cherry', 'orange']
print (list1)
print (list2)
# I think this means list2 is a pointer referencing list1.
# So changes made to list1 are seen in list2,
# and vice-versa.
list2.pop()
# ['apple', 'banana', 'cherry']
print (list1)
print (list2)
# So we need a way to actually *copy* the list
list2 = list1.copy()
list1.append('orange')
list2.pop()
# Now we're cooking with gas
# ['apple', 'banana', 'cherry', 'orange']
# ['apple', 'banana']
print (list1)
print (list2)
print("---")
# Another way to copy a list
list1 = ["apple", "banana", "cherry"]
list3 = list(list1)
# ['apple', 'banana', 'cherry']
print (list3)
list3.insert(2, "popsicle")
#['apple', 'banana', 'cherry']
#['apple', 'banana', 'popsicle', 'cherry']
print (list1)
print (list3)
Python - Sort lists
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
# ['banana', 'kiwi', 'mango', 'orange', 'pineapple']
print(thislist)
# I guess Python knows to sort numerically instead of alphabetically
# if the items in the list are all numeric
thislist = [100, 50, 65, 82, 23]
thislist.sort()
# [23, 50, 65, 82, 100]
print(thislist)
# Sort descending
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
# ['pineapple', 'orange', 'mango', 'kiwi', 'banana']
print(thislist)
# Sort numbers descending
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
# [100, 82, 65, 50, 23]
print(thislist)
# Sort is case sensitive
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort()
# ['Kiwi', 'Orange', 'banana', 'cherry']
print(thislist)
# How to do a case insensitive sort
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort(key = str.lower)
# ['banana', 'cherry', 'Kiwi', 'Orange']
print(thislist)
# This reverses the items in a list
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
# ['cherry', 'Kiwi', 'Orange', 'banana']
print(thislist)
Python - List Comprehension
# It is a shortcut
# W3Schools gives an example where you have a list of fruits
# and want to return a new list containing only the fruits
# with the letter "a" in the name
# The typical way:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
# This will loop through all the items in the list "fruits"
for x in fruits:
# If the letter "a" is in the item
if "a" in x:
# Then add this item to the new list
newlist.append(x)
# ['apple', 'banana', 'mango']
print(newlist)
# We can use list comprehension to do the same thing
# but with fewer lines of code
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
# ['apple', 'banana', 'mango']
print(newlist)
# You can also manipulate the items in the list before you return
# the result like this:
newlist = [x.upper() for x in fruits]
# ['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO']
print(newlist)
Python - Loop through lists
thislist = ["apple", "banana", "cherry"]
# This will show each item in the list, one line per item
# apple
# banana
# cherry
for x in thislist:
print(x)
# 3
print (len(thislist))
# range(0, 3)
print (range(len(thislist)))
print("---")
# This will print each index number of the list: 0, 1, and 2
for i in range(len(thislist)):
print(i)
# This will print each item in the list
# by specifing each index number in the list:
for i in range(len(thislist)):
print(thislist[i])
print("---")
# Loop through with a while loop
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i += 1
# Looping with "List Comprehension"
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
Python - Add or remove items from a list
myList = ["apple", "banana", "cherry"]
myList.append("orange")
print(myList)
# Add multiple items to a list
myList = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
myList.extend(tropical)
# ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
print(myList)
# Insert an item mid-list
myList = ["apple", "banana", "cherry"]
myList.insert(1,"orange")
# ['apple', 'orange', 'banana', 'cherry']
print(myList)
# Remove an item from a list
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
# ['apple', 'cherry']
print(thislist)
# Pop method to remove the list item from a list
thislist = ["apple", "banana", "cherry"]
thislist.pop()
# ['apple', 'banana']
print(thislist)
# Pop method with an index value targets that item in list
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
# ['apple', 'cherry']
print(thislist)
# Delete method
thislist = ["apple", "banana", "cherry"]
del thislist[0]
# ['banana', 'cherry']
print(thislist)
# Delete the entire list
thislist = ["apple", "banana", "cherry"]
del thislist
# This will generate an error because the list itself is deleted,
# not simply emptied of all items.
#print(thislist)
# This is the way to empty all items from a list
thislist = ["apple", "banana", "cherry"]
thislist.clear()
# []
print(thislist)
Thursday, June 30, 2022
Python - Lists
# Displays all the items in the list
print(thislist)
# Displays only index item 0 of the list (apple)
print(thislist[0])
# How many items in the list?
# 3
print(len(thislist))
list1 = ["abc", 34, True, "Pecan"]
print(list1)
# class 'list'
print (type(thislist))
# Negative indexing
# cherry
print(thislist[-1])
# 0 1 2 3 4 5 6
bigList = ["Adam", "Bob", "Charlie", "David", "Ed", "Frank", "Greg" ]
# Charlie, David, Ed
# So confusing. Stops before the upper end
# W3schools says: "Start at index 2 (included) and end at index 5 (not included)
print(bigList[2:5])
# Ends at index 3 (not included)
# Adam Bob Charlie
print(bigList[:3])
# Starts at index 3 and goes to the end
# David Ed Frank Greg
print(bigList[3:])
# Starts with Ed, three from the end
# Ends with the last one, but does not include the last one
# So winds up being:
# Ed Frank
print(bigList[-3:-1])
# This will replace index 1 (Bob) with the value of "Babette"
# Remember that indexes start with 0,
# so "1" would be the SECOND index position (Bob)
bigList[1] = "Babette"
# Adam Babette Charlie...
print (bigList)
# Replace index values #2 and #3
# Remember it stops before #4
bigList[2:4] = ["Camille", "Denise"]
# Adam Babette Camille Denise Ed Frank Greg
print (bigList)
# 7 items
print (len(bigList))
# Replace index position 1 with two list
bigList[1:2] = ["Becca", "Cagney"]
# ['Adam', 'Becca', 'Cagney', 'Camille', 'Denise', 'Ed', 'Frank', 'Greg']
print (bigList)
# Now 8 items
print (len(bigList))
# Insert a list item at index position 4
# It becomes the new index position 4; everything else is moved toward the end
bigList.insert(4, "Daphne")
# ['Adam', 'Becca', 'Cagney', 'Camille', 'Daphne', 'Denise', 'Ed', 'Frank', 'Greg']
print (bigList)
# 9
print (len(bigList))
Wednesday, June 29, 2022
Python - Lists, Tuples, Sets, and Dictionaries
# Lists use a square bracket []
# Tuples are ordered, unchangeable, and allow duplicate values
# Tuples use parentheses ()
# Sets are UNORDERED, unchangeable, and do NOT allow duplicates
# Sets use curly brackets {}
# Dictionaries are ordered, changeable, and do NOT allow duplicates
# Dictionaries use curly brackets {} but specify key:value pairs
mylist = ["apple", "banana", "cherry", "apple"]
mytuple = ("apple", "banana", "cherry", "apple")
myset = {"apple", "banana", "cherry"}
mydictionary = {
"brand": "chiquita",
"model": "banana",
"year": 2022
}
# ['apple', 'banana', 'cherry', 'apple']
print(mylist)
# ('apple', 'banana', 'cherry', 'apple')
print(mytuple)
# {'banana', 'cherry', 'apple'}
print(myset)
# {'brand': 'chiquita', 'model': 'banana', 'year': 2022}
print(mydictionary)
Thursday, June 23, 2022
Python - Booleans
print(10 > 9)
# Returns false
print(10 == 9)
# Returns false
print (10 < 9)
a = 100
b = 9
# Prints "a is greater than b"
if a > b:
print ("a is greater than b")
else:
print ("a is not greater than b")
print ("out of the if/else")
print("---")
# Most values are true
# These all display "True"
print(bool("abc"))
print(bool(True))
print(bool(123))
print(bool(["apple", "cherry", "banana"]))
print("---")
# But some values are false
print(bool(False))
print(bool(None))
print(bool(0))
print(bool(""))
print(bool(()))
print(bool([]))
print(bool({}))
print("---")
# Functions can return True or False
def myFunction():
# Is John awesome?
return True
print(myFunction())
if myFunction():
print ("John is awesome")
else:
print ("You are mistaken")
print("---")
Wednesday, June 22, 2022
Python - String methods
There are a bunch of string methods listed over at W3Schools.
Here are some I particularly like:
count() - Returns the number of times a specified value occurs in a string
find() - Searches the string for a specified value and returns the position of where it was found
index() - Searches the string for a specified value and returns the position of where it was found
isalnum() - Returns True if all characters in the string are alphanumeric
isalpha() - Returns True if all characters in the string are in the alphabet
isdecimal() - Returns True if all characters in the string are decimals
isdigit() - Returns True if all characters in the string are digits
isnumer() - Returns True if all characters in the string are numeric
isprintable() - Returns True if all characters in the string are printable
lstrip() - Returns a left trim version of the string
replace() - Returns a string where a specified value is replaced with a specified value
rfind() - Searches the string for a specified value and returns the last position of where it was found
rindex() - Searches the string for a specified value and returns the last position of where it was found
rstrip() - Returns a right trim version of the string
split() - Splits the string at the specified separator, and returns a list
startswith() - Returns true if the string starts with the specified value
strip() - Returns a trimmed version of the string
zfill() - Fills the string with a specified number of 0 values at the beginning
Python - Escape characters
# Because of the quotes around the word "never"
#txt = "This will "never" work"
# The fix is to escape the quotes
txt = "This will \"never\" work"
print(txt)
"""
\' = Single quote
\\ = Backslash
\n = New line
\r = Carriage return
\t = Tab
\b = Backspace
\f = Form feed
"""
Python - Formatting strings
# A simple approach
myVar = "caveman"
txt = "So easy a " + myVar + " could do it"
print(txt)
# Fancy
age = 36
txt = "My name is John, and I am not {}"
print(txt.format(age))
# Fancier
age = 36
name = "John"
txt = "My name is {}, and I am not {}"
print(txt.format(name, age))
# Fancy-pants
# Notice the order
quantity = 3
itemno = 567
price = 49.95
myOrder = "I want to pay {2} dollars for {0} pieces of item {1}."
# This will print "I want to pay 49.95 dollars for 3 pieces of item 567."
print(myOrder.format(quantity, itemno, price))
print ("---")
# The following line of code will never work
# Because of the quotes around the word "never"
#txt = "This will "never" work"
# The fix is to escape the quotes
txt = "This will \"never\" work"
print(txt)
Python - Concatenate strings
a = "Hello, World!"
# Prints HELLO, WORLD!
print(a.upper())
# Prints hello, world!
print(a.lower())
print("---")
a = " Hello, World! "
# Prints "Hello, World!"
# Notice the space at the beginning and end is removed (stripped)
print(a.strip())
print("---")
print("Destructive?")
# Destructive to original contents?
a.strip()
# No. Not destructive. This will print the original with spacing intact
print (a)
# So we gotta do this?
a = a.strip()
print (a)
print("Split examples:")
# This will return a list
b = a.split(",")
print (b)
# Told you it was a list
print (type(b))
print ("Type: " + str(type(b)))
Python - Left, Right, and Middle string ranges
# Python stops right before the upper bound
# So this will print "llo"
# Character positions 2, 3, and 4
# Remember that strings start at 0
b = "Hello, World!"
print(b[2:5])
# By leaving out the starting parameter,
# we can print the first five characters
# This prints "Hello"
b = "Hello, World!"
print(b[:5])
# Conversely, if we leave out the end parameter
# we print to the end of the string
# Therefore, this prints "llo World!"
b = "Hello, World!"
print(b[2:])
# Use negatives indexes to count from the end
# to print the last character of the string
# This will print the last character (!)
b = "Hello, World!"
print(b[-1])
# This will print the "W"
print(b[-6])
# Getting fancy:
# Print from W to the end of the string
print(b[-6:])
Python - Fun with strings and random numbers
myVar = "hello"
# First character is position 0
# So this will print "h"
print(myVar[0])
print("----")
# This will print 0 through 4
for x in range(5):
print(x)
print("Now out of for loop")
print("----")
# This will loop through the string myVar
# It will print each letter on a different line
# "hello" = 5 characters
# Positions 0 through 4
for x in range(5):
print (myVar[x])
print("Now out of for loop")
print("----")
# Lazier way of looping through the word
print("Lazy:")
for x in myVar:
print(x)
print("----")
# Print a random letter of the string
# Need to import the module
import random
# By default the seed will use the system time if we leave it blank
random.seed()
myRandomNumber = random.randrange(0, len(myVar) )
print(myVar[myRandomNumber])
print("----")
# Do it 30 times, each time printing a different random letter
# This is a good way to force errors if I have my upper/lower bounds wrong
for x in range(30):
myRandomNumber = random.randrange(0, len(myVar) )
print(myVar[myRandomNumber])
Python - Data Types
x = str("Hello World")
print (x)
print (type(x))
# Will display <class 'str'>
x = int(20)
print(type(x))
x = float(20.5)
print(type(x))
x = complex(1j)
print(type(x))
x = list(("apple", "banana", "cherry"))
print(type(x))
x = tuple(("apple", "banana", "cherry"))
print(type(x))
x = range(6)
print(type(x))
x = dict(name="John", age=36)
print(type(x))
x = set(("apple", "banana", "cherry"))
print(type(x))
x = frozenset(("apple", "banana", "cherry"))
print(type(x))
x = bool(5)
print(type(x))
x = bytes(5)
print(type(x))
x = bytearray(5)
print(type(x))
x = memoryview(bytes(5))
print(type(x))
Python - Global variables
x = "awesome"
def myfunc():
x = "cool"
print("Python is " + x) # Output: Python is cool
myfunc()
print("Python is " + x) # Output: Python is awesome
def myfunc2():
# Notice here we turn what would normally have been a local variable 'y' into a global variable
# This means it is accessible outside this function
global y
y = "fantastic"
print ("Python is " + y) # Output: Python is fantastic
myfunc2()
print ("Python is " + y) # Output: Python is fantastic
Python - Start here
# This is a comment
x = 5 # This is another comment
y = 2
"""
This is an interesting way to make
a multi-line comment.
But it is probably convenient to comment out a bunch of code this way.
"""
z = "Hello world"
if x > y:
print("Five is greater than 2")
print("Out of the if branch")
# Here we see the variables were set above
print("x:")
print(x)
print("y:")
print(y)
print("z:")
print(z)
# Now we redefine the variables to different types on the fly
x = "apple"
y = "banana"
z = 400
# Here we see the redefined variables worked
print("x:")
print(x)
print("y:")
print(y)
print("z:")
print(z)
# This tells us the data type
print("z is a")
print(type(z))
# This lets us recast a variables type as a different type
z = str(z)
print("z:")
print(z)
print("z is a")
print(type(z))
# Both single and double quotes work
firstName = "John"
lastName = 'Doe'
myVar = "John"
myVAR = "Doe"
# Case sensitive variable names
print("myVar:")
print(myVar)
print("myVAR:")
print(myVAR)
# Assign 3 variables three different values
x, y, z = "John", "Doe", "Smith"
print(x)
print(y)
print(z)
# Assign 3 variables the same value is evil!
multi = variable = declaration = "evil"
print(multi)
print(variable)
print(declaration)
# Unpacking is cool: This snippet "unpacks" a list into three different variables
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x) # x will be set to apple
print(y) # y will be set to banana
print(z) # z will be set to cherry
# Here is a way of printing all three variables on the same line
print(x, y, z) # Output: apple banana cherry
# Here is another way but no space in between each value
print(x + y + z) # Output: applebananacherry
x = 10
y = 5
print(x + y) # Output: 15
x = "John"
y = 5
z = "years old"
print(x, "is", y, z) # Output: John is 5 years old
exit()
Tuesday, June 21, 2022
Python - Table of contents
I am working my way through the Python tutorial and taking notes.
I keep my notes here for ease of access (home/work/travel).
I find this is the best way for me to remember and then refer back to a language.
I tend to skip stuff that makes sense to me and go into more detail on things I have trouble with.
So this is more for me than you, weary traveler.
The full W3Schools tutorial can be found here:
https://www.w3schools.com/python/default.asp
Table of contents
Start here
Global variables
Data types
Fun with strings and random numbers
Left, Right, and Middle string ranges
Concatenate strings
Formatting strings
Escape characters
String methods
Booleans
Lists, Tuples, Sets, and Dictionaries
Lists
Add or remove items from a list
Loop through lists
List Comprehension
Sort lists
Copy lists
Join lists
Tuples
Unpacking tuples
Add and remove items from a set
Set joins
Dictionaries
If... Else
While loops
For loops
Functions
Classes and Inheritance
Scope
Modules
Dates
Math
Regular Expressions
Error handling (try, except, else, finally)
User input
String formatting
File handling
Friday, February 11, 2022
Read Mac file systems from Windows
--> Linux Reader: Freeware Linux Reader for Windows <--
This is the one I used to read an HFS drive.
APFS read only is a recent addition
Does not handle Encrypted APFS
https://www.diskinternals.com/linux-reader/
Free
Linux Reader Pro
$30 - Not free but handles Encrypted APFS
Other options:
HFSExplorer
Free but only handles HFS, is read only, and requires Java.
Paragon
Paragon software comes up in web searches but the inexpensive $50 version is only for home use. For business alternatives, Paragon redirects the user to a $600 version. I don't know if you *have* to buy the $600 if you are a business. Limited to only APFS?
MacDrive
Handles both HFS and APFS. Seems like the most powerful of the options (support for Mac-formatted RAID disks, verify & repair Mac formatted drives). Maybe the best for a tech who needs a lot of options.
$50