Midterm2 Flashcards

1
Q

Do elements of a list have to be the same type?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you slice a list?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens you you add lists? C = A + B
What about multiplication? C = A * 3

A

-C will contain all the elements from A and B (in that respective order)
-C contains elements of a n times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the error?

list = [#elements]
list[1:3] = 5

print(list)

A

When replacing values inside the list using slicing, you can only use an iterable, 5 is not an iterable. ==> TypeError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you remove elements from list using slicing?

A

list[start element to remove:right after ele to remove] = []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what does str.strip() do?

A

it takes away the leading and trailing specified chars (not the ones in between). If none are specified, then whitespace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a function?
What is a method?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

```def example(x):
x = x * 5

x = True
example(x)
print(x)```

What prints?

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you use doctest?

A
import doctest
doctest.testmethod()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does testmethod() from doctest do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a stylistic error?

A

Stylistic errors — The functionality of your code is not affected. Your code is hard to read

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Syntax error?

A

Syntax errors — problems with code syntax (syntax
being the structure or grammar of language)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a runtime error?

A
  • Runtime errors — problems when code is executed (if
    there were no syntax errors)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a logical error?

A
  • Logical errors — no syntax or runtime errors, but the
    program does not produce expected output.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the syntax for raising exceptions?

A

raise <some_exception>(message)</some_exception>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Do the keys in dictionnaries have to be of the same type?

A

No

17
Q

What will the following code do?
~~~
heights = {‘Sasha’ : 182}
heights[‘Rohan’] = 168
heights[‘Kate’] = 177
~~~
What will be printed?
~~~
print(heights)
~~~

A

1)
Creating ‘heights’ dictionnary and then add two entries to it.
2)
It will print:
{‘Sasha’ : 182, ‘Rohan’ : 168, ‘Kate’ : 177}

18
Q

How do you remove items from a dictionnary?

A

Using ‘del’:
»> del my_dict[key]

19
Q

What kind of error is raised when trying to add/delete (access) an item with a non-existent key?

A

KeyError

20
Q

When are lists prefered over dictionnaries?

A

-When you need an ordered list of something
-An element might appear more than once
-Whenever order is important

21
Q

When are dictionaries preffered to lists?

A

-Have an unordered
collection, and you would like to ‘index’ on something
other than non-negative integers
-Very useful for
counting elements

22
Q
A