<jupyter_start><jupyter_text>String FunctionsIf you want to do something with Strings, check the document first: [link](https://docs.python.org/2/library/string.html)<jupyter_code># change case
word = 'Hello'
print(word.lower())
print(word.upper())
# concatenation
print('1' + '2')
print('Hi' + ' there!')
# replication
print('12'*2)
print('1'*2 + '2'*3)
# strip
s = '         Extras \n'
print(s)
print(s.strip())
t = '****10****'
print(t)
print(t.strip('*'))
# split
s = 'Let\'s split the words'
print(s)
print(s.split(' '))
t = '<NAME>, Cars 5'
print(t)
print(t.split(','))
# slicing
word = 'Hello'
print(word[1:3])
print(word[4:7])
print(word[-4:-1])<jupyter_output>el
o
ell<jupyter_text><jupyter_code># substring testing
print(word)
print('HE' in word)
print('He' in word)
print(word.find('el'))
# convert to number
st = '1234'
print(int(st))
print(float(st))
# get's an error because 'Hello' isn't a number
print(int(word))
# string formatting. {} is a placeholder used by format function
statement = 'We love {} {}.'
print(statement.format('data','science'))

# we can specify the order of a placeholder
statement1 = 'We love {1} {0}'
print(statement1.format('data','science'))
# it changed the order of the placeholders<jupyter_output>We love data science.
We love science data<jupyter_text>---  ListsA list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets `[ ]`.<jupyter_code>list = [11,22,33]
list[1]

#will get an error: Error - index out of range
#list[3]<jupyter_output><empty_output><jupyter_text><jupyter_code>for i in list:
    print(i)
list[1]=95
list<jupyter_output><empty_output><jupyter_text><jupyter_code># appending to a list
list = [11,22,33]
list.append(44)
print(list)<jupyter_output>[11, 22, 33, 44]<jupyter_text><jupyter_code># deleting an element from a list by index
list = [11,22,33,44]
print(list)
list.pop(2)
print(list)
print('-'*16)
list = [11,22,33,44]
print(list)
list.remove(33)
print(list)
# removing an element from a list by value<jupyter_output>[11, 22, 33, 44]
[11, 22, 44]
----------------
[11, 22, 33, 44]
[11, 22, 44]<jupyter_text><jupyter_code># adding a list to a list: extend
list = [1,2,3]
list2 = [4,5,6]
list.extend(list2)
print(list)
# adding a list to a list: append
list = [1,2,3]
list2 = [4,5,6]
list.append(list2)
print(list)
# we get a list inside of a list
# zipping lists
list = [1,2,3]
list2 = [4,5,6]

for x,y in zip(list,list2):
    print(x,",",y)<jupyter_output>1 , 4
2 , 5
3 , 6<jupyter_text>---  TuplesA tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.<jupyter_code># tuples are IMMUTABLE
tuple1 = ('Honda','Civic',4,2017)
print(tuple1)
print(tuple1[0])
print('The len of this tuple is: %d' %len(tuple1))
print('-'*28)
for i in tuple1:
    print(i)
# gets an error: TypeError: 'tuple' object does not support item assignment    
#tuple1[3]=2018<jupyter_output>('Honda', 'Civic', 4, 2017)
Honda
The len of this tuple is: 4
----------------------------
Honda
Civic
4
2017<jupyter_text>---  DictionariesA dictionary maps a set of objects (keys) to another set of objects (values). A Python dictionary is a mapping of unique keys to values. Dictionaries are mutable, which means they can be changed. The values that the keys point to can be any Python value. Dictionaries are unordered, so the order that the keys are added doesn't necessarily reflect what order they may be reported back.<jupyter_code># dictionary basics
dict = {('Ghostbusters',2016): 5.4,
        ('Ghostbusters',1984): 7.8}
print(dict)
print(dict[('Ghostbusters',2016)])
print(len(dict))
# adding to a dictionary
print(dict)
dict[('Cars',2016)] = 7.1
print(dict)
# getting a value from a dictionary
x = dict[('Cars',2016)]
x

# if we ask for a value for a key not in the dictionary?
# we get a runtime error
# x = dict[('Toy Story',1995)]
# safer way to get from a dictionary
## 1) get method:
x = dict.get(('Cars',2016)) #exists
print(x)
x = dict.get(('Toy Story',1995)) #does not exist
print(x)
print('-'*28)
## 2) in keyword
x = ('Cars',2016) in dict # exists
print(x)
x = ('Toy Story',1995) in dict # does not exist
print(x)
# deleting from a dictionary
dict = {('Cars', 2016): 7.1, ('Ghostbusters', 1984): 7.8, 
        ('Ghostbusters', 2016): 5.4}
print(dict)
dict.pop(('Ghostbusters',2016))
print(dict)
# iterating over a dictionary
## returns the dictionary
dict = {('Cars', 2016): 7.1, ('Ghostbusters', 1984): 7.8, 
        ('Ghostbusters', 2016): 5.4}
for i in dict:
    print(i)
print('-'*28)    
## returns the dictionary and its key
for key,value in dict.items():
    print(key,":",value)
# be CAREFUL while iterating
dict = {('Cars', 2016): 7.1, ('Ghostbusters', 1984): 7.8, 
        ('Ghostbusters', 2016): 5.4}
for i in dict:
    dict.pop(i)
# selective removal
dict = {('Cars', 2016): 7.1, ('Ghostbusters', 1984): 7.8, 
        ('Ghostbusters', 2016): 5.4}
print(dict)
to_remove = []
## find keys which match a criteria - remove movies year < 2000
for i in dict:
    if(i[1] < 2000):
        to_remove.append(i)
## remove those movies by iterating through the list        
for i in to_remove:
    dict.pop(i)
print(dict)<jupyter_output>{('Cars', 2016): 7.1, ('Ghostbusters', 1984): 7.8, ('Ghostbusters', 2016): 5.4}
{('Cars', 2016): 7.1, ('Ghostbusters', 2016): 5.4}<jupyter_text>List and Dictionary ComprehensionComprehension is a way to build lists and dictionaries quickly<jupyter_code># list comprehension
list = [i**2 for i in range(1,11)]
print(list)
print('-'*39)
list = [i for i in range(0,21,2)]
print(list)
print('-'*39)
list = [i%2 for i in range(0,13)]
print(list)
print('-'*39)

import random
list = [random.randint(0,5) for i in range(0,10)]
print(list)
#dictionary comprehension
dict = {i : i**2 for i in range(1,10)}
print(dict)
print('-'*60)

dict = {i : chr(i) for i in range(65,91)}
print(dict)<jupyter_output>{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
------------------------------------------------------------
{65: 'A', 66: 'B', 67: 'C', 68: 'D', 69: 'E', 70: 'F', 71: 'G', 72: 'H', 73: 'I', 74: 'J', 75: 'K', 76: 'L', 77: 'M', 78: 'N', 79: 'O', 80: 'P', 81: 'Q', 82: 'R', 83: 'S', 84: 'T', 85: 'U', 86: 'V', 87: 'W', 88: 'X', 89: 'Y', 90: 'Z'}<jupyter_text>SetsIt supports a number of useful math operations and because they only allow unique elements.* Unordered;* Unique elements;* Support useful set operations (e.g., union, intersection)<jupyter_code># set basics: create, add and discart
## creating
leos_colors = set(['blue','green','red'])
print(leos_colors)
print('-'*35)
## adding
leos_colors.add('yellow')
print(leos_colors)
print('-'*35)
## discard
leos_colors = set(['blue','green','red'])
leos_colors.discard('green')
print(leos_colors)
# set operations: union and intersection
## union
leos_colors = set(['blue','green','red'])
ilkays_colors = set(['blue','yellow'])
either = ilkays_colors.union(leos_colors)
print(either)
print('-'*35)
## intersection
leos_colors = set(['blue','green','red'])
ilkays_colors = set(['blue','yellow'])
both = ilkays_colors.intersection(leos_colors)
print(both)<jupyter_output>{'red', 'green', 'yellow', 'blue'}
-----------------------------------
{'blue'}<jupyter_text>Sets - quick operations* Union can be done with the pipe (|) operator```set1 | set2```* Intersection can be done with the and (&) operator```set1 & set2```<jupyter_code>print('Union is:',leos_colors | ilkays_colors)
print('Intersection is:',leos_colors & ilkays_colors)<jupyter_output>Union is: {'red', 'green', 'yellow', 'blue'}
Intersection is: {'blue'}
{'fraction_non_alphanumeric': 0.1846915460776847, 'fraction_numerical': 0.05699416095455699, 'mean_word_length': 4.7218591140159765, 'pattern_counts': {'":': 1, '<': 42, '<?xml version=': 0, '>': 40, 'https://': 1, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '239209', 'license': 'Apache-2.0', 'n_tokens_mistral': 3182, 'n_tokens_neox': 2830, 'n_words': 907, 'path': 'Python for Data Science/Week 2 - Background in Python and Unix/Key Data Structures.ipynb', 'repo_name': 'mattssilva/Data-Science-Micromasters', 'size': '7880'}