Python Flashcards

1
Q

What is the difference between “is” and “==”?

A

== 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a decorator?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you perform web scraping in Python?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are lookups faster with dictionaries or lists in Python?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some primitive data structures in Python? What are some user-defined data structures?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the different data types in Python?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is data smoothing and how do you do it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

For what is NumPy used? What are its benefits?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a cache database? And why would you use one?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Difference Between List,Tuple,Set and Dictionary

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly