<jupyter_start><jupyter_text>Standard Libraryhttps://docs.python.org/3/library/<jupyter_code># usually we want the imports as the first cell 
# in other words we put imports in the beginning of our code
import string #meaning I have access now to extra string goodies
string.ascii_letters
string.punctuation
# for example if you wanted to get rid of all punctuations in some Text
text = """A brown fox: said "(something) "    :/... and so on"""
text
clean_text = text
for bad_char in string.punctuation:
    clean_text = clean_text.replace(bad_char, "") 
    # loop through all the characters in my punctuation and get replace with nothing
clean_text
# you do not want to calculate dates yourself, 
# easy to make mistakes with timezones, Georgian calendar, leap years etc
import datetime
now = datetime.datetime.now() #notice datetime needs another datetime a bit uglyl
now
time_passed = datetime.datetime.now() - now
time_passed
time_passed.seconds
time_passed.days
type(now)
type(time_passed)
# difference that datetime has more properties than time_passed
now.year, now.day, now.date()
type(now)
from datetime import datetime as dtime #you can create alias to a longer module
really_now = dtime.now() 
really_now
really_now.weekday() # it is a method not a property
weekdays=["Mon","Tue","Wed", "Thu","Fri","Sat","Sun"]
print(f"Today is {weekdays[now.weekday()]}")
import calendar
cal = calendar.Calendar()
# TODO find where actual English weekdays are stored , cssclasses?
cal.firstweekday
# turns out weekday starts on Monday with 0 and Sunday is 6
from collections import Counter
cnt = Counter("Abracababra my magic")
cnt.most_common()
cnt.most_common(3)
sentence = "A quick brown fox jumped over a sleeping fox and that's a wrap"
word_count = Counter(sentence.split()) # we split sentence by default whitespace
word_count.most_common()
sentence = "A quick brown fox jumped over a sleeping fox and that's a wrap"
word_count = Counter(sentence.lower().split()) # we split sentence first normalize by default whitespace
word_count.most_common()
# compare to not splitting then we get characters
char_count = Counter(sentence)
char_count.most_common()
text = "See spot run. See spot jump. See spot run."
sentences = text.split(".")
sentences
sentences = text.split(".")
print(sentences)
sentences = [sentence.strip() for sentence in sentences] # we strip whitespace for each sentence
print(sentences)
scounter = Counter(sentences)
scounter.most_common()
import math
math.cos(3.14)
math.cos(math.pi)
math.pi
math.e
phi = ( 1 + math.sqrt(5) ) / 2
phi
phi = ( 1 + 5**0.5) / 2
phi
math.log2(256)
math.log10(1_000_000)
import random

random.seed(1504) # seed guarantees same specific pseudo-random values
# good for testing but obviously this line not good if you need fresh values
random.random(),random.random()
# in old days we would do this
# https://www.amazon.com/Million-Random-Digits-Normal-Deviates/dp/0833030477#customerReviews
# https://xkcd.com/221/
random.randint(1,6) # 6 is included, quite rare in programming
million_dice = [random.randint(1,6) for _ in range(1_000_000)] # _ because i do not need the value
sum(million_dice)
sum(million_dice)/len(million_dice)
import statistics
statistics.mean(million_dice) # so you already have this in statistics
statistics.stdev(million_dice)
statistics.mode(million_dice) # most often used number
dcount = Counter(million_dice)
dcount.most_common()
my_alphabet = list(string.ascii_lowercase)
my_alphabet[:5],my_alphabet[-5:]
# you do not want to write your own shuffle algorithm 
# it is easy to make mistakes
random.shuffle(my_alphabet) 
# this will be IN PLACE meaning my_alphabet will get shuffled
my_alphabet[:10]
random.choice(my_alphabet) # single random choice, my_alphabet stays intact
new_alphabet = random.choices(my_alphabet, k=len(my_alphabet)) 
# returns a new shuffled list with unique picks keeping old one untouched
my_alphabet[:5], new_alphabet[:10]
random.choices(range(5), k=5) # we got doubles
random.sample(range(5), k=5) # no double, just random sample
random.sample(range(5), k=4) # no double, just random sample
random.sample(range(5), k=6) # no double, just random sample
import os
os.listdir() # list all the files/directories in your current directory
os.getcwd() # get current working directory
# should be different to you
# for files there is newer Pathlib library which is recommended
import itertools
# we save writing of 3 nested loops
list(itertools.product("ABC", range(4), ["Boo","Foo"])) # so 3 * 4 * 2 = 24 

list(itertools.permutations("ABC")) # itertools not surprisingly give us iterable not a list
# also combinations etc at https://docs.python.org/3/library/itertools.html#module-itertools<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09773160907356371, 'fraction_numerical': 0.01695993216027136, 'mean_word_length': 5.401628222523745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '214066', 'license': 'MIT', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1427, 'n_words': 527, 'path': 'core/Python Standard Library.ipynb', 'repo_name': 'ValRCS/Python_DIP720_RTU_Spring_2021', 'size': '4717'}