Python Flashcards

1
Q

What is snake case in python?

A

Using underscores to form a long variable name

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

What is the type of the result of a division operation in python?

A

floating point

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

How do you get integer value out out of a division in python?

A

By using the ‘//’ operator

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

Does // operator do a round-off?

A

No

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

How do you distinguish between a variable definition and further use in python?

A

You can’t, both have same syntax

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

f-strings are available from which python version?

A

3.6 onwards.

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

What are f-strings?

A

A convenient way to format different types into string

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

What is another way (than f-strings) to format strings without explicitly converting them?

A

By having placeholders in the string with “{}” and calling the format method of the string with requisite parameters

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

What is the constraint on the number of parameters for the format method of the string?

A

It can be more, but cannot be less

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

In what ways “format” can take parameters for a string?

A

format can take parameters as positional as well as keyword based.

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

How does “format” method take keyword based arguments?

A

keyword based arguments have to be given in the last after positional arguments. They cannot be mixed.

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

What is necessary for passing keyword based arguments to “format” method of string?

A

The string should contain the keyword in { } in it

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

Is string.format(name=name) valid? Both identifiers

A

Yes, the first one will be looked for in the string, while the second one is treated as a proper variable

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

How does f-string and format compare?

A
  • f-string takes actual parameters, while format takes formal parameters (which is to be filled in by passing format with actual parameters)
  • f-string can take expressions that evaluate to a value, while format cannot
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which function in python reads input from keyboard and returns a string?

A

input(“message”)

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

What are boolean keywords in python?

A

True and False

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

What does print take?

A

An expression that can be evaluated

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

What are python’s boolean operators?

A

and, not, or

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

What does and/or return?

A
  • and returns first value if it is false, otherwise it returns the second value
  • or returns the first value it is true, otherwise it returns the second value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does not return?

A

not always returns True or False

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

What does list.remove() take?

A

The value of the element which needs to be removed from the list

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

Is parens mandatory for writing tuples?

A

No

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

How to minimally express a tuple of only one item?

A

a,

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

How do you add a new element to a tuple variable?

A

By adding “+” another tuple of single element to the variable: v = v + (e,)

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

How do we represent set1 - set2 in python?

A

set1.difference(set2)

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

How do we represent symmetric difference between two sets (set1 U set2) - (set1 AND set2)

A

set1.symmetric_difference(set2)

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

What type is “{}”

A

dict, not set

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

How do we represent set intersection in python?

A

set1.intersection(set2)

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

How do we represent set union in python?

A

set1.union(set2)

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

How do we represent an empty set in python?

A

set()

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

Does dictionary keep order of elements inserted?

A

Yes, in python 3.7

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

How to make a dict from values?

A

Have a list of 2-element tuples and feed it to dict()

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

A list of grades, a tuple of grades, a set of grades - which is not a correct choice

A

set of grades

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

A list of grades, a tuple of grades - which is a better option?

A

if grades are not going to be added, a tuple is better, otherwise a list is better

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

sum(), len() - can be used on which of the following data structures: set, list, tuple

A

all of them

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

How come we can pass values and expressions to if or while in python?

A

Python implicitly passes all those expressions through bool to get True or False

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

What is the else/if keyword in python?

A

elif

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

What is the difference between while loop and for loop in python?

A

While loop runs an undefined number of times - based on an external event, and for loop runs for a defined number of times

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

What is the difference between:
if a in b:
for a in b:

A

In the first one, “a” is a known identifier or a constant, while in the second one, “a” is a new identifier

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

What is the act of taking a collection into more than one variable is called?

A

destructuring

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

What does for d in dict give?

A

A list of all the keys

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

How do you iterate over values of a dict?

A

dict.values()

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

How do you destructure key, value from a dict?

A

dict.items()

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

what does while-else/for-else statement do in python?

A

If for doesn’t encounter a break, then else clause will be executed, otherwise else clause will not be executed

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

What is the difference between aList and aList[:]

A

The second one gives a new copy of aList

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

what does aList[0] and aList[-1] represent?

A

The first and the last elements of the list

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

what does aList[-x] represent?

A

xth element in the list starting from the last element with index as 1 towards left side

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

how do you mathematically repreent aList[x:y]?

A

[x, y)

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

What is the direction of slicing a list?

A

Always from left to right

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

What happens if a list is sliced with indices pointing to elements from right to left?

A

An empty list is returned

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

In aList[-x:-y], what is the constraint to get at least one element?

A

x > y

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

How do you differently explain list comprehension?

A

Expression followed by the for statement with an optional if statement

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

What is a readable way to write a list comprehension?

A

expression, for statement, if statement - each in one line

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

Comprehension is applicable for which of the following collections? list, set, dict

A

All of them

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

When you create a collection, think about _____

A

comprehension

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

How do you create a list of tuples with lists to feed to dict()?

A

With a zip function

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

Which is more simpler? a dict made out of comprehension or a dict made out of a zip?

A

dict made out of a zip

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

When does a dict made out of comprehension make sense over a dict made out of zip?

A

When we want to conditionally add the list elements into the zip

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

Does zip takes only two lists?

A

No, it can take more than two

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

What happens if the lists given to zip are of different lengths?

A

zip will make a list of tuples of the size of the smallest list

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

can the argument to a function be nameless?

A

Yes

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

How is a variable resolved i.e. searched for resolution?

A

First in local scope, then in global scope

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

What is the type of the value returned from a function which has no explicit return statement?

A

None

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

What are the two types of arguments in python to a function?

A

positional and keyword arguments

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

What is the constraint for passing positional and keyword arguments in python?

A

positional arguments should precede keyword arguments

66
Q

What is the constraint for defining a function with default and non-default parameters?

A

non-default parameters must precede default parameters

67
Q

What is the argument given to print to change what is printed between the arguments?

A

sep=xyz

68
Q

When are the default parameters for a function evaluated and stored?

A

At the time of function definition, not function invocation

69
Q

What are the consequences of having non-constant expressions as values for default parameters?

A

Any change in the variables will not change the value of default parameters and it becomes less readable

70
Q

What is the difference between a normal function and a lambda function?

A

Normal function is assigned to a variable with the function definition itself, where as lambda function has to be explicitly assigned to a variable through an assignment statement

71
Q

When is a function vs lambda destroyed in python?

A

When the function or lambda reference goes out of scope

72
Q

Can a lambda definition span more than one line?

A

No

73
Q

What happens when a lambda definition is not assigned to anything?

A

it is destroyed immediately

74
Q

How do you check for the presence of a key in dict?

A

with the “in” operator

75
Q

Which string method is used to validate if the string is a number?

A

isdigit(), isdecimal(), isnumeric()

76
Q

What is the difference between two string methods: isdigit() and isnumeric()

A

The latter works with unicode characters

77
Q

What is the no-op statement in python?

A

pass

78
Q

What happens if a function is defined two times?

A

The latest definition overrides the old definition

79
Q

What is a clean and extensible way to implement a menu of options to be selected by the user?

A

A dictionary with key as the supported options and values as the function handlers

80
Q

How does lambda functions help write generic code

A

It helps by the caller pass the paramater(object) as is to the lambda function without interpreting the type of the parameter

81
Q

In the class definitions, is the “self” keyword mandatory?

A

No, the first parameter refers to the object and it can be named anything.

82
Q

What is the function that gets called first when a class is instantiated?

A

__init__

83
Q

__init__ is also called as?

A

dunder init function

84
Q

What are the different valid ways of calling an object method?

A
  1. object.method()

2. class.method(object)

85
Q

How does the list of attributes in a class defined?

A

As and when they are assigned.

86
Q

Can a class attributes grow dynamically after object creation?

A

Yes

87
Q

List of arguments to python script is stored in?

A

sys.argv list

88
Q

Current OS is stored in?

A

sys.platform

89
Q

Name the fully qualified function to write to stdout

A

sys.stdout.write(str)

90
Q

Python version is stored in?

A

sys.version

91
Q

PATH contents are available in?

A

sys.path

92
Q

Function to check if a file exists in file system?

A

os.path.isfile()

93
Q

Function to check access permissions of a file?

A

os.access(file, os.)

94
Q

Function to list the contents of a directory?

A

os.listdir(dir)

95
Q

Function to get current working directory?

A

os.getcwd()

96
Q

Function to execute a shell command?

A

os.system()

97
Q

Function to walk the current directory?

A

os.walk()

98
Q

What does os.walk() return?

A

it returns the root directory (which is being walked), a list of directories and files in the root

99
Q

Function to check OS?

A

platform.system()

100
Q

What is the difference between sys.platform and platform.system() ?

A

sys.platform is an variable attribute, platform.system is a method attribute

101
Q

What are different ways to run a command?

A

os.system(), subprocess.run(), subprocess.call(), os.spawn(), os.popen() etc.

102
Q

When executing commands like “clear” through subprocess, what needs to be indicated?

A

shell=True

103
Q

What does subprocess.run() return?

A

An instance of CompletedProcess class

104
Q

What does subprocess.check_call() function return?

A

Nothing if the call succeeds, otherwise raises a CalledProcessError exception

105
Q

What does subprocess.check_output() function return?

A

Returns the output as a byte string if it exits normally otherwise raises CalledProcessError exception

106
Q

What does os.system() return?

A

the exit code of the process

107
Q

What does stdout=subprocess.PIPE mean?

A

Instead of pointing to standard output, stdout is now pointing to a pipe (which will be instantiated and returned)

108
Q

What does stdout=subprocess.PIPE, stderr=subprocess.STDOUT mean?

A

output is given a new pipe and err is redirected to stdout (to the new pipe here)

109
Q

Why subprocess.wait() needs to be used?

A

to let the child finish so as to collect output

110
Q

What does subprocess.communicate() return?

A

A tuple of (stdout, stderr)

111
Q

What is the general sequence of steps to spawn a process and collect its output with subprocess?

A
sp=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ret=sp.wait()
out, err = sp.communicate()
#check ret, out, err
112
Q

How to convert bytes to string?

A

str(, “UTF-8”) #encoding scheme

113
Q

How can “ps aux | grep xyz” be achieved in python with subprocess methods?

A

ps=subprocess.Popen([“ps”, “aux”], stdout=subprocess.PIPE)
grep=subprocess.check_output([“grep”, “xyz”], stdin=ps.output)
ret = ps.wait() #check return code

114
Q

Once a Popen object is created with PIPE, how can that be used elsewhere or assigned elsewhere?

A

we can refer to stdout, stdin of the Popen object

115
Q

When a command and its parameters is given as a string to subprocess.Popen(), what should shell=?

A

True, as it is passed as is to the shell to execute, without which it will fail

116
Q

With shell=True, what happens if we give command as a list of command, parameters?

A

The parameters are ignored and only the command is executed

117
Q

What is given to subprocess.Popen to get output and error as text (string) instead of byte object?

A

universal_newlines=True

118
Q

What are the 4 steps of argument parsing?

A

import argparse; create a parser; add arguments; parse arguments

119
Q

How is an argument parser created?

A

argparse.ArgumentParser

120
Q

What is the function in argparse to add an argument?

A

add_argument()

121
Q

How do we convert arguments passed to add_argument to different types?

A

with type=xyz

122
Q

How do we add description to an argument in add_argument?

A

with help=abc

123
Q

How do we validate that given input is within a specified list when adding an argument with add_argument?

A

with choices=[a, b, c]

124
Q

which kwarg specifies that an argument is mandatory or not in add_argument

A

required=True or required=False

125
Q

How are positional arguments in add_argument referred to in the code?

A

when we give dest=”xyz”, it is referred to with attribute xyz on the argumenr parser object

126
Q

How are optional arguments given a default value in add_argument of argparse?

A

with default=abc

127
Q

How are optional arguments added with

add_argument?

A

with a “-“ or with a “–”

128
Q

How are optional arguments referred to in the code?

A

the name given with “-“ or “–” or with a dest=abc

129
Q

How is a boolean flag specfied in add_argument of argparse?

A

with action=”store_true”

130
Q

When short and long names are given in add_argument of argparse, which one prevails?

A

long one

131
Q

Which function in argparse is used to create a set of mutually exclusive arguments?

A

add_mutualy_exclusive_group, group.add_argument()

132
Q

With argparse, how do we specify that an argument is a file that has to be read or written?

A

with type=argparse.FileType(‘r’) or type=argparse.FileType(‘w’, encoding=”UTF-8”)

133
Q

How do we import scapy into python code?

A

import scapy.all as scapy and call scapy.xxx()

134
Q

Which operator is used to stack different layers in a scapy constructed packet?

A

’/’ operator

135
Q

What denotes the latest result in python?

A

“_”

136
Q

Which function will omit showing default in scapy constructed packets?

A

hide_defaults()

137
Q

which scapy function is used to read pcap files?

A

rdpcap()

138
Q

Which scapy function is used to dump the packet in hex?

A

hexdump()

139
Q

Which scapy function is used to format the packet with a format specifier?

A

pkt.sprintf()

140
Q

Which scapy function is used to dump the packet in the command format (which can be executed to generate the same packet)?

A

pkt.command()

141
Q

Which scapy function is used to display the packet after proper processing (dns resolution, cheksum calculation etc.)

A

pkt.show2()

142
Q

In scapy, how to set a field to a range of values?

A

ttl=[1,2,(5, 9)]

143
Q

In scapy, when we give a range (a,b), is it inclusive or exclusive?

A

Inclusive

144
Q

In scapy, which function sends packets at layer 3?

A

send()

145
Q

In scapy, which function sends packets at layer 2?

A

sendp()

146
Q

In scapy, with send/sendp, which kwarg is used to get back the list of packets sent?

A

return_packets=True

147
Q

In scapy, which function is used to randomize the packet fields?

A

fuzz()

148
Q

In scapy, how is fuzz() used to randomize fields?

A

fuzz(IP()/UDP()) etc.

149
Q

In scapy, what all fuzz() can randomize and what it cannot?

A

Fields overridden by upper layers and fields that are not defaults - are not fuzzed

150
Q

In scapy, to randomize (fuzz) IP addresses which function should be used?

A

RandIP()

151
Q

In scapy, what does the send and receive family of functions return?

A

a couple of two lists - the first list is a couple of (sent, received packets), the second list is unanswered packets

152
Q

In scapy, in the send/receive family of functions what is the advantage of receiving unanswered packets in return?

A

We can call send/receive again with the unanswered list as is

153
Q

In scapy, in send/receive family of functions, what do the kwargs inter, retry and timeout mean?

A

inter - interval (in secs) between transmitting two packets
retry - if retry is x, then unanswered list is retransmitted “x” number of times. If the retry is -x, then the unanswered list is transmitted until the unanswered list is the same for the last x tries
timeout - when to start treating sent packets as unanswered

154
Q

In scapy, which function lists all the available packet types?

A

ls()

155
Q

In scapy, how do we see all the supported fields for a given packet type?

A

ls(pkt_type)

156
Q

In scapy, which function lists all the commands available?

A

lsc()

157
Q

In scapy, how do we see the help for a command?

A

help(cmd)

158
Q

In scapy, which function is used to write packets to a pcap file?

A

wrpcap()

159
Q

In scapy, which function is used to read packets from a pcap file?

A

rdpcap()

160
Q

In scapy, when we create a ICMP() packet, which type of the packet is created by default?

A

echo-request

161
Q

In scapy, how can we change the configuration on which scapy operates?

A

conf.xyz = abc;