Midterm2 Flashcards
Do elements of a list have to be the same type?
No
Can you slice a list?
Yes
What happens you you add lists? C = A + B
What about multiplication? C = A * 3
-C will contain all the elements from A and B (in that respective order)
-C contains elements of a n times
What is the error?
list = [#elements]
list[1:3] = 5
print(list)
When replacing values inside the list using slicing, you can only use an iterable, 5 is not an iterable. ==> TypeError
How do you remove elements from list using slicing?
list[start element to remove:right after ele to remove] = []
what does str.strip() do?
it takes away the leading and trailing specified chars (not the ones in between). If none are specified, then whitespace
What is a function?
What is a method?
objects of type string
-A function is a “stand-alone” program that you can call from your
program:
Example:
a = int(‘1234’)
b = round(12.5)
c = get_pseudo()
int and round are built-in functions, get_pseudo is a function that we
created
-A method is a function that “belongs” to an object: We call a method
using the dot notation
object.method_name()
Example:
s = ‘hello’
valid = s.isdecimal() # isdecimal is a method that we can use only on
```def example(x):
x = x * 5
x = True
example(x)
print(x)```
What prints?
5
How do you use doctest?
import doctest doctest.testmethod()
What does testmethod() from doctest do?
Will test your method with examples from docstring where you wrote “»>”.
If something goes wrong, will say as such in output, if the tests pass, it does nothing.
What is a stylistic error?
Stylistic errors — The functionality of your code is not affected. Your code is hard to read
What is a Syntax error?
Syntax errors — problems with code syntax (syntax
being the structure or grammar of language)
What is a runtime error?
- Runtime errors — problems when code is executed (if
there were no syntax errors)
What is a logical error?
- Logical errors — no syntax or runtime errors, but the
program does not produce expected output.
What is the syntax for raising exceptions?
raise <some_exception>(message)</some_exception>