General python Flashcards

(212 cards)

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
What do you need to do to get input usable with operators?
float()
26
What does isna() return?
Boolean series
27
How do you count how many missing values?
df.isna().sum()
28
How do you get a dataframe into a list?
df.values.flatten()
29
How do you get summary statistics by row?
1) data = pd.DataFrame() 2) var = df.mean(axis = 1) 3) data['var'] = var
30
What is the keyboard shortcut to see all methods?
tab after the .
31
What is the keyboard shortcut to see the arguments available?
shift and tab
32
What is the difference between a method and an attribute?
Attribute describes and has no parentheses, methods have parentheses
33
Describe ==
Binary, left binded
34
In python what are the comparison operators?
==, !=, >, =, <=
35
What has higher priority: == or >=
>=
36
What is the difference between if and elif?
If will always be evaluated, elif and else stop once something is found to be true (and assumes the previous thing was false)
37
What does range(100) do?
0 to 99
38
What does range(2,8) do?
2 to 7
39
What does range(2,8,3)
2, 5
40
What does break do?
Exits the loop immediately
41
What does continue do?
Behaves as if the programme has reached the end of the body of the loop and starts the next turn
42
What is the difference between break and continue?
Break exits the loop, continue goes to start the next loop immediately
43
What are the logical operators?
and or not
44
What are the logical operators vs the comparison operators?
Comparison >= > != | Logical and or not
45
What has higher priority; comparison operators or logical?
Comparison
46
Is and binary or unary?
binary
47
What has higher priority; and or or?
and
48
What does not do?
It turns false into true and true into false?
49
What priority is not equivalent to?
unary + and -
50
What are the bitwise operators?
& | ~ ^
51
Which are the logical operators and which are the bitwise operators?
Logical: and, or, not Bitwise: & | ~ ^
52
What is the difference between | and ~ ?
| needs at least 1 1 - ~ needs exactly 1 1
53
Do bitwise operators need integers or flats?
integers
54
If a variable is 5, is it true or false?
True - anything that is not zero is true
55
What is the difference between logical and bitwise operations?
Bitwise takes the bit representation of the number and then compares each pair of bits separately
56
How do you use an abbreviated form of bitwise operators?
x = x & y is x &= y
57
How do you replace an item in a list?
listname[index value] = newvalue
58
what is del?
and instruction not a funcction
59
how do you get the last item in the list?
list[-1]
60
What is a method versus a function?
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
61
How do you add to a list?
Append (adds to the end) | Insert (you specific the location, and everything moves to the right)
62
Can you use list[new index] to add to a lits?
no
63
How do you use a for loop to add up all items in a list?
``` list = [1,2,3,4,5] total = 0 ``` for i in list: total += i print(total)
64
How do you swap the variables in a list?
``` var1 = 4 var2 = 6 ``` var1, var2 = var2, var1
65
Is a list ordered or unordered?
Ordered
66
Where does mylist[:end] start?
0
67
How do you copy the full list?
mylist[:]
68
How do you remove some items of a list?
del mylist[:4]
69
Does using del create a new list?
no
70
How do you delete a list contents?
del mylist[:]
71
How do you delete a list?
del mylist
72
How do you work out if something is in a list?
in or not in
73
What is the difference between: list2 = list1 list2 = list1[:]
The second copies the contents, the first points to the location of list 1 so if you change list it is refleccted
74
Where do funcitons come from?
Python (built in), modules, code
75
What is special about a parameter?
It only exists inside a function | Assigning a value to it only happens specifying an argument when invoking the function
76
Can a variable and a parameter be named the same?
Yes
77
What are the ways ways to pass parameters?
Positional and keyword
78
How do you get a function to insert default value for a parameter?
def function(keyword = 'default')
79
Can you overwrite a default parameter in a function?
Yes
80
What comes first, positional or keyword arguments?
Positional
81
How many types of return are there?
Return without an expression | Return with an expression
82
What does return without an expression do?
Stop the funcions activity on demand and return to the point of invocation
83
What does return with an expression do?
Stops the functions execution and then evaluates the expression's value and return the functions result
84
What is return with an expression
Something after the return instruction
85
What must you do to see the return with an expressions results?
Assign it to a variable and print it
86
What is None?
A keyword?
87
What happens when a function doesn't return a certain value using return with an expression?
Returns None
88
Can you pass a list to a function?
Yes
89
How does insert work?
Add an item to the list at a specific value list.insert(0,i)
90
Does a variable defined outside the body of a function exist inside?
Yes
91
What is the exception to a variable being read from outside the function?
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
Does a function receive an argument or an arguments value?
Arguments value
93
Can you change a paramters value?
Yes
94
True or false: A variable that exists outside a function has a scope inside the function body
True
95
True or false: A variable that exists outside a function does not have scope inside the function body
False
96
How do you get a variable to be applicable outside a functions body?
global var
97
What is mutable and immutable data?
Immutable cannot be chnaged
98
What is a sequence?
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
What is the syntax for a tuple?
``` tuple1 = (1, 2, 4, 8) tuple2 = 1., .5, .25, .125 ```
100
If you print: tuple1 = (1, 2, 4, 8) tuple2 = 1., .5, .25, .125 What do you see?
(1, 2, 4, 8) | 1.0, 0.5, 0.25, 0.125
101
Can tuple elements be of different types?
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
Can a tuple be empty?
Yes
103
How do you create an empty tuple?
emptyTuple = ()
104
What do you have to do to create a one element tuple?
``` oneElementTuple1 = (1, ) oneElementTuple2 = 1., ``` Add a comma to distinguish it from single value
105
What happens if you + tuples together?
It joins them together
106
What happens if you * a tuple?
It multiples them
107
What works on tuples?
len() + * In and not in
108
Is a dictionary a sequence?
No
109
Is a dictionary mutable?
yes
110
What is a dictionary?
This means that a dictionary is a set of key-value pairs.
111
Keys in a dictionary can be the same: T or F?
False
112
What kinds of data can be in dictionary/
Anything except lists
113
What do len for a dictionary give you?
The number of key-value elements
114
What is a dictinoary syntax?
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
Is a dictionary ordered or unordered?
Unordered
116
Are keys in a dictionary case sensitive or not?
Yes
117
Can you use a for loop on a dictionary?
No
118
How can you get a for loop to work on on dictionary?
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
How do you extract bits out of dictionaries?
.items() .values() .keys()
120
What data types does dict.items() return?
A tuple
121
How do you replace a value within a dictionary?
dict['key'] = 'new value'
122
How do you add a new key, value pair to a dictionary?
dict['new key'] = 'value' or dict.update({'duck': 'canard'})
123
With lists can you assign a value to a non existnent index?
No
124
How do you remove a key from a dictionary?
del dict['key']
125
What can you do to edit a dictionary?
Update values - dict['original'] = 'new' Add new values dict['new key'] Delete - del dict['key']
126
How can items be added to a list?
Append - if a list, adds the list to the list | Extend - adds individual elements
127
How do you create a tuple?
(1,2,3)
128
How do you create a one element tuple?
(1,) with a comma otherwise a variable is created
129
Can you access elements of a tuple by indexing?
Yes
130
How do you create an empty tuple?
()
131
What are tuples?
Immutable and ordered/
132
Can you iterate over a tuple?
Yes
133
How do you work out if something is in a tuple?
print( 5 in tup) | in or not in
134
Can you join or mutliple tuples?
Yes
135
How do you convert an existing element to a tuple?
tuple(lst)
136
How do you iterate over a dictionary?
for keys, values in dict1.items(): | print(keys, values)
137
What can you do to check if a key is in a dictionary?
if "key" in dict: | print("yes")
138
How do you delete an item from a dicctionary?
del dict1['key1']
139
What is the difference between clear and delete for dictionary/
clear removes the items, delete actually gets rid of it
140
How do you create a dictionary from another data type?
dict()
141
How do you make range go negative
You must specify the last argument to be -1 otherwise it won't work
142
What data type do you get from this? | tup[0:3]
Tuple
143
What data type do you get from this? tup = (1,2,3) tup[1]
integer
144
Why is this bad? From math import *
Because every function is brought into the glboal scope and could conflict with your variables
145
What does from maths import mean enable you to do?
Use mean() without having to do maths.mean()
146
Is a tuple immutable?
Yes but you can overrite it - it points to a new object
147
How do you get the unique refence of an object?
Id()
148
Do you need () for a tupe?
No
149
How do you create a single item tuple?
('Red',)
150
Tuple or string? ('Red')
String - for it to be a tuple it would have to be ('Red',)
151
How do you access elements of a tuple?
Square brackets - just like lists
152
Tuple1 = (10,20,30). Is tuple1 the object or is (10,20,30)?
(10,20,30) is the object
153
Tuple1 = (10,20,30) and then tuple 2 = Tuple 2. If you then modify Tuple1, does tuple 2 change?
No because tuples are immutable. MOdifying tuple1 creates a new object whereas tuple2 points to the original object
154
If you append a tuple to a list what happens?
The nubmers are added to to the end of the list (as a list)
155
Can tuples have mutable objects?
Yes (1,2,['hello', 'bye'])
156
If you modify a mutable oject within a tuple, does it create a new object?
No
157
How do you unpack the tuple?
x, y = ('Amanda',[1,2,3])
158
What does enumerate do?
Products a sequence of tuples with the index and item of the list.
159
How do you get an output like this? 0: red 1: white 3: blue
for index,value in enumerate(['red','white','blue']): | print(f'{index}: {value}')
160
How do you delete items from a list?
del numbers[-1]
161
Using del - can you delete items from a list, a whole variable or both?
Both
162
How do you select every other item from a list?
ls[::2]
163
Sorted() or ls.sort(). One is method one is a fuction, which way around?
Sorted is a built in function, sort is method
164
ls.sort() - does it happen in place?
Yes
165
Can you use sorted() on immuatable types?
Yes but you have to assign it to a new variable name
166
Sorted() vs ls.sort() which one is in place?
ls.sort() is in place
167
What happens if you mix caps and non caps when sorting?
It might not come alphabetically as items are sorted lexigraphically
168
How do you find the location within an item in a list?
list.index(5)
169
What do you use to evaluation if something is contained in a list?
in 1000 in numbers False
170
How do you generate a list using rangewithout using append?
list comprehension | list = [i for i in range(0,6)
171
How do you generate a list only if certain conditions are met
list = [i for i in range(0,6) if i % 2 != 0]
172
What is the difference between list comprehension and generator expressions?
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
what is filter?
An inbuilt higher order function (can take other functions)
174
How do you use filter?
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
What is the structure of a lambda expression?
lamba x: x % 2 != 0
176
Do map and filter actually return the sequences of elements?
No it returns an iterator - you have to iterate over the object to get the sequence of values. list() does this
177
When do you use map versus filter?
Map when you want to do something to every element, filter when you want to filter on the elements
178
What does zip do?
Combines parts of different lists / tuples
179
If you create a list of lists - do the rows have to have the same number of columns?
No - jagged array
180
If you want to index a multidimensional list, what comes first row or column?
Row
181
How do you index a multidimensional list? a[1,2] or a[1][2]
a[1][2]
182
Can the keys of a dictionary be mutable?
No
183
What does len of a dictionary reveal?
The number of key value pairs
184
What is the difference between del x and x.clear()
Del removes the variable name, clear keeps the variable name
185
What does dictionary.items() give you?
An iterator which you can iterate over - gives a tuple of key value pairs
186
Is a dictionary mutable?
Yes
187
How do you add a new dicitonary key value pair?
dict['new label']
188
What order does the dictionary show?
The order they were added
189
How do you remove an element from a dictionary?
del dict['element']
190
When you in to search for an item in a dictionary - does it look at keys, values or both?
keys
191
What is the opposite of in
Not in
192
What does this do? print(i, end = ' ')
Prints items on one line with a space between
193
What can you use to act as a word counter?
from collections import Counter
194
How can you update a dctionary on mass
dictionary.update({})
195
dictionary.update({}) or dictionary update(key = value)
Either works
196
Can you pass a tuple to dictionary.update?
Yes
197
What is the difference between a set and dictionary?
{} but not key value pair in set. Items in sets are not in the order they are inserted in
198
Can you have duplicates in a set?
No
199
How do you create a set?
{} with a sequence of values or set()
200
How do you create an empty set?
set(), {} won't work
201
Are sets mutable or immutable?
Mutable but there is a frozen set which is immutable
202
When comparing sets, does the order of the items matter?
No
203
What does < do when comparing sets?
Tests if it is sub set
204
What will this output? {1,2,3}
False, there must be fewer elements in the left hand one
205
What is the difference between < and issubset()?
issubset() will evaluate to true if the sets are equal
206
How do you merge 2 sets?
| or union()
207
How do you return only the overlapping items between 2 sets?
& or intersection()
208
How do you get items that are only in one set (the left one)
- or difference()
209
How do you get the items that are not common across both sets?
^ or symmetric_difference
210
How can you change elements in set?
.add() .remove()
211
What is the structure to use map?
List(map(lambda x: x*2, thelist))
212
Can you use map() or do you need to do something else?
It produces something you can iterate over so you need to use list()