a = {'hello', 0, 439.23, "world"}
print(sum(a))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
There are 2 functions A and B. A calls B and B calls A. What are these functions called?
Mutually Recursive Functions
Functions A and B are mutually recursive if A calls B and B calls A.
Two Truths and a Lie:
1. open(filename, mode) will set the file pointer to the beginning of the file.
2. fileHandler.read(k) will return the next k lines in the file as a list of strings.
3. You cannot simultaneously read and write the same file in Python.
2. is false. fileHandler.read(k) will return the next k characters from the file as a string.
myObj = {0: 'A', 1: 'B', 2: 'C'}
How will you delete the key-value pair (1, 'B') from this myObj?
del myObj[1]
General Syntax: del dictionaryName[key]
or you may also use myObj.pop(1)
The general syntax for using pop is dictionaryName.pop(key).
import random
lst = [0, 1, 2, 3, 4]
result = random.shuffle(lst)
print(result)
None
open(filename, mode)
What is the name of the object returned by this function call? What is it used for?
The file object that is returned on opening a file is also called a file handle. It can be used within the program to access and interact with the file.
What are two main differences between print and write?
1. print inserts a newline at the end of each line, unless specified otherwise (end = ""). write does not add a newline at the end of each line by default.
2. print takes an arbitrary number of arguments and coerces them all to strings; write only takes one argument and it must be a string.
How many comparisons, on average, does it take to find an element using binary search in a list of n items?
log2(n)
Each step in binary search cuts the search space in half. Hence, with n items, the whole search space can be cut in half log2 (n) times.
lst = [x ** 2 for x in range(100)]
print( len(set(lst)) == len(lst) )
Output: True
Explanation:
lst = [x ** 2 for x in range(100)] will be the list of squares of integers from 0 to 99.
len(set(lst)) == len(lst) checks if lst has unique values only. Since each nonnegative integer's square is unique, lst will have no duplicate values.
What's the term for
selecting a contiguous subsequence of a sequence
Slicing means to select a contiguous subsequence of a sequence (Ex: strings, lists).
General Form:
string[start : end]
lst[startIdx : endIdx]
RecursionError: maximum recursion depth exceeded in comparison
What does this error mean? How can you resolve this error?
Meaning: The runtime stack has overflown - the number of recursive function calls has exceeded the “recursion depth.”
Ways to Solve the Error:
1. Check your base case and any missed cases.
2. Try an iterative solution.
3. Try to find a closed form solution (i.e. can be solved mathematically / in a finite number of standard operations).
Two Truths and a Lie
1. In Python sets, the alternate syntax for a.symmetric_difference(b) is a ^ b.
2. In Python, strings, lists, tuples, and dictionaries are sub-scriptable objects.
3. In Python sets, the alternate syntax for s1.issubset(s2) is s1 < s2.
3. is false. In Python sets, the alternate syntax for s1.issubset(s2) is s1 <= s2 (NOT s1 < s2), since the issubset() method does not check for a proper subset.
Based on the issubset() and issuperset methods, a set is a subset and a superset of itself.
The < and > operators check specifically for proper subsets and supersets. s1 is a proper subset of s2 if s1 is a subset of s2, but not equal to s2.