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.