Intermediate Python Flashcards
Advanced level python
How do you combine multiple .csv files into a single DataFrame?
Loop through each CSV file in the directory
Directory where the individual CSV files are saved
csv_directory = ‘/Users/Documents/Udacity/Political_News_2024/political_news’
Initialize an empty list to hold DataFrames
dataframes = []
for filename in os.listdir(csv_directory):
if filename.endswith(‘.csv’): # Check if the file is a CSV
file_path = os.path.join(csv_directory, filename)
df = pd.read_csv(file_path) # Save the CSV file
dataframes.append(df) # Append the DataFrame to the list
# Concatenate all DataFrames in the list
combined_news_df = pd.concat(dataframes, ignore_index=True)
# Get today’s date in YYYY-MM-DD format
today = datetime.today().strftime(‘%Y-%m-%d’)
# Define the path and name for the combined CSV file
combined_filename = os.path.join(csv_directory, f’combined_political_news_data_{today}.csv’)
# Save the combined DataFrame to CSV
combined_news_df.to_csv(combined_filename, index=False)
What are the steps to learning python
Learn the python language
Learn the standard library
Learn the ecosystem
What three questions are asked to verify if data is a collection?
Can I know the size, or length, of this data? If so, the data is Sized.
Can I produce elements from this data set, one at a time? If so, the data is Iterable.
Can I check whether an element is in this data set? If so, the data is a Container.
What questions should you ask when thinking about structure, performance, and clarity?
Is the data ordered (sequential), or unordered?
Is the data associative, mapping between keys and values?
Is the data unique?
Define mutability
whether an object in Python can be changed after it has been created.
Define hashability
If all data in a collection is immutable. An object is hashable if it has a fixed hash value for its lifetime and it implements the __hash__() method.
What does false mean in Python
Something that is False
What means nothingness?
None
Iterable
An object that can produce a stream of elements of data.
Mapping
An unordered collection associating elements of data (keys) to other elements of data (values).
Set
An unordered collection of unique data elements
Sized
An object that has a finite size
Structured data
Information to which we assign meaning through organizaiton
What is a bool
Either False or True and encodes binary values
What are the numeric values for a number?
int (integer) or float (real)
What is a string
An immutable sequence of text characters
What represents nothingness
None
What are the building blocks of data
bool
number
string
None
How do you escape special characters in string literals
\
print(‘doesn't’)
What does r”U\escaped” do?
It avoids ecaping, creating a raw string so special sequences U\escaped, \n (newline), or \t (tab) are treated as literal text, not as control characters.
name common methods for Strings
- greeting.find(‘lo’) # 3 (-1 if not found)
- greeting.replace(‘llo’, ‘y’) # => “Hey world!”
- greeting.startswith(‘Hell’) # => True
- greeting.isalpha() # => False (due to ‘!’)
- greeting.lower() # => “hello world! “
- greeting.title() # => “Hello World! “
- greeting.upper() # => “HELLO WORLD! “
- greeting.strip(‘dH !’) # => “ello worl”
Visualize how to convert the following:
str
int
float
bool
str(42) # => “42”
int(“42”) # => 42
float(“2.5”) # => 2.5
bool(None)
T or F: converting a value to a bool turns it into a boolean
F: it gives the value an essence of truthiness
bool(None) # => False
bool(False) # => False
bool(41) # => True
bool(‘abc’) # => True
What is a Python Object
A Python object can be thought of as a suitcase that has a type and contains data about its value. Everything in Python is an object.
isinstance(4, object) # => True
isinstance(“Hello”, object) # => True
Visualize how to evaluate a type function
type(1) # => <class ‘int’>
type(“Hello”) # => <class ‘str’>
type(int) # => <class ‘type’>
type(type(int)) # => <class ‘type’>
What are the characteristics of a Python Object?
everything in Python is an object
every Python object has a type
every Python object has an identity
Visualize how to evaluate a Python object identity
id(41) # => 4361704848 (for example)
An object’s “identity” is unique and fixed during an object’s lifetime.* Python objects are tagged with their type at runtime and contain a reference to their blob of data
What is a variable
a named reference to an object.
x = 5
What are namespaces?
A namespace (or symbol table) is an associative mapping from (variable) names to objects.
How do you access namespaces
locals()
globals()
What is abstraction?
Distilling a concept to its core ideas so it can apply to multiple scenarios.
Name the four keys to thinking in abstraction
Generalization - dstilling a concept to core idea for widespread application
Simplication - removing unncessary details
Focus on Why - think about the goal
Reusability and scalability - desire is to reuse across multiple scenarios
What comes after a dot (.)?
an attribute
What is a name?
A reference to an object
What are Objects?
Bundles of data associated with actions. Basically everything in Python.
Visualize a Class Object
class ClassName:
<statement>
<statement>
<statement></statement></statement></statement>
What are elements?
A single unit of data that is Boolean, and a number or text.
What are ways to represent collections
Sets - unordered, unique data like tags for a blog
Sequences - ordered, non-unique or unique data like a list of tasks
Mappings -associative data like a product name with an id, country and a country code
Container
An object that holds elements of data
Mutable
A property of a data collection where the top-level collection can be changed.
Schema
A representation or outline of a data model
Sequence
An ordered collection of data elements
What is 5 ** 2
25.
** acts as square
What is 7 // 2?
//Gives division with a floor.
What are f-strings
a string literal prefixed with f or F, allowing for embedding expressions with {}
f”{x}”
Visualize how to use the modulo operator for string interpolation
> > > name = “Jane”
“Hello, %s!” % name
‘Hello, Jane!’
Visualize how to use a tuple with the modulo operator
> > > name = “Jane”
age = 25
> > > ” Hello, %s! You’re %s years old.” % (name, age)
‘Hello, Jane! You’re 25 years old.’
Visualize how to use the str.format() method
> > > name = “Jane”
age = 25
> > > “Hello, {}! You’re {} years old.”.format(name, age)
“Hello, Jane! You’re 25 years old.”
or
“Hello, {1}! You’re {0} years old.”.format(age, name)
“Hello, Jane! You’re 25 years old.” ## uses indices to specify placements
or
“Hello, {name}! You’re {age} years old.”.format(name=”Jane”, age=25)
“Hello, Jane! You’re 25 years old.” ## improves readibility
Visualize how to use dictionaries to provide values for interpolating strings
> > > person = {“name”: “Jane”, “age”: 25}
> > > “Hello, {name}! You’re {age} years old.”.format(**person)
“Hello, Jane! You’re 25 years old.”
Visualize how to access strings by index
x = “string”
x[0] = “s”
x[-1] = “g”
visualize slicing string sequences
x = “string”
x[0:2] = “st”
what is slicing
Special syntax for accessing or updating elements or subsequences of data from a sequence type.
What is str?
An immutable sequence of characters.
what would
print(r”U\nescaped”) print?
U\nescaped
appending a r to the “” makes it a raw string, and ignores the escape. `
visualize string conversions
float()
int()
str()
bool(False)
What are false values
bool(None)
bool(0)
bool(0.9)
bool(“”)
T or F: bool([False]) is False
False. It’s true because it contains something in it.
Visualize how to remove a value from a list
list.remove(value)
visualize how to return number of occurrences from a list
list.count(value)
visualize how to return first index of value
list.index(value, [start, [stop]])
visualize how to return item at index
list.pop([index])
visualize how to sort in place
list.sort(key=None, reverse=False)
list.reverse()
what is a list
a finite, ordered, mutable sequence of elements.
letters = [‘a’, ‘b’, ‘c’, ‘d’]
visualize how to check if a str is an object
isinstance(“string”, object)
Return True if the object argument is an instance of the classinfo argument
Visualize how to get an objects identity
id(x)
xxxxxxxx
Visualize how to get the objects type
type(x)
what is a tuple?
an immutable sequence of arbitrary data.
v = ([1, 2, 3], [‘a’, ‘b’, ‘c’])
translate s[1:5:2]
for
s = ‘Udacity’
go between positions 2 and 4, taking a step size of 2
s[1:5:2] = ‘dc’
how do you reverse the elements of x?
x = ‘string’
x[::-1]
‘gnirts’
how would you modify the list at index 3?
letters = [‘a’, ‘b’, ‘c’, ‘d’]
letters[2] = 3
letters = [‘a’, ‘b’, 3, ‘d’]
visualize how to insert a value into a list
list.insert(x, index)
T or F: s is a tuple
s = (‘value’)
False. A ‘,’ has to follow ‘value’ or it is s becomes a string.
s = (‘value’,)
T or F: the below code would fail.
x = ([1,2,3], [‘a’,’b’,’c’])
x[0].append(4)
print(x)
False.
x = ([1,2,3], [‘a’,’b’,’c’])
x[0].append(4)
print(x)
Output
x = ([1,2,3,4], [‘a’,’b’,’c’])
Tuples aren’t immutable all the way down. You can modify what’s inside the lists.
What does packing and unpacking mean?
Any comma-separated values are packed into a tuple:
t = 12345, 54321
print(t) # (12345, 54321)
type(t) # => tuple
Any comma-separated names unpack a tuple of values (which must be of the same size)
x, y = t
x # => 12345
y # => 54321
Visualize variable-length tuple tuple packing/unpacking *start
Collect elements at the start
*start, x, y = (1, 2, 3, 4, 5)
print(start) # Output: [1, 2, 3]
print(x) # Output: 4
print(y) # Output: 5
Visualize variable-length tuple packing/unpacking for *end
Collect elements at the end
a, b, *end = (1, 2, 3, 4, 5)
print(a) # Output: 1
print(b) # Output: 2
print(end) # Output: [3, 4, 5]
Visualize variable-length tuple packing/unpacking using ignore
a, _, b, *rest = (1, 2, 3, 4, 5)
print(a) # Output: 1
print(b) # Output: 3
print(rest) # Output: [4, 5]
Visualize variable-length tuple packing/unpacking using nesting
data = (1, 2, (3, 4, 5))
a, b, (x, *rest) = data
print(a) # Output: 1
print(b) # Output: 2
print(x) # Output: 3
print(rest) # Output: [4, 5]
Visualize swapping using tuple packing/unpacking
x=5
y=6
x,y = y,x
Output:
x=6
y=5
for sequence = [‘red’, ‘green’, ‘blue’]
Why is the code:
for index, value in enumerate(sequence):
print(index, value)
better than the code:
for i in range(len(sequence)):
print(i, sequence[i])
enumerate() directly provides both the index (index) and the corresponding value (value)
This is a form of tuple packing/unpacking
Visualize how to make a string, list, or tuple
str([3, 4, 5]) # => ‘[3, 4, 5]’
list(“ABC”) # => [“A”, “B”, “C”]
tuple(“XYZ”) # => (“X”, “Y”, “Z”)
Visualize how to use .split
‘ham cheese bacon’.split() # => [‘ham’, ‘cheese’, ‘bacon’]
‘03-30-2016’.split(sep=’-‘) # => [‘03’, ‘30’, ‘2016’]
Visualize how to use .join
=> “Eric, John, Michael”
join creates a string from an iterable (of strings)
‘, ‘.join([‘Eric’, ‘John’, ‘Michael’])
What is mapping?
using hashable values to:
-Encoding associative information
-Capturing plain data with named fields.
-Building more complicated data structures.
Visualize the ways to create a dictionary
empty = {‘one’: ‘1’, ‘two’: 2, ‘three’ : 3}
dict(one = 1, two = 2, three = 3)
Visualize ways to modify the dictionary
d = {“one”: 1, “two”: 2, “three”: 3}
Access and modify
d[‘one’] = 22
Visualize how to use dict.get
temps = {‘CA’: [101, 115, 108], ‘NY’: [98, 102]}
temps.get(‘CA’) # => [101, 115, 108]
temps.get(‘KY’) # => None (not a KeyError!)
What are ways to remove elements in dictionarys?
d = {“one”: 1, “two”: 2, “three”: 3}
del d[“one”]
d.pop(“three”, default) # => 3
d.popitem() # => (“two”, 2)
d.clear() #removes everything
What is the .get()?
This method allows you to access a dictionary and specify a default value
a.get(‘five’, 0) #won’t have an error if five doesn’t exist.
Visualize how to loop over the values in a dictionary
for v in x.values():
print(v)
Visualize how to use a for loop to pack and unpack an dictionary
for key, value in x.items():
print(key,value)
What methods allow you to view keys, values, or items?
d.keys()
d.values()
d.items()
What is a set?
an unordered collection of distinct hashable elements
Can check: the size of, element containment, and loop over it
Visualize a set
empty_set = set()
set_from_list = set([1, 2, 3, 2, 3, 4, 3, 1, 2]) # => {1, 2, 3, 4}
if you want to create a set, you have to use set()
T or F: Sets can contain duplicates
False. Sets can only have unique values
What are some set operations that you can use?
a = set(“mississippi”) # {‘i’, ‘m’, ‘p’, ‘s’}
a.add(‘r’)
a.remove(‘m’) # Raises a KeyError if ‘m’ is not present.
a.discard(‘x’) # Same as remove
, except will not raise an error.
a.pop() # => ‘s’ (or ‘i’ or ‘p’)
a.clear()
len(a) # => 0
Visualize how to use mathematical operations on sets
a = set(“abracadabra”) # {‘a’, ‘b’, ‘c’, ‘d’, ‘r’}
b = set(“alacazam”) # {‘a’, ‘m’, ‘c’, ‘l’, ‘z’}
# Set difference
a - b # => {‘b’, ‘d’, ‘r’}
# Union
a | b # => {‘a’, ‘b’, ‘c’, ‘d’, ‘l’, ‘m’, ‘r’, ‘z’}
# Intersection
a & b # => {‘a’, ‘c’}
# Symmetric Difference
a ^ b # => {‘b’, ‘d’, ‘l’, ‘m’, ‘r’, ‘z’}
# Subset
a <= b # => False
What are the below named variations for the sets:
A. difference
B. union
C. intersection
D. symmetric difference
A. set.difference
B. set.union
C. set.intersection
D. set.symmetric_difference
E. set.issubset
Comprehensions
A convenient shorthand to build lists, sets, or dictionaries
ex.
for x in range(6):
squares.append(x**2)
This is the comprehension [x***2 for x in range(6)]
print(squares)
[0,1,4,9,16,25,36)
Visualize list comprehensions
[a transformation for an element in a collection (that meets criteria)]
[ x ** 2 for x in range(10) if x % 2 == 0]
# Lowercased versions of words in a sentence.
[word.lower() for word in sentence]
# Words in a sentence of more than 8 letters long.
[word for word in sentence if len(word) > 8]
What is the dictionary comprehension syntax
{key_func(vars): value_func(vars) for vars in iterable}
Visualize a dictionary comprehension
{student: grades for student, grades in gradebook.items() if sum(grades) / len(grades) > 90 builds a dictionary of student grades for students whose average grade is at least 90.