Python Flashcards

Study about Python programming language (79 cards)

1
Q

What is the default return value of a function which doesn’t have an explicit return statement

A

None

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

What are the “keyword arguments” in functions

A

Keyword arguments allow to specify the argument names making the code more readable.

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

What does “**kwargs” represent

A

Dictionary containing the arbitrary number of keyword arguments passed to a function.

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

How can one modify the value of a global variable in a function

A

By declaring the variable of interest as global at the beginning of the function definition.

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

What is ‘a list’ in Python

A

Data structure which can hold multiple values called items.

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

There is a numbers list defined as: numbers[1, 5, -10, 500, 766]. What value is returned by following statement: numbers[-2]

A

500

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

What is a slice

A

A way to extract a part of a sequence like - sequence[start:stop:step]

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

There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[:4]

A

1, 2, 3, 4

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

There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[2:4]

A

3, 4

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

There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[1:]

A

2, 3, 4, 5

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

There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[:4:2]

A

1, 3

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

How to determine the size of a list?

A

By calling len() function with the list as argument.

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

What does list() function do

A

It creates a list from an iterable object or either an empty list.

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

What does following list() invokation do:

tuple_data = (1, 2, 3, 4)
newList = list(tuple_data)

A

It creates a list with same elements as in tuple: [1, 2, 3, 4]

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

What does following list() invokation do:

dict_data = { ‘a’: 1, ‘b’:2, ‘c’:3}
newList = list(dict_data)

A

It creates a list with ‘key’ from a dictionary:
[‘a’, ‘b’, ‘c’]

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

What does index method do on a list

A

It returns an index of the first occurrence of the element provided as argument if one is found. Otherwise it throws a “ValueError” exception.

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

What does append method do on a list

A

It adds an element to end of the list.

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

What does insert method do on a list

A

It allows insertion of an element anywhere in the list

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

What does remove method do on a list

A

It removes the first occurrence of given element if such element exists.

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

Are lists mutable or immutable?

A

Mutable

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

Are strings mutable or immutable

A

Immutable

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

What is the line continuation character which can be used to stretch the python statement over several lines

A

\

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

What is a dictionary?

A

A key-value like data structure.

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

What does dict() function do?

A

It creates a dictionary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does following dict function do: dict("color": "green", "name": "annie")
It creates a dictionary with two elements.
26
Is dictionary mutable or immutable data type?
Mutable
27
What does setdefault method do on a dictionary?
It sets the default value for the key if the key is not defined explicitly.
28
What does this statement do: var = {}
It creates an empty dictionary.
29
What does pass keyword do?
Pass keyword is used as a placeholder in places where we might want to add implementation later but not exactly now, like in functions.
30
What does type() function do ?
It tells as the type of the argument that is passed in.
31
What is a "raw" string in Python?
A string prefixed with r which will turn any escape characters into printable ones.
32
What is used for multiline strings?
Multiline strings are enclosed with triple quotes, like """.
33
What does following code do: ‘,'.join([‘cat', ‘dog']) ?
It creates a new string in which all words passed in the iterable object are separated with comma: "cat,dog"
34
What does rjust method do on strings?
It padds the string with spaces (or given characters) to the left so the string is right aligned.
35
What does center method do on strings?
It padds the string with spaces (or given characters) from both sides so the string is in center.
36
What does ljust method do on strings?
It padds the string with spaces (or given characters) to the right so the string is left aligned.
37
What does strip method do on strings?
It removes any whitespace (or given characters) characters from left or the right side of the string.
38
Are tuples mutable or immutable?
Immutable.
39
How do you access cmd arguments from your program?
They are part of sys.argv.
40
What is the module which contains regular expression functions?
re
41
What function can be used to create a regex object?
re.compile()
42
What function can be used to find a first occurrence of the regex pattern?
search()
43
What function can be used to find all occurrences of the regex pattern?
findall()
44
What function can be used to the get grouped regex patterns found in the text?
group()
45
How are groups created in regex patterns?
With parentheses.
46
How to match the literal parentheses within the regex pattern?
By preceding it with '\'.
47
What is the purpose of the pipe '|' in group matching?
It can be used as an or operator to match several different options.
48
What is the meaning of '?' in regex pattern matching?
It states that the pattern matches zero or one time.
49
What is the meaning of '*' in regex pattern matching?
It states that the pattern matches zero or more times.
50
What is the meaning of '+' in regex pattern matching?
It states that the pattern matches one or more times.
51
What is the meaning of curly braces '{}' in regex pattern matching?
They state that the pattern can match specific number of times.
52
What is the meaning of curly braces with two values in regex pattern matching?
They state that the pattern can match minimum and maximum number of times.
53
What does leaving out one of the values from the curly braces mean in regex pattern matching?
It means that there is either no minimum or maximum.
54
What is greedy regex pattern matching?
Greedy matching matches the longest string possible.
55
What is non-greedy regex pattern matching?
Non-greedy matching matches the shortest string possible.
56
What character can be put after curly braces to do a non-greedy regex pattern matching?
Question mark.
57
What does findall() return in regex pattern matching if the pattern contains 0 or 1 group?
A list of strings.
58
What does findall() return in regex pattern matching if the pattern contains 2 or more groups?
A list of tuple of a strings.
59
What is \d in regex pattern matching?
Shorthand for digit characters.
60
What is \w in regex pattern matching?
Shorthand for word characters.
61
What is \s in regex pattern matching?
Shorthand for whitespace characters (space, tab, newline).
62
What is \D in regex pattern matching?
Shorthand for non-digit characters.
63
What is \W in regex pattern matching?
Shorthand for non-word characters.
64
What is \S in regex pattern matching?
Shorthand for non-whitespace characters.
65
How could you make your own characters classes in regex pattern matching?
With square brackets: e.g [aeiou] will match only lowercase vowels.
66
What does caret do in own character classes in regex pattern matching, e.g. [^aeiou]?
It matches all the characters except the ones in the brackets.
67
What does ^ (caret) represent in regex pattern matching (not for own character classes context) ?
It states that a pattern should be at the beginning of the examined string.
68
What does $ represent in regex pattern matching?
It states that a pattern should be at the end of the examined string.
69
What does dot '.' character match in regex pattern?
Any character except newlines.
70
How to make the regex pattern matching case-insenstitive?
By passing re.I to re.compile method.
71
What method can be invoked on regex object to replace matches with some other text?
sub(replacement, originalText)
72
What does re.DOTALL accomplish when working with regexes?
It changes the behavior of the '.' pattern to include newlines when matching text.
73
Which module contains useful folders and files functions?
os module
74
Which module contains useful functions for moving, copying files?
shutil
75
What is provided by shelve module?
Shelve module provides a way to quickly save and restore data from and to python programs by using binary files.
76
What does os.unlink() do ?
It deletes a file.
77
What does os.rmdir() do ?
It removes a directory but only if empty.
78
What does shutil.rmtree() do?
It removes a directory and all its contents.
79
How does one throw/raise an exception?
raise Exception("message")