Python II Flashcards
(15 cards)
Primary data
Data collected by the individual doing the analysis
Experiments (labs)
Observations (surveys/sensors)
Simulations (theoretical models)
Scraping (webscraping)
Secondary data
Data collected by someone else and published for public use
Any primary data collected by someone else
Institutionalized data bands (census)
Random binomial
import numpy
print(numpy.random.binomial(n, p, size)
N = number of trials per experiment
P = probability of success per trial
Size = number of experiments
Slicing a string
string[first_index : last_index]
Mutability
Data types that can be changed are mutable
Data types that can’t be changed, like strings, are immutable
Escape character
Adding \ will escape the special character
“He said, \”blueberries are my favorite!\””
Boolean conditions with strings
print(“e” in “blueberry” and “e” in “carrot”)
Returns False
print(“e” in “blueberry” and not “e” in “carrot”)
Returns True
Splitting strings using escape sequences
‘\n’ will allow us to split by line breaks
‘\t’ will allow us to split by tabs
Replacing string method
string_name.replace(substring_being_replaced, new_substring)
Python dictionary
Unordered set of keys associated with values
Syntax to create
Menu = {“toast”: 6, “juice”: 5, “muffin”: 2}
Keys can be string or number
values can be any type of data
Syntax to add to a dictionary
dictionary[key] = value
Adding multiple keys to a dictionary at once
.update()
dictionary.update({“key1”:value1, “key2”:value2, …})
Dict comprehension
dictionary = {key:value for key,value in zip(list1, list2)
.get() for dictionaries
dictionary.get(‘key’, return value if none)
Will return value if the key exists
If the key does not exist it will return None
You can specify the return value if None with second argument
Remove an entry from dictionary using .pop()
dictionary.pop(key, return value if key does not exist)
Creating a dictionary with zip
Dictionary = {key:value for key, value in zip(list1,list2)