In [5]:
fileName = "2023-04-13_finances.txt"

def fileYear(filename: str) -> int:
    """
    Returns the year in the file name filename.
    """
    return int(filename[:4])

def fileMonth(filename: str) -> int:
    """
    Returns the month in the file name filename.
    """
    return int(filename[5:7])

def fileDay(filename: str) -> int:
    """
    Returns the day in the file name filename.
    """
    return int(filename[8:10])

# fileName = "2023-04-13_finances.txt"

def fileSubject(filename: str) -> str:
    """
    Returns the subject in the file name filename.
    """
    return filename[11:-4]

print(fileYear(fileName), fileMonth(fileName), fileDay(fileName))
print(fileSubject(fileName))
2023 4 13
finances
In [7]:
lst = [8, 6, 7, 5, 3, 0, 9]
x = lst
lst.sort()
print(lst)
print(x)
[0, 3, 5, 6, 7, 8, 9]
[0, 3, 5, 6, 7, 8, 9]
In [8]:
"a" < "z"
Out[8]:
True
In [10]:
lst = ["f", "a", "c", "e"]
lst.sort()
print(lst)
['a', 'c', 'e', 'f']
In [11]:
lst = [99, "bottles of beer on the wall"]
lst.sort()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [11], in <cell line: 2>()
      1 lst = [99, "bottles of beer on the wall"]
----> 2 lst.sort()

TypeError: '<' not supported between instances of 'str' and 'int'
In [14]:
print([2] < [3])
print([2, 1] < [2])
True
False
In [16]:
x = [[3], [2], [2, 1]]
y = x
x.sort()
print(x)
print(y)
[[2], [2, 1], [3]]
[[2], [2, 1], [3]]
In [17]:
lst = [8, 6, 7, 5, 3, 0, 9]
x = lst[:]
x.sort()
print(lst)
print(x)
[8, 6, 7, 5, 3, 0, 9]
[0, 3, 5, 6, 7, 8, 9]
In [18]:
s = "Hello world"
s2 = sorted(s)
print(s2)
[' ', 'H', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
In [19]:
def lgg(lst: list[float]) -> tuple[float, float, float]:
    """
    Returns the least value, greatest value, and greatest gap between values in lst
    as a tuple.
    """
    s = sorted(lst)
    greatestGap = 0.0
    for idx in range(0, len(lst)-1):
        gap = s[idx+1] - s[idx]
        if gap > greatestGap:
            greatestGap = gap
            
    return (s[0], s[-1], greatestGap)

print(lgg([8, 6, 7, 5, 3, 0, 9]))
(0, 9, 3)
In [22]:
lst = [1, 2, 3]
help(lst.sort)
#x = lst.sort()
#print(x)
Help on built-in function sort:

sort(*, key=None, reverse=False) method of builtins.list instance
    Sort the list in ascending order and return None.
    
    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
    order of two equal elements is maintained).
    
    If a key function is given, apply it once to each list item and sort them,
    ascending or descending, according to their function values.
    
    The reverse flag can be set to sort in descending order.

In [24]:
x = [2, 4, 6, 0, 1]
x.sort(reverse=True)
print(x)

print(sorted([2, 4, 6, 0, 1], reverse=True))
[6, 4, 2, 1, 0]
[6, 4, 2, 1, 0]
In [26]:
x = ["an", "excellent", "list", "of", "strings"]
x.sort(key=len)
print(x)
['an', 'of', 'list', 'strings', 'excellent']
In [27]:
def printAndLen(s: str) -> int:
    """
    Prints s and returns its length.
    """
    print(s)
    return len(s)

x = ["an", "excellent", "list", "of", "strings"]
x.sort(key=printAndLen)
print(x)
an
excellent
list
of
strings
['an', 'of', 'list', 'strings', 'excellent']
In [30]:
def parity(val: int) -> int:
    """
    Returns the parity of val.
    """
    return val%2

lst = [8, 6, 7, 5, 3, 0, 9]
s = sorted(lst, key=parity)
print("After sorted, lst is", lst)
print("Sorted:", s)
lst.sort(key=parity)
print("After .sort, lst is", lst)
After sorted, lst is [8, 6, 7, 5, 3, 0, 9]
Sorted: [8, 6, 0, 7, 5, 3, 9]
After .sort, lst is [8, 6, 0, 7, 5, 3, 9]
In [31]:
VOWELS = "aeiou"

def vowels(s: str) -> int:
    """
    Returns the number of vowels in s.
    """
    res = 0
    for ch in s:
        if ch in VOWELS:
            res = res + 1
    return res

lst = ["these", "are", "just", "some","of", "my", "favo[u]rite", "words"]
lst.sort(key=vowels)
print(lst)
['my', 'just', 'of', 'words', 'these', 'are', 'some', 'favo[u]rite']
In [33]:
lst = [1, 2, 3]
help(lst.reverse)
help(lst.index)
Help on built-in function reverse:

reverse() method of builtins.list instance
    Reverse *IN PLACE*.

Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.list instance
    Return first index of value.
    
    Raises ValueError if the value is not present.

In [37]:
s = "Hello"
help(s.join)
help(s.split)

s = ".".join(["ab", "pq", "rs"])
print(s)
lst = s.split(sep=".")
print(lst)
Help on built-in function join:

join(iterable, /) method of builtins.str instance
    Concatenate any number of strings.
    
    The string whose method is called is inserted in between each given string.
    The result is returned as a new string.
    
    Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'

Help on built-in function split:

split(sep=None, maxsplit=-1) method of builtins.str instance
    Return a list of the words in the string, using sep as the delimiter string.
    
    sep
      The delimiter according which to split the string.
      None (the default value) means split according to any whitespace,
      and discard empty strings from the result.
    maxsplit
      Maximum number of splits to do.
      -1 (the default value) means no limit.

ab.pq.rs
['ab', 'pq', 'rs']
In [38]:
info = (
    "Richards",
    "Gregor",
    1.76,
    "University of Waterloo",
    "Purdue University",
    2014,
    12
)

print(info[0])
Richards
In [40]:
info = {
    "surname": "Richards",
    "given name": "Gregor",
    "height": 1.76,
    "employer": "University of Waterloo",
    "alma mater": "Purdue University",
    "graduation year": 2014,
    "employment years": 12
}

print(
    info["given name"], info["surname"],
    "works at", info["employer"]
)
Gregor Richards works at University of Waterloo