General python Flashcards

1
Q

How do you get a new blank row into code?

A

\n , print()

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

What is the escape character?

A

\

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

What does \ do?

A

Announces that the string should be paused and that the character after means something

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

How do you print the character \

A

print(“\”)

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

What happens when you pass muliple arguments to a print function

A

1) It combines them all on one line

2) It puts a small between the outputted arguments

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

What is a positional argment?

A

The location determines what it does

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

How do you use keyword arguments?

A

1) keyword = value

2) after positional arguments

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

What does the default end keywork argument do in print

A

\n

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

How do you alter the default behaviour of print to insert a space between arguments?

A

sep =

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

What is a literal?

A

Data you can determine the value of without any additional knowledge e.g. 123 vs c

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

What are the 2 ways to show quotation marks in Python

A

" or use ‘’ instead of “”

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

How do you encode ‘to the power of’

A

**

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

What does print(6/2) give you?

A

A float

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

What does // do?

A

Integer division

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

What happens when the result of // is not an integer?

A

Rounds to the lower number (ie. floor division)

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

What is the result of print( 6 // - 4)?

A

-2

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

What type does the % operation result in?

A

Float

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

what is a unary and a binary operatory

A

Binary expect 2 arguments, unary expects 1

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

Is python predominantly right side or left side binding?

A

Left side

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

What is the exception to left side binding

A

Exponentiation

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

What is the order of priority of operators?

A

1) + - (unary)
2) **
3) * / %
4) + - (binary)
5) , >=
6) ==, !=

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

What does a variable contain?

A

A name and value

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

How do you evaluate the square root of something?

A

** 0.5

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

What does input() store values as?

A

A string

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

What do you need to do to get input usable with operators?

A

float()

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

What does isna() return?

A

Boolean series

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

How do you count how many missing values?

A

df.isna().sum()

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

How do you get a dataframe into a list?

A

df.values.flatten()

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

How do you get summary statistics by row?

A

1) data = pd.DataFrame()
2) var = df.mean(axis = 1)
3) data[‘var’] = var

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

What is the keyboard shortcut to see all methods?

A

tab after the .

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

What is the keyboard shortcut to see the arguments available?

A

shift and tab

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

What is the difference between a method and an attribute?

A

Attribute describes and has no parentheses, methods have parentheses

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

Describe ==

A

Binary, left binded

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

In python what are the comparison operators?

A

==, !=, >, =, <=

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

What has higher priority: == or >=

A

> =

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

What is the difference between if and elif?

A

If will always be evaluated, elif and else stop once something is found to be true (and assumes the previous thing was false)

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

What does range(100) do?

A

0 to 99

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

What does range(2,8) do?

A

2 to 7

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

What does range(2,8,3)

A

2, 5

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

What does break do?

A

Exits the loop immediately

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

What does continue do?

A

Behaves as if the programme has reached the end of the body of the loop and starts the next turn

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

What is the difference between break and continue?

A

Break exits the loop, continue goes to start the next loop immediately

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

What are the logical operators?

A

and
or
not

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

What are the logical operators vs the comparison operators?

A

Comparison >= > !=

Logical and or not

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

What has higher priority; comparison operators or logical?

A

Comparison

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

Is and binary or unary?

A

binary

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

What has higher priority; and or or?

A

and

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

What does not do?

A

It turns false into true and true into false?

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

What priority is not equivalent to?

A

unary + and -

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

What are the bitwise operators?

A

& | ~ ^

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

Which are the logical operators and which are the bitwise operators?

A

Logical: and, or, not
Bitwise: & | ~ ^

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

What is the difference between | and ~ ?

A

needs at least 1 1 - ~ needs exactly 1 1

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

Do bitwise operators need integers or flats?

A

integers

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

If a variable is 5, is it true or false?

A

True - anything that is not zero is true

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

What is the difference between logical and bitwise operations?

A

Bitwise takes the bit representation of the number and then compares each pair of bits separately

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

How do you use an abbreviated form of bitwise operators?

A

x = x & y is x &= y

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

How do you replace an item in a list?

A

listname[index value] = newvalue

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

what is del?

A

and instruction not a funcction

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

how do you get the last item in the list?

A

list[-1]

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

What is a method versus a function?

A

A method is a kind of function but it is owned by the data it works for and can change the internal state of the data

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

How do you add to a list?

A

Append (adds to the end)

Insert (you specific the location, and everything moves to the right)

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

Can you use list[new index] to add to a lits?

A

no

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

How do you use a for loop to add up all items in a list?

A
list = [1,2,3,4,5]
total = 0

for i in list:
total += i

print(total)

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

How do you swap the variables in a list?

A
var1 = 4
var2 = 6

var1, var2 = var2, var1

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

Is a list ordered or unordered?

A

Ordered

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

Where does mylist[:end] start?

A

0

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

How do you copy the full list?

A

mylist[:]

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

How do you remove some items of a list?

A

del mylist[:4]

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

Does using del create a new list?

A

no

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

How do you delete a list contents?

A

del mylist[:]

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

How do you delete a list?

A

del mylist

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

How do you work out if something is in a list?

A

in or not in

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

What is the difference between:
list2 = list1
list2 = list1[:]

A

The second copies the contents, the first points to the location of list 1 so if you change list it is refleccted

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

Where do funcitons come from?

A

Python (built in), modules, code

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

What is special about a parameter?

A

It only exists inside a function

Assigning a value to it only happens specifying an argument when invoking the function

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

Can a variable and a parameter be named the same?

A

Yes

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

What are the ways ways to pass parameters?

A

Positional and keyword

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

How do you get a function to insert default value for a parameter?

A

def function(keyword = ‘default’)

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

Can you overwrite a default parameter in a function?

A

Yes

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

What comes first, positional or keyword arguments?

A

Positional

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

How many types of return are there?

A

Return without an expression

Return with an expression

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

What does return without an expression do?

A

Stop the funcions activity on demand and return to the point of invocation

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

What does return with an expression do?

A

Stops the functions execution and then evaluates the expression’s value and return the functions result

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

What is return with an expression

A

Something after the return instruction

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

What must you do to see the return with an expressions results?

A

Assign it to a variable and print it

86
Q

What is None?

A

A keyword?

87
Q

What happens when a function doesn’t return a certain value using return with an expression?

A

Returns None

88
Q

Can you pass a list to a function?

A

Yes

89
Q

How does insert work?

A

Add an item to the list at a specific value list.insert(0,i)

90
Q

Does a variable defined outside the body of a function exist inside?

A

Yes

91
Q

What is the exception to a variable being read from outside the function?

A

A variable existing outside a function has a scope inside the functions’ bodies, excluding those of them which define a variable of the same name.

92
Q

Does a function receive an argument or an arguments value?

A

Arguments value

93
Q

Can you change a paramters value?

A

Yes

94
Q

True or false: A variable that exists outside a function has a scope inside the function body

A

True

95
Q

True or false: A variable that exists outside a function does not have scope inside the function body

A

False

96
Q

How do you get a variable to be applicable outside a functions body?

A

global var

97
Q

What is mutable and immutable data?

A

Immutable cannot be chnaged

98
Q

What is a sequence?

A

A sequence type is a type of data in Python which is able to store more than one value (or less than one, as a sequence may be empty), and these values can be sequentially (hence the name) browsed, element by element.

99
Q

What is the syntax for a tuple?

A
tuple1 = (1, 2, 4, 8)
tuple2 = 1., .5, .25, .125
100
Q

If you print:
tuple1 = (1, 2, 4, 8)
tuple2 = 1., .5, .25, .125
What do you see?

A

(1, 2, 4, 8)

1.0, 0.5, 0.25, 0.125

101
Q

Can tuple elements be of different types?

A

Yes. a tuple’s elements can be variables, not only literals. Moreover, they can be expressions if they’re on the right side of the assignment operator.

102
Q

Can a tuple be empty?

A

Yes

103
Q

How do you create an empty tuple?

A

emptyTuple = ()

104
Q

What do you have to do to create a one element tuple?

A
oneElementTuple1 = (1, )
oneElementTuple2 = 1., 

Add a comma to distinguish it from single value

105
Q

What happens if you + tuples together?

A

It joins them together

106
Q

What happens if you * a tuple?

A

It multiples them

107
Q

What works on tuples?

A

len()
+
*
In and not in

108
Q

Is a dictionary a sequence?

A

No

109
Q

Is a dictionary mutable?

A

yes

110
Q

What is a dictionary?

A

This means that a dictionary is a set of key-value pairs.

111
Q

Keys in a dictionary can be the same: T or F?

A

False

112
Q

What kinds of data can be in dictionary/

A

Anything except lists

113
Q

What do len for a dictionary give you?

A

The number of key-value elements

114
Q

What is a dictinoary syntax?

A

dictionary = {“cat” : “chat”, “dog” : “chien”, “horse” : “cheval”}
The list of pairs is surrounded by curly braces, while the pairs themselves are separated by commas, and the keys and values by colons.

115
Q

Is a dictionary ordered or unordered?

A

Unordered

116
Q

Are keys in a dictionary case sensitive or not?

A

Yes

117
Q

Can you use a for loop on a dictionary?

A

No

118
Q

How can you get a for loop to work on on dictionary?

A

The first of them is a method named keys(), possessed by each dictionary. The method returns an iterable object consisting of all the keys gathered within the dictionary. Having a group of keys enables you to access the whole dictionary in an easy and handy way.

119
Q

How do you extract bits out of dictionaries?

A

.items()
.values()
.keys()

120
Q

What data types does dict.items() return?

A

A tuple

121
Q

How do you replace a value within a dictionary?

A

dict[‘key’] = ‘new value’

122
Q

How do you add a new key, value pair to a dictionary?

A

dict[‘new key’] = ‘value’
or
dict.update({‘duck’: ‘canard’})

123
Q

With lists can you assign a value to a non existnent index?

A

No

124
Q

How do you remove a key from a dictionary?

A

del dict[‘key’]

125
Q

What can you do to edit a dictionary?

A

Update values - dict[‘original’] = ‘new’
Add new values dict[‘new key’]
Delete - del dict[‘key’]

126
Q

How can items be added to a list?

A

Append - if a list, adds the list to the list

Extend - adds individual elements

127
Q

How do you create a tuple?

A

(1,2,3)

128
Q

How do you create a one element tuple?

A

(1,) with a comma otherwise a variable is created

129
Q

Can you access elements of a tuple by indexing?

A

Yes

130
Q

How do you create an empty tuple?

A

()

131
Q

What are tuples?

A

Immutable and ordered/

132
Q

Can you iterate over a tuple?

A

Yes

133
Q

How do you work out if something is in a tuple?

A

print( 5 in tup)

in or not in

134
Q

Can you join or mutliple tuples?

A

Yes

135
Q

How do you convert an existing element to a tuple?

A

tuple(lst)

136
Q

How do you iterate over a dictionary?

A

for keys, values in dict1.items():

print(keys, values)

137
Q

What can you do to check if a key is in a dictionary?

A

if “key” in dict:

print(“yes”)

138
Q

How do you delete an item from a dicctionary?

A

del dict1[‘key1’]

139
Q

What is the difference between clear and delete for dictionary/

A

clear removes the items, delete actually gets rid of it

140
Q

How do you create a dictionary from another data type?

A

dict()

141
Q

How do you make range go negative

A

You must specify the last argument to be -1 otherwise it won’t work

142
Q

What data type do you get from this?

tup[0:3]

A

Tuple

143
Q

What data type do you get from this?
tup = (1,2,3)
tup[1]

A

integer

144
Q

Why is this bad? From math import *

A

Because every function is brought into the glboal scope and could conflict with your variables

145
Q

What does from maths import mean enable you to do?

A

Use mean() without having to do maths.mean()

146
Q

Is a tuple immutable?

A

Yes but you can overrite it - it points to a new object

147
Q

How do you get the unique refence of an object?

A

Id()

148
Q

Do you need () for a tupe?

A

No

149
Q

How do you create a single item tuple?

A

(‘Red’,)

150
Q

Tuple or string? (‘Red’)

A

String - for it to be a tuple it would have to be (‘Red’,)

151
Q

How do you access elements of a tuple?

A

Square brackets - just like lists

152
Q

Tuple1 = (10,20,30). Is tuple1 the object or is (10,20,30)?

A

(10,20,30) is the object

153
Q

Tuple1 = (10,20,30) and then tuple 2 = Tuple 2. If you then modify Tuple1, does tuple 2 change?

A

No because tuples are immutable. MOdifying tuple1 creates a new object whereas tuple2 points to the original object

154
Q

If you append a tuple to a list what happens?

A

The nubmers are added to to the end of the list (as a list)

155
Q

Can tuples have mutable objects?

A

Yes (1,2,[‘hello’, ‘bye’])

156
Q

If you modify a mutable oject within a tuple, does it create a new object?

A

No

157
Q

How do you unpack the tuple?

A

x, y = (‘Amanda’,[1,2,3])

158
Q

What does enumerate do?

A

Products a sequence of tuples with the index and item of the list.

159
Q

How do you get an output like this?

0: red
1: white
3: blue

A

for index,value in enumerate([‘red’,’white’,’blue’]):

print(f’{index}: {value}’)

160
Q

How do you delete items from a list?

A

del numbers[-1]

161
Q

Using del - can you delete items from a list, a whole variable or both?

A

Both

162
Q

How do you select every other item from a list?

A

ls[::2]

163
Q

Sorted() or ls.sort(). One is method one is a fuction, which way around?

A

Sorted is a built in function, sort is method

164
Q

ls.sort() - does it happen in place?

A

Yes

165
Q

Can you use sorted() on immuatable types?

A

Yes but you have to assign it to a new variable name

166
Q

Sorted() vs ls.sort() which one is in place?

A

ls.sort() is in place

167
Q

What happens if you mix caps and non caps when sorting?

A

It might not come alphabetically as items are sorted lexigraphically

168
Q

How do you find the location within an item in a list?

A

list.index(5)

169
Q

What do you use to evaluation if something is contained in a list?

A

in
1000 in numbers
False

170
Q

How do you generate a list using rangewithout using append?

A

list comprehension

list = [i for i in range(0,6)

171
Q

How do you generate a list only if certain conditions are met

A

list = [i for i in range(0,6) if i % 2 != 0]

172
Q

What is the difference between list comprehension and generator expressions?

A

List comprehension is greedy - it means that the output has to be generated immediately. Genetaor expressions us lazy evaluation - values are produced on demand
List is []
Generator is ()

173
Q

what is filter?

A

An inbuilt higher order function (can take other functions)

174
Q

How do you use filter?

A

list(filter(somefunction, the item to filter)

The somefunction can either be a function name or lambda e.g.

List(filter(lambda x : ( x > 4 ) == True, somelist)

175
Q

What is the structure of a lambda expression?

A

lamba x: x % 2 != 0

176
Q

Do map and filter actually return the sequences of elements?

A

No it returns an iterator - you have to iterate over the object to get the sequence of values. list() does this

177
Q

When do you use map versus filter?

A

Map when you want to do something to every element, filter when you want to filter on the elements

178
Q

What does zip do?

A

Combines parts of different lists / tuples

179
Q

If you create a list of lists - do the rows have to have the same number of columns?

A

No - jagged array

180
Q

If you want to index a multidimensional list, what comes first row or column?

A

Row

181
Q

How do you index a multidimensional list? a[1,2] or a[1][2]

A

a[1][2]

182
Q

Can the keys of a dictionary be mutable?

A

No

183
Q

What does len of a dictionary reveal?

A

The number of key value pairs

184
Q

What is the difference between del x and x.clear()

A

Del removes the variable name, clear keeps the variable name

185
Q

What does dictionary.items() give you?

A

An iterator which you can iterate over - gives a tuple of key value pairs

186
Q

Is a dictionary mutable?

A

Yes

187
Q

How do you add a new dicitonary key value pair?

A

dict[‘new label’]

188
Q

What order does the dictionary show?

A

The order they were added

189
Q

How do you remove an element from a dictionary?

A

del dict[‘element’]

190
Q

When you in to search for an item in a dictionary - does it look at keys, values or both?

A

keys

191
Q

What is the opposite of in

A

Not in

192
Q

What does this do? print(i, end = ‘ ‘)

A

Prints items on one line with a space between

193
Q

What can you use to act as a word counter?

A

from collections import Counter

194
Q

How can you update a dctionary on mass

A

dictionary.update({})

195
Q

dictionary.update({}) or dictionary update(key = value)

A

Either works

196
Q

Can you pass a tuple to dictionary.update?

A

Yes

197
Q

What is the difference between a set and dictionary?

A

{} but not key value pair in set. Items in sets are not in the order they are inserted in

198
Q

Can you have duplicates in a set?

A

No

199
Q

How do you create a set?

A

{} with a sequence of values or set()

200
Q

How do you create an empty set?

A

set(), {} won’t work

201
Q

Are sets mutable or immutable?

A

Mutable but there is a frozen set which is immutable

202
Q

When comparing sets, does the order of the items matter?

A

No

203
Q

What does < do when comparing sets?

A

Tests if it is sub set

204
Q

What will this output? {1,2,3}

A

False, there must be fewer elements in the left hand one

205
Q

What is the difference between < and issubset()?

A

issubset() will evaluate to true if the sets are equal

206
Q

How do you merge 2 sets?

A

or union()

207
Q

How do you return only the overlapping items between 2 sets?

A

& or intersection()

208
Q

How do you get items that are only in one set (the left one)

A
  • or difference()
209
Q

How do you get the items that are not common across both sets?

A

^ or symmetric_difference

210
Q

How can you change elements in set?

A

.add() .remove()

211
Q

What is the structure to use map?

A

List(map(lambda x: x*2, thelist))

212
Q

Can you use map() or do you need to do something else?

A

It produces something you can iterate over so you need to use list()