A scripting language! Though Programming Language is also accepted.
What is the difference between a for loop and a while loop?
For loops typically has a set boundary of numbers to iterate through at the second when the for i in something is processed.
While loops typically does not know when the loop will end, thus it can go infinite.
Define what is a function?
function is where you have an input and an output.
What are the three modes in opening a file?
Could you also name the hiearchy of it? Such if implemented in one mode, it will also give access to another mode.
Read, Write, Append
Name at least 6 basic built-in types for python
String, Int, Float, Bool, List, Dict, Tuple, Set
What are implicit & explicit bounds?
How would you iterate over a 2-D list? (each inner list may have a different length)
You can set up 2 for loops like this:
for i in range(len(L)):
for j in range(len(L[i]))
for i in L:
for j in i:
EC: What will happen if you do not include a return, what is the default value that any functions return?
return functions gives the output to wherever a function call occured.
default return is None.What is the better way of opening files so that you don't have to worry about file closing?
the "with" operator.
Name the difference between List, Set, Dict and Tuple
List -> Basic container type
Dict -> allow key and value pairs, key must be immutable
Set -> disallow duplicates.
Tuple -> immutable
List indexing is when you do L[0] or L[1]
List slicing is L[0:10]
Name 2 ways that you can iterate over a dictionary.
If find a third way that uses both a .keys() and .values() method, then EC!
for k,v in dict.items()
orfor k in dict:
# k is key, then dict[k] is val
orfor k,v in zip(dict.keys(),dict.values()):
What is the difference between pre and post condition asserts?
Pre usually goes inside the function and test to see if parameter or inputs are correct or not.
Post usually goes outside the function, typically after the function call, to test if the output is the expected output.What is a csv file, what is the pattern inside the file?
name,ID,year
hitoki,123,2025
sam,234,2026
john,589,2027
Write a code such that it defines a namedtuple of four fields called "Student", the four field can be anything. Then actually use this Student namedtuple to make a dummy tuple.
Student = namedtuple("Student",["name", "ID", "classEnrolled", "year"])
hitoki = Student("Hitoki", "123456789", ["ICS31","ICS32"], 2)
sorted(L, reverse = True)
Name 3 functions that you can use over a list so that it helps in using loops. like range()
range()
enumerate()
zip()
What are try-except-finally? Write an example that will cause an error to occur and your except should catch it.
Try except finally block allows testing code to see if it works, then if it does not work then there is a corresponding code that match with the error as a second plan. Note the finally block will always run no matter what.
raise is to manually create an error. Syntax is raise TypeErrorWhat value of x in the following code will print True?
print(True if (x % 2 == 0) and x>50 else True if (x==4) else False)
True if x is even and if x is over 50, or that if it is equal to 4 then print True. Otherwise print False
1) When an object can be modified without reassignment.
2) When an object can use any in-place operators.
What is the output of this?
not(not True and False or True or False) and not True or False or not (False) and True
True!
min = None
for i in L:
if i < min:min = i
L.remove(min)sList = sorted(L)
L.remove(sList[0])
What are *args and **kwargs? What are the rules in writting them? What is the format of the two variables if you want to access them in your function?
What is the output of the following list comprehension?
[[x+y for y in range(x)] for x in range(3,-1,-1)]
[[3, 4, 5], [2, 3], [1], []]