<jupyter_start><jupyter_text>Strings<jupyter_code>x = 'This is a String Type in Python'
# indexing 
print (x[0])
print (x[-1])
# range
print (x[:4])
print (x[-6:])
# string basic ops 
# '+' to concate 
# '*' to repeate
first = 'John'
last = 'Doe'
print (first + ' ' + last)
print (first * 4 + ' ' + last * 2)<jupyter_output><empty_output><jupyter_text>Data Files and Summary Statistics<jupyter_code>import csv
%precision 2
with open('mpg.csv') as csvfile:
    mpg = list(csv.DictReader(csvfile))
mpg[:3]
# keys 'columns' for any row in csv file
mpg[2].keys()
# values of the record
mpg[2].values()
# number of records
len(mpg)
# printing the CSV data 
for rec in range(len(mpg)):
    print (mpg[rec].values())
# Average of mpg across all cars
sum(float(i['mpg']) for i in mpg)/len(mpg)
# grouping the cars by number of cylinder, and finding the average cty mpg for each group
cylinders = set(d['cylinders'] for d in mpg)
cylinders # unique level of numbers of cylinders
yearmodel = [] 
for c in cylinders:
    sumc = 0
    yearcount = 0
    for d in mpg:
        if d['cylinders'] == c:
            yearcount +=1
            sumc += float(d['mpg'])
    yearmodel.append((c,sumc/yearcount))

yearmodel.sort(key=lambda x:x[0])
yearmodel<jupyter_output><empty_output><jupyter_text>Date and Time in Python<jupyter_code>import datetime as dt
import time as tm
tm.time() # return timestamp/current time in milliseconds since epoch from 1 Jun 1970
time = dt.datetime.fromtimestamp(tm.time()) # convert timestamp to datetime (year/mon/day hour/min/second)
time.time()
time.date()
# create delta for time operation  -- 20 days ago
delta = dt.timedelta(days=20)
delta
# get today 
today = dt.date.today()
today
today - delta # date from delta(20 days ago)<jupyter_output><empty_output><jupyter_text>functional programming and .map()<jupyter_code>a = [1,2,3,4]
b = [3,4,5,6]
cheep = map(max,a,b)
cheep
for i in cheep:
    print (i)
def st(x):
    if isinstance(x,str):
        return True ## most preferred way to check if it's list
    else:
        return False

st("sdsds")
string_map = map(st,['1',23,'str'])
for i in string_map:
    print (i)<jupyter_output>True
False
True<jupyter_text>Lambda  : for creating anonymouse function<jupyter_code># lambda must return express
add = lambda a,b : a + b
mul = lambda a,b : a * b
exp = lambda a : a**a
add(2,3)
exp(3)
people = ['Dr. <NAME>', 'Dr. <NAME>', 'Dr. VG <NAME>', 'Dr. <NAME>']

def split_title_and_name(person):
    return person.split()[0] + ' ' + person.split()[-1]
p = lambda person: person.split()[0] + ' ' + person.split()[-1]
#option 1
for person in people:
    print(split_title_and_name(person) == (lambda p: p.split()[0] + ' ' + p.split()[-1])(person))

#option 2
#list(map(split_title_and_name, people)) == list(map(???))
list(map(split_title_and_name, people)) == list(map(lambda p: p.split()[0] + ' ' + p.split()[-1], people))<jupyter_output><empty_output><jupyter_text>List Comperhension ; condition and loop in one line<jupyter_code># before list comperhension we use for-loop and condition
even = []
for i in range(0,100):
    if i % 2 == 0:
        even.append(i)
print (even)
# using list comperhension
evens = [ev for ev in range(0,100) if ev % 2 == 0]
print (evens)
even == evens
# conver nested loop to list comperhension
def times_tables():
    lst = []
    for i in range(10):
        for j in range (10):
            lst.append(i*j)
    return lst

ttable = [i*j for i in range(10) for j in range(10)]
ttable == times_tables()
lowercase = 'abcdefghijklmnopqrstuvwxyz'
digits = '0123456789'

correct_answer = [a+b+c+d for a in lowercase for b in lowercase for c in digits for d in digits]

print (correct_answer[:50])# Display first 50 ids<jupyter_output>['aa00', 'aa01', 'aa02', 'aa03', 'aa04', 'aa05', 'aa06', 'aa07', 'aa08', 'aa09', 'aa10', 'aa11', 'aa12', 'aa13', 'aa14', 'aa15', 'aa16', 'aa17', 'aa18', 'aa19', 'aa20', 'aa21', 'aa22', 'aa23', 'aa24', 'aa25', 'aa26', 'aa27', 'aa28', 'aa29', 'aa30', 'aa31', 'aa32', 'aa33', 'aa34', 'aa35', 'aa36', 'aa37', 'aa38', 'aa39', 'aa40', 'aa41', 'aa42', 'aa43', 'aa44', 'aa45', 'aa46', 'aa47', 'aa48', 'aa49']<jupyter_text>Numpy<jupyter_code>import numpy as np
# create a list convert it to numpy array 
np.array([1,2,3,4])
# multidimensional array -- list of list 
mul = np.array([[1,2,3],[3,4,5],[4,6,6]])
type(mul)
mul.shape
# generating numbers 
arr = np.arange(0,20,2)
arr
# linspace returns evenly spaced numbers over a specified interval.
arr = np.linspace(0,1,5)
arr
arr.reshape(5,2)
arr = np.arange(0,30,2)
arr
# reshaping array dimensions
arr.reshape(3,5)
y = np.array([4, 5, 6, 3, 4, 5, 6])
y
np.diag(y)
# create array after evalueate expression
np.array([1, 2, 3] * 3)
# replicate each elemet x times after each other
np.repeat([1, 2, 3], 3)
# there are ones/zeros/diag "make elements in diagonal" /eye "ones on the diagonal else where is zeros"
p = np.ones([2, 3],int)
p
# append vertically
np.vstack([p,[5,6,7]])
# append horizontally
np.hstack([p, [[9],[10]]])<jupyter_output><empty_output><jupyter_text>2D arrays<jupyter_code>x = np.arange(36)
x = x.reshape((6,6))
x
x[:,3:6]<jupyter_output><empty_output><jupyter_text>Note that sub-array is reference to the original  so make sure you .copy() array before editing the sub array  Iterating over arrays<jupyter_code>test = np.random.randint(0,10,(5,5))
test
for r in test:
    print (r)
len(test)
for r in range(len(test)):
    print(test[r])
# for getting the index
for i, r in enumerate(test):
    print ('Row ',i,'> ',r)
test2 = test**2
test2<jupyter_output><empty_output><jupyter_text>Iterate through both arrray<jupyter_code>zip(test,test2)
zip([1,2,3])
for i, j in zip(test,test2):
    print (i,'+',j, '=',i+j)
lambda a : a + 2
 ['a', 'b', 'c'] + [1, 2, 3]
test
test[::7]
test[:,::1]
np.arange(36).flatten()[::7]
test = np.arange(36).reshape(6,6)
test
test[2:4,2:4]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16938775510204082, 'fraction_numerical': 0.04608843537414966, 'mean_word_length': 4.682125603864734, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '604144', 'license': 'MIT', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2283, 'n_words': 654, 'path': 'PythonEssentials/Python Essentials and Numpy.ipynb', 'repo_name': 'deebify/python-ml', 'size': '5925'}