Python Syntax Quick Reference Flashcards

(23 cards)

1
Q

What is the purpose of collections in Python?

A

To provide specialized container data types

Common imports include defaultdict, Counter, and deque.

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

What does the function heapq.heappush do?

A

Adds an element to the heap

It maintains the heap property.

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

How do you convert a string to a list of characters in Python?

A

chars = list(s)

This operation splits the string into its individual characters.

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

What is the syntax to join a list of characters back into a string?

A

s = ‘‘.join(chars)

This combines the characters into a single string.

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

What method would you use to split a string into words?

A

s.split() or s.split(‘,’)

This separates the string at whitespace or commas.

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

How do you strip whitespace from a string?

A

s.strip()

This removes leading and trailing whitespace.

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

What is the syntax to copy a list in Python?

A

new_list = old_list[:] or new_list = old_list.copy()

Both methods create a shallow copy of the list.

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

How do you slice a list to exclude the last element?

A

arr[:-1]

This returns all elements except the last one.

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

What is the syntax to reverse a list?

A

arr[::-1]

This creates a new list that is the reverse of the original.

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

How do you sort a list in place?

A

arr.sort()

This modifies the original list to be in sorted order.

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

What is the function of sorted() in Python?

A

Returns a new sorted list

It does not modify the original list.

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

How do you sort a list with a custom key?

A

sorted(arr, key=lambda x: x[0], reverse=True)

This sorts the list based on the first element of each item.

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

What does the method d.get(key, default_value) do?

A

Returns the value for key or default_value if key is not found

This prevents KeyError exceptions.

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

What does d.setdefault(key, default_value) do?

A

Sets default_value for key if key is not already in the dictionary

This is useful for initializing dictionary entries.

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

How do you iterate over a dictionary in Python?

A

for key, value in d.items():

This allows access to both keys and values.

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

What does enumerate(arr) return?

A

(index, value) pairs

This is useful for getting the index along with the value.

17
Q

What does any(condition for x in arr) return?

A

True if any element matches the condition

This checks for at least one True condition in the iterable.

18
Q

What does all(condition for x in arr) return?

A

True if all elements match the condition

This checks if every element satisfies the condition.

19
Q

What is the syntax for integer division in Python?

A

a // b

This rounds down the result to the nearest whole number.

20
Q

What does float(‘inf’) represent?

A

Positive infinity

This is used to represent an unbounded upper limit.

21
Q

What does float(‘-inf’) represent?

A

Negative infinity

This is used to represent an unbounded lower limit.

22
Q

What does range(n) produce?

A

0 to n-1

This generates a sequence of numbers starting from 0 up to n-1.

23
Q

What is the syntax for creating a custom step in a range?

A

range(start, end, step)

This allows you to specify the increment between numbers.