Python Flashcards
What is the difference between “is” and “==”?
== is used to check if two objects have the same value.
Is is used to determine if two variables point to the same object in memory.
Example:
say we make a list a = [1,2,3]
and set a = b
both a == b and a is b would be true
now lets say we used a slice operator and made a copy of a called c.
a == c would be true since its the same value.
but the slice operator created a new object in memory so a is c would be false
What is a decorator?
a decorator is a function that takes another function as an argument and returns a closure.
if i had a function called add_one_wrapper that took the function add_one as an argument.
In add one i take a number and return one added to ti.
How would you perform web scraping in Python?
First, I’d use the request library to access the URL and extract data using BeautifulSoup. With the raw data, I would convert it into a structure suitable for pandas and then clean the data using pandas and NumPy. Finally, I would save the data in a spreadsheet.
Are lookups faster with dictionaries or lists in Python?
Dictionaries are faster because you dont have to go through all the keys to access a value.
Lists have a linear relationship to time complexity. you have to go through a list to find the value you want
What are some primitive data structures in Python? What are some user-defined data structures?
The built-in data types in Python include lists, tuples, dictionaries, and sets. These data types are already defined and supported by Python and act as containers for grouping data by type. User-defined data types share commonalities with primitive types, and they are based on these concepts. But ultimately, they allow users to create their data structures, including queues, trees, and linked lists.
What are the different data types in Python?
What is data smoothing and how do you do it?
Data smoothing is a technique that eliminates noise from a dataset, effectively removing or “smoothing” the rough edges caused by outliers. There are many different ways to do this in Python. One option would be to use a library like NumPy to perform a Rolling Average, which is particularly useful for noisy time-series data.
For what is NumPy used? What are its benefits?
NumPy is one of the most popular Python packages, along with pandas and Matplotlib. NumPy adds data structures, including a multidimensional array, to Python, which is used for scientific computing. One of the benefits of using NumPy arrays is that they’re more compact than Python lists, and therefore, it consumes less memory.
What is a cache database? And why would you use one?
A cache database is a fast storage solution for short-lived structured or unstructured data. Generally, this database is much smaller than a production database and can store in memory.
Caching is helpful for faster data retrieval because Users can access the data from a temporary location. There are many ways to implement caching in Python, and you can create local data structures to build the cache or host a cache as a server, for example.
Difference Between List,Tuple,Set and Dictionary
Lists are mutable. Tuples are immutable. Sets are mutable and have no duplicate elements. Dictionaries are mutable and keys do not allow duplicates.
Lists are declared with square braces. Tuples are enclosed within parenthesis. Sets are represented in curly brackets. Dictionaries are enclosed in curly brackets in the form of key-value pairs.