<jupyter_start><jupyter_text>tally: arcane, grim, esoteric, corpus, persnickety, menagerie  Lists: a review<jupyter_code>x = [5, 10, 15, 20, 25, 30]
x[3]
[2, 4, 6, 8, 10][4]
x[90]
type(x)
type(x[0])
len(x)
len([10])
type([10])
len([])
x
max(x)
min(x)
sum(x)
sorted(x)
#takes a list and returns it sorted in numerical or alphabetical order
sorted([17, -4, 1004, 3, 15, 8.3])
sorted(["badger", "crocodile", "aardvark", "zebra", "emu"])
range(10)
list(range(10))
list("this is a test")<jupyter_output><empty_output><jupyter_text>using expressions as indices<jupyter_code>x[3]
n = 1 + 2
x[n]
x[(4 + 2)//3]
#side note what is thaaaaat<jupyter_output><empty_output><jupyter_text>Negative indices<jupyter_code>x[-1]
x[-2]
x[-3]
x[-90]<jupyter_output><empty_output><jupyter_text>List slices<jupyter_code>x
# _xprlist[__exprint_:__exprint__]
x[1:4]
x[2:5]
n = 2
x[n:n+3]
x[-3:-1]
x[3:9000]
#just givesyou what's included, no indicator you're way off base
type(x[1:4])
for item in x[2:5]:
    print(item)<jupyter_output>15
20
25<jupyter_text>omitting slice values<jupyter_code>x[0:4]
x[:4]
#at beginning, is zero
x[4:]
#at end, gives you to the end
x[-3:]
x[-2:]
x[:-1]
x[1:]<jupyter_output><empty_output><jupyter_text>List comprehensions<jupyter_code>#very common to take a list, apply some transformation to it to get another list
#another thing that's very common is to take a list, filter it, and end up with another list

x
source = [3, -1, 4, -2, 5, -3, 6]
dest = []
for item in source:
    if item > 0:
        dest.append(item * item)
dest
[item * item for item in source if item > 0]
ark = ["aardvark", "badger", "crocodile", "dingo", "emu", "flamingo"]
zoo = []
for item in ark:
    if len(item) <= 6:
        zoo.append(item)
zoo
[item for item in ark if len(item) <= 6]
#[_expr_ for _temp_ in _source_ if _expression_ ]
#if... is optional
x
#what i want is: [2, 7, 12, 17, 22, 27]
stuff = []
for item in x:
    stuff.append(item - 3)
stuff
[item - 3 for item in x]
source = [3, -1, 4, -2, 5, -3, 6]
# modulo operator: %
#gives you remainder, basically
#can be used to determine if a number is odd/even, just modulo 2 ya know
60 % 5
60 % 7
dest = []
for i in range(10):
    if i % 2 == 0:
        dest.append(i*i)
dest
[i*i for i in range(10) if i % 2 == 0]
#in python any number other than zero is considered to be True<jupyter_output><empty_output><jupyter_text>a slightly  more practical example<jupyter_code>rawdata = "2,3,5,7,11,13,17,19,23"
rawdata.split(",")
values = rawdata.split(",")
sum(values)
#that doesn't work
int("17")
srcstr = "1237"
type(srcstr)
converted = int(srcstr)
type(converted)
#int: str -> int() -> int
int("-17")
values
int(values)
#it does not work!
num_values = [int(i) for i in values]
#values is source list
#predicate expression is calling the function on the thing
sum(num_values)
sum([int(i) for i in rawdata.split(",")])
stuff = "hello there how are you"
stuff.split(" ")
#split divides things into a list based on what the divider is
stuff.split("e")
for i in stuff.split("e"):
    print(i)<jupyter_output>h
llo th
r
 how ar
 you<jupyter_text>string operationsthe `in` operator<jupyter_code>"foo" in "buffoon"
#if the string on the left is a substring of the string on the right
"foo" in "reginald"
ark
[animal for animal in ark if 'a' in animal]
for animal in ark:
    if 'a' in animal:
        print(animal)
check = "foodie"
check.startswith("foo")
check.startswith("f")
check.startswith("blah")
check.startswith("Foo")
#case-sensitive, FYI
check.isdigit()
#will say True if it is entirely numeric
number_str = "12345"
number_str.isdigit()
number_str = "12,345"
number_str.isdigit()
number_str = "123.45"
number_str.isdigit()
#.islower() .isupper()
check.islower()
check.isupper()
yelling = "I LIKE THINGS AND THYE ARE GOOD"
yelling.isupper()<jupyter_output><empty_output><jupyter_text>String indexes and slices<jupyter_code>message = "bungalow"
message[0]
message[-1]
message[3:-2]<jupyter_output><empty_output><jupyter_text>Finding substrings<jupyter_code>src = "Now is the winter of our discontent"
src.find("win")
src.find("lose")
location = src.find("win")
src[location:]
src[-1]
location = src.find("z")
if location != -1:
    print(src[location:location+2])
location = src.find("z")
print(location)
print(src[location:location+2])
src
src.count("is")
#count takes a substring and counts how many it appears in the string
src.count(" is ")
my_is_patterns = ["is", " is ", "is ", " is", "Is"]
for item in my_is_patterns:
    print(item, src.count(item))
for vowel in ['a', 'e', 'i', 'o', 'u']:
    print(vowel, src.count(vowel))
src2 = "Someone tell me where the poetry is."
src3 = "is this really all the poetry you have?"<jupyter_output><empty_output><jupyter_text>String transformations<jupyter_code>comment = "ARGUMENTATION! DISAGREEMENT! STRIFE!"
comment.lower()
comment
message = "e.e. cummings is not happy about this"
message.upper()
str1 = "dog"
str2 = "Dog"
#so say i wanted to compare these but not case-sensitively
str1 == str2
str1.lower() == str2.lower()
movie = "dr. strangelove, or, how i learned to stop worrying and love the bomb"
movie.title()
movie2 = "rosemary's baby" 
#you would think, "<NAME>"
movie2.title()
#but no
rawtext = "      weird extra spaces before and after      "
rawtext.strip()
line = "hello there this is a line of text\n"
#\n is NEW LINE
print(line)
print(line.strip())
song = "I got rhythm, I got music, I got my man, who could ask for anything more"
step1 = song.replace("I got", "I used to have")
#turning it into a COUNTRY SONG
print(step1)
step2 = step1.replace("more", "less")
print(step2)
telegram = song.replace("I got", "someday I will have").replace("anything more", "a future more bright").replace(",", " STOP").upper() + " STOP"

print("original:", song)
print("much improved:", telegram)
song
#original stays the same eh
rawdata = "Get data that<br>looks like this<br>because it was"
print(rawdata)
print(rawdata.replace("<br>", "\n"))<jupyter_output>Get data that
looks like this
because it was<jupyter_text>A difficult problem<jupyter_code>input_str = "Yes, y zip code is 12345. I heard that Gary's zip code is 23456. But 212 is not a zipc code."
print(input_str)
# results in: ["12345", "23456"]
current = ""
zips = []
for ch in input_str:
    if ch.isdigit():
        current += ch
    else:
        current = ""
    if len(current) == 5:
        zips.append(current)
        current = ""
zips<jupyter_output><empty_output><jupyter_text>Regular expressions<jupyter_code>import re
zips = re.findall(r"\d{5}", input_str)
zips
#that means finds five digits in a row<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14935557240333586, 'fraction_numerical': 0.03244882486732373, 'mean_word_length': 4.931654676258993, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '371728', 'license': 'MIT', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2503, 'n_words': 788, 'path': '07-notebook-and-data/614notes.ipynb', 'repo_name': 'kbennion/foundations-hw', 'size': '6604'}