Priority 5 Flashcards
Which is faster, Python lists or Numpy arrays?
NumPy arrays
Why are NumPy arrays faster than Python lists?
NumPy arrays are implemented in C versus Python lists are implemented in Python. Because C is a compiled language, it is faster than Python, which is an interpreted language.
What are the differences between Python lists and tuples?
3 bullet points
- Lists are mutable whereas tuples are not.
- Lists are defined using square brackets
[]
whereas tuples are defined using parentheses()
. - Tuples are generally faster than lists given immutability, allowing for code optimization.
What are the similarities between Python lists and tuples?
3 bullet points
- Both collection of objects.
- Both comma-separated values.
- Both ordered.
What is a Python set?
Unordered collection of unique objects
What is the typical use case of Python sets?
Often used to store a collection of distinct objects and perform membership tests (i.e., to check if an object is in the set).
How are Python sets defined?
Curly braces, {}
, and a comma-separated list of values.
What are the key properties of Python sets?
5 bullet points
- Unordered
- Unique
- Mutable
- Not indexed/do not support slicing
- Not hashable (cannot be used as keys in dictionaries or as elements in other sets)
What is the difference between Python split and join?
1 bullet point for each
- Split function is used to create a list from a string based on some delimiter (e.g., space).
- Join function concatenates a list of strings into a single string.
Syntax: Python split
Include definition of any class objects and/or parameters
string.split(separator, maxsplit)
- string: The string you want to split.
- separator: (optional): The delimiter used to split the string. If not specified, it defaults to whitespace.
- maxsplit: (optional): The maximum number of splits to perform. If not specified, it splits the string at all occurrences of the separator.
Syntax: Python join
Include definition of any class objects and/or parameters
separator.join(iterable)
- separator: The string that will be used to separate the elements of the iterable in the resulting string.
- iterable: An iterable object (e.g., a list, tuple, or string) whose elements will be joined together.
What are the logical operators in Python? What are they used for?
-
and
,or
,not
- Used to perform boolean operations on
bool
values.
Logical operators in Python: and
Returns True
if both operands are True
; otherwise, False
.
Logical operators in Python: or
Returns True
if either of the operands are True
; returns False
if both operands are False
.
Logical operators in Python: not
Returns True
if the operand is False
; returns False
if the operand is True
.
What are the top 6 functions used for Python strings?
len()
strip()
split()
replace()
upper()
lower()
Top 6 functions used for Python strings: len()
Returns the length of a string.
Top 6 functions used for Python strings: strip()
Removes leading and trailing whitespace from a string.
Top 6 functions used for Python strings: split()
Splits a string into a list of substrings based on a delimiter.
Top 6 functions used for Python strings: replace()
Replaces all occurrences of a specified string with another string.
Top 6 functions used for Python strings: upper()
Converts a string to uppercase.
Top 6 functions used for Python strings: lower()
Converts a string to lowercase.
What is the pass
keyword in Python? What is it used for?
pass
is a null
statement that does nothing. It is often used as a placeholder where a statement is required syntactically, but no action needs to be taken.
What are some common use cases of the pass
keyword in Python?
3 bullet points
- Empty functions or classes: When you define a function/class but haven’t implemented any logic yet. Use
pass
to avoid syntax errors. - Conditional statements: If you need an
if
statement but don’t want to take any action in theif
block, you can usepass
. - Loops: You can use
pass
in loops when you don’t want to perform any action in a specific iteration.
What is the use of the continue
keyword in Python?
continue
is used in a loop to skip over the current iteration and move on to the next one.
Definition: immutable data type in Python
Object whose state cannot be modified after it is created.
Definition: mutable data type in Python
Object whose state can be modified after it is created.
Examples of immutable data types in Python
- Numbers:
int
,float
,complex
bool
str
- Tuples
Examples of mutable data types in Python
- Lists
- Dictionaries
- Sets
Because numbers are immutable data types in Python, what happens when you change the value of a number variable?
Old value gets garbage-collected, freeing up the memory assigned to stroing the object
Python variables versus objects
- Variables are names that refer to or hold references to concrete objects.
- Objects are concrete pieces of information that live in specific memory positions on computer.
Can you use sort()
on tuples? Why or why not?
No. Tuples are immutable. You would have to create a new sorted tuple from the original tuple.
What are try except
blocks used for in Python?
Exception handling
try except
blocks in Python: what is the try
block?
Contains code that might cause an exception to be raised.
try except
blocks in Python: what is the except
block?
Contains code that is executed if an exception is raised during the execution of a try
block.
What are the similarites between Python functions and methods?
3 bullet points
- Both blocks of code that perform a specific task.
- Both can take input parameters and return a value.
- Both defined using the
def
keyword.
What are the key differences between Python functions and methods?
4 bullet points
- Functions are defined outside of classes; methods are functions that are associated with a specific object or class.
- Functions can be called on a standalone basis; methods are called using the dot notation on an object of a class.
- Functions perform general tasks; methods perform actions specific to the object they belong to.
- Parameters are optional for functions; for methods, the first parameter is usually
self
, which refers to the instance of the class.
How do functions help in code optimization?
4 high-level points
- Code reuse
- Improved readability
- Easier testing
- Improved performance
Functions + code optimization: Code reuse
Allow you to reuse code by encapsulating it in a single place and calling it multiple times from different parts of your program. Reduces redundancy, making code more concise and easier to maintain.
Functions + code optimization: Improved readability
Functions make your code more readable and easier to understand by dividing your code into logical blocks. This makes it easier to identify bugs and make changes.
Functions + code optimization: Easier testing
Functions allow you to test individual blocks of code separately, which can make it easier to find and fix bugs.
Functions + code optimization: Improved performance
Functions allow you to use optimized code libraries and/or allow the Python interpreter to optimize the code more effectively.
Why is NumPy often used for data science?
3 bullet points
- Fast and efficient operations on arrays and matrices of numerical data versus Python’s built-in data structures. This is because it uses optimized C and Fortran code behind the scenes.
- Large number of functions for performing mathematical and statistical operations on arrays and matrices.
- Integrates well with other scientific computing libraries in Python, such as SciPy and pandas.
Definition: list comprehension in Python
Shorter syntax when creating a new list based on the values of an existing list.
Syntax: Python list comprehension
new_list = [expression for item in iterable if condition]
Definition: dict comprehension in Python
Concise way of creating dictionaries in Python
Syntax: Python dict comprehension
{key: value for item in iterable}
Definition: global variable in Python
A variable that is defined outside of any function or class