Python Flashcards

1
Q

Find length of list

A

length = len(list)

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

For loop for list

A

for element in list:

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

If, elif, else

A

if condition1:

elif condition2:

else:

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

If, elif, else

A

if condition1:

elif condition2:

else:

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

Print value

A

print(value)

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

Print floating point number with precision

A

print(f’{value:.6f}’)

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

Initiate variable

A

variable = 0

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

Increase a count

A

count += 1

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

Decrease a count

A

count -= 1

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

Initialise a variable to positive infinity

A

variable = float(‘inf’)

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

Initialise a variable to negative infinity

A

variable = -float(‘inf’)

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

Print multiple values in one line in Python

A

print(f’{value1} {value2}’)

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

Replace a substring in a string in python

A

new_string = old_string.replace(‘old_substring’, ‘new_substring’)

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

Check if substring in string

A

if ‘substring’ in string:

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

How to get a substring of first n numbers of characters

A

substring = string[:2]

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

Get substring of characters after first 2

A

substring = string[2:]

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

Convert string to int

A

number = int(string)

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

Convert number to string

A

str(number)

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

Replace first occurrence of a substring in Python

A

new_string = old_string.replace(‘old_substring’, ‘new_substring’, 1)

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

How to iterate from 1 to n inclusive in a for loop using range

A

for i in range(1, n+1)

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

Iterate from 0 to n exclusive in a for loop using range

A

for i in range(n):

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

Iterate from starting value to an ending value with a specific step in a for loop

A

for i in range(start, stop, end):

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

How to check if a number is divisible by another number

A

number % division == 0

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

Iterate backwards from n to 1 inclusive using range

A

for i in range(n, 0, -1):

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

Sort list in place

A

list.sort()

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

Find floor of a number

A

import math

math.floor(number)

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

Find ceiling of a number

A

import math

math.ceil(number)

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

Create an empty dictionary

A

dictionary = {}

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

Check if key exists in dictionary

A

if key in dictionary:

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

Delete key value pair

A

del dictionary[key]

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

Add key value pair

A

dictionary[key] = value

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

Get list of all keys in a dictionary

A

keys = list(dictionary.keys())

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

Calculate the trace of diagonal elements

A

import numpy as np

np.trace(matrix)

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

Transpose a matrix and find trace

A

import numpy as np

np.trace(matrix.T)

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

Iterate through the rows of a square matrix

A

for i in range(len(matrix)):

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

Access elements on the primary diagonal of a square matrix

A

matrix[i][i]

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

Access element on the secondary diagonal of a square matrix

A

matrix[len(matrix) - 1 - i][i]

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

Find absolute value of a number

A

abs(number)

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

Initialise an array with zeros in Python

A

array = [0] * size

40
Q

Initialise an array with a specific value using list comprehension in Python

A

array = [value for x in range(size)]

41
Q

Initialise a list with a given size and fill with None values

A

array = [None] * size

42
Q

What is counting sort

A

Counting sort is a non-comprehension-based sorting algorithm that sorts integers by counting the number of occurrences of each unique element. It then calculates the positions of each element in the sorted array

43
Q

When to use Counting Sort

A

Counting sort is efficient when the range of input values (k) is not significantly larger than the number of elements (n) to be sorted. It is ideal for sorting integers within a limited range

44
Q

Time complexity of counting sort

A

O(n+k) where n is the number of elements in the input array and k is the range of the input

45
Q

How to find the minimum value in a list in Python

A

min_value = min(list)

46
Q

Find maximum value in a list in Python

A

max_value = max(list)

47
Q

Calculate the range of elements in a list

A

range_of_elements = max_value - min_value

48
Q

Find maximum value of many values

A

max_value = max(value1, value2, value3, value4)

49
Q

Swap two elements in a list

A

list[index1], list[index2] = list[index2], list[index1]

50
Q

Unpack a list into separate arguments

A

function(list)
print(
list)

51
Q

Find index of a character in a string in Python

A

index = string.find(character)

52
Q

Get character at index Python

A

character = string[index]

53
Q

Concatenate string Python

A

result = string1 + string2

54
Q

Check if character in string

A

if character in string

55
Q

Create a list with list comprehension

A

new_list = [expression for item in iterable]

56
Q

Iterate from both ends of a string towards the center

A

left, right = 0, len(string) - 1
while left < right:
#proces
left += 1
right -= 1

57
Q

Check if string is a palindrome

A

string == string[::-1]

58
Q

Slice a string to remove character at index

A

new_string = string[:index] + string[index+1:]

59
Q

Sort each row in a 2D list in Python

A

sorted_grid = [sorted(row) for row in grid]

60
Q

How to sort in place

A

string.sort()

61
Q

Sort and return new string

A

sorted(string)

62
Q

Check if stack (list) is empty before accessing the top element

A

if stack and stack[-1] == element:

63
Q

Remove last element in stack

A

stack.pop()

64
Q

Remove element with index

A

stack.pop(index)

65
Q

Add to end of a list

A

list.append(value)

66
Q

Count occurrences of elements in a list using a dictionary in Python

A

string_counts = {}
for s in list_of_strings:
if s in string_counts:
string_counts[s] += 1
else:
string_counts[s] = 1

67
Q

Retrieve a value from a dictionary with a default if the key is not present. So we can have a value if there is no key

A

Example:

value = dictionary.get(key, default_value)

count = string_counts.get(query, 0)

68
Q

Make string lower case

A

str.lower()

69
Q

Make python string upper case

A

str.upper

70
Q

Convert string to a set. This is a set of unique characters

A

set(string)

71
Q

Check if a set is a subset of another set

A

set1.issubset(set2)

Where set1 is the constraint set like alphabet, so you would see if set2 contains the whole alphabet

72
Q

Convert first letter to capital in string

A

str.capitalise()

73
Q

Center a string after padding with a specified character

A

str.center(width, [fillchar])

E.g., str.center(24, ‘*’)

74
Q

Return number of occurrences of a substring in a given string

A

str.count(substring, start, end)

str.count(‘p’)

75
Q

Return true if a string ends with a specified suffix

A

str.endswith(‘PM’)

76
Q

Return index of first occurrence of substring if found, if not found returns -1

A

str.find(‘fun’)

77
Q

Return index of first occurrence of substring if found, if not found returns -1

A

str.find(‘fun’)

78
Q

Return true if all characters in string are alphanumeric, if not false

A

str.isalnum()

79
Q

Find if string is all alphabet characters, either upper or lower case

A

str.isalpha()

80
Q

Return true if all characters in a string are digits

A

str.isdigit()

81
Q

Return true if all characters are lower case

A

str.islower()

82
Q

Return true is all characters are upper case

A

str.isupper()

83
Q

Join all elements of an iterable list separated by a given separator

A

‘ ‘.join(array)

84
Q

Convert characters in a string to the opposite case

A

str.swapcase()

85
Q

Replace a substring with another string, this is for every occurrence

A

str.replace(‘ba’, ‘ro’)

86
Q

Split a string to an array with a chose separator

A

str.split()

str.split(separator)

87
Q

Split a string to an array with a chose separator

A

str.split()

str.split(separator)

88
Q

Sort list in ascending order

A

list.sort()

89
Q

Sort list in descending order

A

list.sort(reverse=True)

90
Q

Calculate the sum of a subarray

A

subarray_sum = sum(list[start:end])

91
Q

Calculate the sum of a subarray

A

subarray_sum = sum(list[start:end])

92
Q

Iterate over all possible subarrays of a given length

A

access the subarray with list[i:i+subarray_length]

for i in range(len(list) - subarray_length + 1):

93
Q

Remove value from array

A

stack.remove(num)

94
Q

Count occurrences of elements in a list using a dictionary

A

sock_counts = {}
for sock in list_of_socks:
if sock in sock_counts:
sock_counts[sock] += 1
else:
sock_counts[sock] = 1

95
Q

Iterate through values in dictionary

A

for value in dictionary.value():

96
Q

Calculate the number of pairs from a count of items

A

pairs = count // 2

This is integer division, essentially calculating the number of times 2 goes completely into pairs