General python Flashcards
How do you get a new blank row into code?
\n , print()
What is the escape character?
\
What does \ do?
Announces that the string should be paused and that the character after means something
How do you print the character \
print(“\”)
What happens when you pass muliple arguments to a print function
1) It combines them all on one line
2) It puts a small between the outputted arguments
What is a positional argment?
The location determines what it does
How do you use keyword arguments?
1) keyword = value
2) after positional arguments
What does the default end keywork argument do in print
\n
How do you alter the default behaviour of print to insert a space between arguments?
sep =
What is a literal?
Data you can determine the value of without any additional knowledge e.g. 123 vs c
What are the 2 ways to show quotation marks in Python
" or use ‘’ instead of “”
How do you encode ‘to the power of’
**
What does print(6/2) give you?
A float
What does // do?
Integer division
What happens when the result of // is not an integer?
Rounds to the lower number (ie. floor division)
What is the result of print( 6 // - 4)?
-2
What type does the % operation result in?
Float
what is a unary and a binary operatory
Binary expect 2 arguments, unary expects 1
Is python predominantly right side or left side binding?
Left side
What is the exception to left side binding
Exponentiation
What is the order of priority of operators?
1) + - (unary)
2) **
3) * / %
4) + - (binary)
5) , >=
6) ==, !=
What does a variable contain?
A name and value
How do you evaluate the square root of something?
** 0.5
What does input() store values as?
A string
What do you need to do to get input usable with operators?
float()
What does isna() return?
Boolean series
How do you count how many missing values?
df.isna().sum()
How do you get a dataframe into a list?
df.values.flatten()
How do you get summary statistics by row?
1) data = pd.DataFrame()
2) var = df.mean(axis = 1)
3) data[‘var’] = var
What is the keyboard shortcut to see all methods?
tab after the .
What is the keyboard shortcut to see the arguments available?
shift and tab
What is the difference between a method and an attribute?
Attribute describes and has no parentheses, methods have parentheses
Describe ==
Binary, left binded
In python what are the comparison operators?
==, !=, >, =, <=
What has higher priority: == or >=
> =
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)
What does range(100) do?
0 to 99
What does range(2,8) do?
2 to 7
What does range(2,8,3)
2, 5
What does break do?
Exits the loop immediately
What does continue do?
Behaves as if the programme has reached the end of the body of the loop and starts the next turn
What is the difference between break and continue?
Break exits the loop, continue goes to start the next loop immediately
What are the logical operators?
and
or
not
What are the logical operators vs the comparison operators?
Comparison >= > !=
Logical and or not
What has higher priority; comparison operators or logical?
Comparison
Is and binary or unary?
binary
What has higher priority; and or or?
and
What does not do?
It turns false into true and true into false?
What priority is not equivalent to?
unary + and -
What are the bitwise operators?
& | ~ ^
Which are the logical operators and which are the bitwise operators?
Logical: and, or, not
Bitwise: & | ~ ^
What is the difference between | and ~ ?
needs at least 1 1 - ~ needs exactly 1 1
Do bitwise operators need integers or flats?
integers
If a variable is 5, is it true or false?
True - anything that is not zero is true
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
How do you use an abbreviated form of bitwise operators?
x = x & y is x &= y
How do you replace an item in a list?
listname[index value] = newvalue
what is del?
and instruction not a funcction
how do you get the last item in the list?
list[-1]
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
How do you add to a list?
Append (adds to the end)
Insert (you specific the location, and everything moves to the right)
Can you use list[new index] to add to a lits?
no
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)
How do you swap the variables in a list?
var1 = 4 var2 = 6
var1, var2 = var2, var1
Is a list ordered or unordered?
Ordered
Where does mylist[:end] start?
0
How do you copy the full list?
mylist[:]
How do you remove some items of a list?
del mylist[:4]
Does using del create a new list?
no
How do you delete a list contents?
del mylist[:]
How do you delete a list?
del mylist
How do you work out if something is in a list?
in or not in
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
Where do funcitons come from?
Python (built in), modules, code
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
Can a variable and a parameter be named the same?
Yes
What are the ways ways to pass parameters?
Positional and keyword
How do you get a function to insert default value for a parameter?
def function(keyword = ‘default’)
Can you overwrite a default parameter in a function?
Yes
What comes first, positional or keyword arguments?
Positional
How many types of return are there?
Return without an expression
Return with an expression
What does return without an expression do?
Stop the funcions activity on demand and return to the point of invocation
What does return with an expression do?
Stops the functions execution and then evaluates the expression’s value and return the functions result
What is return with an expression
Something after the return instruction