Python I Flashcards

1
Q

attribute for getting the dimensions of a numpy array (e. g. (3, 4, 2))

A

arr.shape

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

attribute for getting the datatype of a numpy array

A

arr.dtype

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

attribute for getting the number of dimensions of a numpy array

A

arr.ndim

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

attribute for getting the number of all elements of a numpy array

A

arr.size

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

creating an array in numpy

A

np.array(my_list)

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

creating a range in numpy

A

np.arange(start, stop, step)

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

creating ranges of values in numpy

A

np.linspace(start, stop, numberOfElems, endpoint=True/False)

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

get multiple elements of a numpy array (index)

A

one_dim[[3, 4, 6, 15]] (gets elements at index 3, 4, 5, 6 and 15)

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

get the element of the 3rd row and 4th column of a numpy array

A

arr[2, 3] or arr[(2, 3)]

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

get the second row of a multidimensional numpy array

A

arr[1]

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

get the second column of a multidimensional numpy array

A

arr[: , 1]

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

extract the bottom right 2x2 corner of a 3x4 numpy array

A

arr[1:3, 2:4]

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

indexing of numpy arrays is also possible via masks

A

mask = np.array([[True, False, False, True],
[False, True, False, True],
[False, False, True, True]])
print(“two_dim[mask]”)
print(two_dim[mask])

(Returns flat (1D) array of elements where mask = True)

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

only extract even number out of a numpy array

A

mask = (arr % 2) == 0
print(arr[mask])

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

additional information numpy arrays

A

x2 = two_dim[:][2]

This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”

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

additional information numpy arrays

A

x2 = two_dim[:][2]

This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”

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

additional information numpy arrays

A

x2 = two_dim[:][2]

This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”

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

additional information numpy arrays

A

x2 = two_dim[:][2]

This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”

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

set all elements from index 2 to 4 to zero in a numpy array

A

one_dim[2:5] = [0, 0, 0]

or

one_dim[2:5] = 0 (broadcasting)

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

set all entries of a twodimensional numpy array to 0

A

two_dim[:] = 0

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

set the second to third element of the second row of a numpy array to 0

A

two_dim[1, 1:4] = 0

(also subarrays can be broadcasted, e. g.
two_dim[:, 1:3] = [7, 8])

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

When can numpy arrays be reshaped?

A

When the number of elements stays the same.

a = np.arange(24)
a = a.reshape(2, 3, 4)
a = a.reshape(4, 6)
a = a.reshape(6, -1)

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

add an empty dimension

A

a1 = a[:, None, :]
a2 = a[:, np.newaxis, :]

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

Other important numpy functions

A

print(np.append(a, np.array([1, 2, 3])))
print(np.concatenate([a, a, a]))
print(np.repeat(a, repeats=5))
print(np.tile(a, reps=5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
important: sum in numpy arrays
Sum (similar functions available for: mean, standard deviation, etc.) arr = np.arange(2 * 3 * 4).reshape((2, 3, 4)) * 3 print(arr.sum(axis=0)) # Sum along first axis/dimension (axis 0 will be "collapsed" -> shape: (3, 4)) print(arr.sum(axis=1)) # Sum along second axis/dimension (axis 1 will be "collapsed" -> shape: (2, 4)) print(arr.sum(axis=2)) # Sum along second axis/dimension (axis 2 will be "collapsed" -> shape: (2, 3)) print(arr.sum(axis=(1, 2))) # Sum along second + third axis (axis 1 + 2 will be "collapsed" -> shape: (2,)) print(arr.sum()) # "Global" sum along all axes/dimensions
25
Get the (flat!) index of the minimum of a numpy array and transform it into an array-shaped index
i_min_flat = arr.argmin() i_min_shape = np.unravel_index(i_min_flat, shape=arr.shape)
26
numpy functions with conditionals
print(np.where(arr % 2 == 0, arr, other)) # If condition is True, take from "arr", else take from "other" print((arr > 50).all()) # Check if all elements meet the condition print((arr > 50).any()) # Check if at least one element meets the condition
27
random values in numpy
rng = np.random.default_rng(seed=1234) arr = rng.random((4, 3)) arr = rng.integers(low=1, high=25, size=5) arr = rng.choice(np.arange(100), size=10, replace=False)
28
sort a numpy array
np.sort(arr) or arr.sort() - in-place
29
important info for numpy slicing and reshaping
Reshaping and slicing does not copy the original array, it only creates another "view" on the data.
30
regex - search for the first occurence of a pattern
re.search(pattern, text)
31
regex - find a pattern at the beginning of the string
re.match(pattern, text)
32
regex - groups
Groups can be used to only search for subpatterns within a search pattern (e. g. "H(all)o, how (are) you?") The found patterns can be accessed like this (0 is the whole pattern, 1 is the first group) match_object.group(1)
33
Getting additional information from regex group match objects
match_object.start(1) match_object.end(1) match_object.span(1)
34
regex - find all occurences of a pattern
re.findall(pattern, text) -> list (re.finditer does the same, but does it one item at a time and returns a match object)
35
regex - a set of characters to match
[...] e. g. "[cbr]at"
36
regex - specify ranges
[...] e. g. "[0-5]"
37
regex - negate patterns
^ e. g. "[^0-9]"
38
regex - predefined group for digits
\d
39
regex - predefined group for non-digits
\D
40
regex - predefined group for whitespace characters
\s
41
regex - predefined group for non-whitespace characters
\S
42
regex - predefined group for word characters
\w
43
regex - predefined group for non-word characters
\W
44
regex - match any character (except newline)
. e. g. ".m."
45
regex - search for alternative patterns
| e. g. "[bcr]at|dog"
46
regex - match any number of repetitions (also 0)
* e. g. "([bcr]at|dog)*"
47
regex - match at least one repetition
+ e. g. "([bcr]at|dog)+"
48
regex - be not- greedy (not search for as many repetitions as possible)
? (suffix)
49
regex - positive lookahead
only match if the following is XYZ "(?=XYZ)"
50
regex - negative lookahead
only match if it is not followed by XYZ "(?!XYZ)"
51
regex - positive lookbehind
only match if the preceeding is XYZ "(?<=XYZ)"
52
regex - negative lookbehind
only match if the preceeding is not XYZ "(?
53
call a program with the subprocess module
p = subprocess.Popen(["some_program", "argument1", "argument2"]) - will not wait for the program to finish p.wait(timeout=2) - will wait 2 seconds for the program to finish (except subprocess.TimeoutExpired)
54
get the output and errors of a program
p = subprocess.Popen(["ping", "www.jku.at"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) outs, errs = p.communicate(timeout=15)
55
simple way of calling a program
result = subprocess.run(["some_program", "argument1", "argument2"]) result.returncode result.stdout result.stderr
56
only execute code in the main process
if __name__ == "__main__"
57
use multiprocessing
from multiprocessing import Pool arguments = list(range(100)) without dynamic iterable: with Pool(4) as p: pool_returns = p.map(fcn, arguments) with dynamic iterable: with Pool(4) as p: pool_returns = p.imap(fcn, arguments) (or imap_unordered) for multiple arguments, starmap must be used
58
add a command line argument
parser = argparse.ArgumentParser() parser.add_argument("-n3", "--int_number3", type=int, required=True, help="required int")
59
add a command line argument which accepts mutiple inputs
parser.add_argument("--numbers", nargs=3, type=int) parser.add_argument("--items", nargs="+") parser.add_argument("--items", nargs="*")
60
access command line arguments
args = parser.parse_args() my_filename = args.filename
61
fall back to the default implementation of "==" if the if-statement returns False
def __eq__(self, other): if isinstance(other, Point): return self.x == other.x and self.y == other.y return NotImplemented
62
override output of the line "p1"
__repr__(self)
63
override output of the line "print(p1)"
__str__(self)
64
override indexing
__getitem__(self, key)
65
override "+"
__add__(self, other)
66
get the type of an object
type(dog1).__name__
67
check if a variable is either an integer of a floating point number
isinstance(num, (int, float))
68
syntax of a static method
@classmethod def some_class_method(cls): print(f"this is a class method: {cls}")
69
read file content
with open("my_module.py", "r") as f: print(f.read()) with open("my_module.py") as f: for line in f: print(line, end="") with open("my_module.py") as f: while chars := f.read(10): print("Chunk:") print(chars)
70
write file content
with open("some_file.txt", "w") as f: f.write("This overwrites an existing file\nor creates a new one with this text!\n") with open("some_file.txt", "a") as f: f.write("And here this line is appended.\n") with open("some_file.txt", "a") as f: print("And another line via print()!", file=f)
71
get current working directory
os.getcwd()
72
create a directory
os.makedirs("new_directory", exist_ok=True)
73
rename files or directories
os.rename("new_directory", "my_new_new_directory")
74
remove a directory
os.removedirs("my_new_new_directory")
75
remove a file
os.remove("some_file.txt")
76
list all contents of a directory
os.listdir()
77
os.path methods
path = os.path.join("some", "directory", "filename.py") head, tail = os.path.split(path) os.path.dirname(path) os.path.basename(path) root, ext = os.path.splitext(path) - extensions of a file os.path.exists(path)
78
check if a path is a file
os.path.isfile(path))
79
check if a path is a directory
os.path.isdir(".") - "." refers to the current working directory
80
os.path methods
path = os.path.join("some", "directory", "filename.py") head, tail = os.path.split(path) os.path.dirname(path) os.path.basename(path) root, ext = os.path.splitext(path) - extensions of a file os.path.exists(path) os.path.abspath(path)
81
list of all files and subdirectories in the current working directory
glob.glob("*")
82
search the current working directory for python files
glob.glob("*.py")
83
search for files and folders recursively
glob.glob("**", recursive=True)
84
search in a directory and all subdirectories for python files
glob.glob(os.path.join("some_folder", "**", "*.py"), recursive=True)
85
pickle
import dill as pickle always use with("path", "mode") as f pickle.dump(my_objects, f) data = pickle.load(f)
86
import modules
import sys import sys as system from os import path import os, sys from os import path, makedirs from my_module import add as my_add
87
create a list
empty_list = [] empty_list = list() a_list = [1, 2.3456, 3, "abc"]
88
navigating through nested lists
nested_list_element = nested_list[2][3][0]
89
check if an element is contained in a list
"abc" in a_list
90
sort lists
numeric_list.sort() - in-place sorted_list = sorted(numeric_list) - numeric_list is not changed
91
create a tuple
empty_tuple = () empty_tuple = tuple() single_element_tuple = (1,) a_tuple = 1, "2", 3 a_tuple = (1, "2", 3)
92
create a set
empty_set = set() my_set = {1, 2, 3, 3}
93
add/remove elements to a set
my_set.add(4) my_set.remove(2)
94
common operations on sets
Union new_set = my_set | {1, 4, 7} new_set = my_set.union({1, 4, 7}) Intersection new_set = my_set & {1, 4, 7} new_set = my_set.intersection({1, 4, 7}) Difference new_set = my_set - {1, 4, 7} new_set = my_set.difference({1, 4, 7})
95
create a dictionary
empty_dict = {} empty_dict = dict() some_key = "abc" dictionary = dict(some_key=3.24, other_key="twh") dictionary2 = {"stringkey": 55, some_key: "someitem", 23: 4}
96
get all keys or values of a dictionary
dictionary.keys() dictionary.values() dictionary.items() - tuples
97
additional dictionary stuff
Remove key and return its associated value removed = dictionary2.pop("stringkey") Get a value and use a default value if key does not exist value = dictionary.get("nonexistingkey", "defaultvalue")
98
dictionary iteration
for key in dictionary: (Equal to: dictionary.keys()) print(key) for value in dictionary.values(): print(value) for key_value in dictionary.items(): print(key_value) for key, value in dictionary.items(): print(key, value)
99
list comprehensions
old_list = [1, 2, 3, 4, 5] new_list = [str(i) for i in old_list] new_list = [str(i) for i in old_list if i < 3] new_list = [str(i) if i < 5 else "end" for i in old_list]
100
reverse a list
reversed_list = some_list[::-1]
101
unpacking
a, b, c, d = [1, 2, 3, 4] a, b, *rest = [1, 2, 3, 4] a, *rest, d = [1, 2, 3, 4]
102
for loop with index
for i, elem in enumerate(a_tuple): print(i, elem)
103
iterate through a string with index
for i, char in enumerate("hello"): print(f"loop iteration: {i}, element: '{char}'")
104
integer division
// floor value of a division
105
string functions
uppercase_string = conc_strings.upper() lowercase_string = "SILENCE!".lower() number_of_bla = repeated_string.count("bla") separated_elements = "my-string-elements".split("-") joined_elements = ";".join(separated_elements) replacement = "This is a placeholder".replace("placeholder", "string") check_for_substring = "string" in conc_strings locate_substring = replacement.find("is a")
106
formatted strings
formatted_string = "You can format floating point values as :width.precision " \ f"{long_number:10.5f}" another_formatted_string = f"{long_number:.{n_decimals}f}"
107
console
print("Hello, world!") my_input = input("Please enter something: ")