Python Flashcards
What are the different types of errors?
SyntaxErrors - Detectedbytheinterpreter
RuntimeErrors – Causes the program to abort
LogicErrors – Produces in correct result
what is the eval function?
eval()
The computer evaluates, and guesses what the user may be trying to input.
how to extract a substring from a variable
s[2:4]
substring from index 3 to the end
s[3:]
What is the method to get the character length?
len()
how to replace characters within a string
STRINGVARIABLENAME.replace(‘FIRSTLETTER’,’REPLACINGLETTER’)
what method removes leading/trailing whitespaces
variable.strip()
what is the python method how to make substrings
split()
what method provides the type for a variable?
type()
how to slice an index from a certain part of s string, to another, via a interval step
s.[3:8:2]
How to summon the notepad from the command line in windows command prompt
notepad filename.extension
What are the programming naming conventions (e.g. what “case” would you name a variable as? etc.)
Both Functions and Variable names, and Package and Module names - snake case
Class names – CapitalizedWords (or CamelCase)
What are the three types of operators?
1) Unary — works on one operator (e.g. a negative sign)
2) Binary — works with two operands
3) Ternary — works with three (i.e. known as conditions statements)
How does the range() work?
is the stop number inclusive or exclusive?
range(stop)
generates an arithmetic progression of integers starting from
0 to stop-1 with the step of 1 ❑ range(start, stop)
start to stop-1 with the step of 1 ❑ range(start, stop, step)
generates an arithmetic progression of integers starting from start to stop-1 with the step of step
list.append(elm)
adds the element to the end of the list
Adds all elements of the sequence to the list
list.extend(seq)
Linear Search
The linear search approach compares the key element, key, sequentially with each element in the list. The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is found, the linear search returns the index of the element in the list that matches the key. If no match is found, it returns -1.
What is the python code for binary search?
def binary_search(lst, key): lo = 0 hi = len(lst)-1 while lo <= hi: mid = (lo+hi)//2 if lst[mid] < key: lo = mid+1 elif lst[mid] > key: hi = mid-1 else: return mid return -1
Selection sort implementation syntax
def selection_sort(lst): for i in range(len(lst)): lo = i for j in range(i+1, len(lst)): if lst[j] < lst[lo]: lo = j my_list = [5, 4, -3, 2, -53, 16, 0] selection_sort(my_list) print(my_list)
what are compiled error also called?
check error
what is the keyword used to the throw an exceptions
1) raise this is used for a custom exception doesn't need to be in a conditional 2) assert #this is more commonly used assertion error is less customize-able 3) try...except..else block
What normally goes into the body of the try…except..else..finally clause
log
what is the difference between finally and writing outside of the block
In the event of an unhandled error, the ‘finally’ of the try block will run. But if that same clause is written after the try clause, it will not run.
how to check the type of a variable
isinstance(dvd, float)
for a try statements if you were to put a definition within the body of the try block
It will not run.
You cannot put a function/method definition within the try block