What are the three types of whitespace you can have inside a string?
" " (space), "\t" (Tab), "\n" (Newline)
Name one of the functions we have been using for a while now and describe what it's job is.
Options: print(), input(), len(), int(), float(), bool(), str()
Options:
for loop, while loop for indexing, list comprehension
What will the string method .split(",") return if applied to any string?
A list of strings that have been split whenever there is a comma character.
Yes. You would have to index multiple times until you get to the item.
Name one string operation (there are 3 we learned about) and describe what it does.
1. + - concatenates strings
2. * - repeats strings
3. in - check if certain character or pattern of characters is in our string
What are the 3 general steps we said could be applied to any function?
1. Takes 0 or more inputs as parameters
2. Does some operation(s) a.k.a it's specific job
3. Returns an output object (optional)
list = [x, y, z]
tuple = (x, y, z)
What data type will the method .isalpha() return? Why is this the case?
Bool type because it is checking if the string is either all letters from the alphabet or not, and that can either be True or False, nothing else.
Of the following data types, which ones are immutable?
int, float, lists, tuples, str, bool
int, float, tuples, str, bool
What do the three arguments represent when slicing a string using the following syntax: my_string[x:y:z]
my_string[start_index:end_index:stride]
How many arguments/parameters can I have in a function definition? How do I include these in my function definition?
As many as I want. Separate them with commas
What is the main difference between lists and tuples?
lists - mutable
tuples - immutable lists
Name two string methods that we learned about and give an example of how we can use them.
.upper(), .lower(), .isalpha(), .isdigit(), .replace(), ...etc.
What would the following line of code produce?
print(1 + 2 + 3 + "4")
Type Error!
How do string comparisons work? Explain in detail.
Every pair of characters in both strings are compared. If the comparison holds True for that pair, we compare the next pair and so on until the comparison is False or we reach the end of the strings.
What will happen if I run the following code:
def mystery_function(num):
if num > 0:
return num * 3
else:
return -1
mystery_function()
Error!
I am not including the parameter that my definition says I need. How can I fix this?
What would the following code output?
print( ([1, 2, 3] + [4, 5, 6]) * 2)
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
Options:
.append(), .pop(), .remove(), .insert(), .sort(), .index(), .count()
What is the difference between methods and functions? How do I call each one?
my_string.method_name()
function_name(my_string)
What would the following line of code return?
"lunabear ".strip().upper().replace("L", "B").replace("L", "T")
"BUNABEAR"
When and why do I need to use the return keyword in my function definition? Is there every a scenario in which I don't need to return information?
When you want to return information from inside the function back outside the function (remember we don't know what the elf is doing). UNLESS, we are editing a mutable data type (ex: lists). Then, we don't need to return because our the elf's edits are updated automatically.
What is the difference between my_list.remove(2) and my_list.pop(2)
my_list.remove(2) will remove the first instance from left to right of the element of value 2 in my_list.
What is the difference between my_list.sort() and sorted(my_list)?
.sort() - modifies the list itself
sort() - modifies a copy of the list and returns the copy (the original stays untouched)
what can we use the key parameter for in sorted() and .sort(). Give an example.