Functions, Structured Types, Mutability, Sequences, Lists Flashcards

1
Q

What makes good programming?

A
  • amount of functionality
  • introduce functions
  • mechanisms to achieve decomposition and abstraction

–> more code not necessarily better

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

What do you use abstraction for in programming?

How do you achieve it?

A

–> Suppress details

The process of removing details or attributes to focus attention on details of greater importance

Think of a piece of code as a black box and achieve abstraction using functions or docstrings

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

What do you use decomposition for?

A

It involves breaking down a complex problem into smaller modules that are easier to understand. The smaller parts are simpler to work with.
Modules should be:
-self contained
-intended to be reused

Achieve decomposition using functions or classes

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

What are functions?

A

Functions are reusable pieces of code that have to be called in a program (they do not run automatically)

A function has a name and a body and can have parameters, a docstring
A function should return something

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

How do you write a function?

A
def  (): 
body
return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you invoke a function?

A

you call it by writing its name with round backets and parameters if needed
i.e. is_even(3)

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

What is the difference between a formal and a actual parameter?

A

The formal parameter is the one used i ´n the function definition and the actual one is the one that is called with the function when running –> formal parameter gets found to the value of the actual parameter when function is called

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

What happens if you do not define a return statement ?

A

Python automatically returns value None, if no return is given

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

What is the difference between print and return?

A

Return:

  1. Only has a meaning inside a function
  2. only one return executed inside a function
  3. code after return statement not executed
  4. Has a value associated with it and gives it to the function caller

Print

  1. Can be used outside functions
  2. Can execute several print statements within a function
  3. Code inside a function can be executed after a print statement
  4. The value associated to it will only be outputted to the console
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are higher-order functions?

A

Functions that take functions as argument/parameter

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

What is a list?

A

A list is a ordered sequence of information accessible by index

  • -> A list is denoted by square brackets [] and contains elements
  • -> can contain mixed types but usually homogenous i.e. all ints
  • -> lists are mutable (can be changed)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you iterate through a list L?

A

for elem in L:

body

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

How do you access objects (like list instances)?

A

using the dot (.)

list_name.append(something)

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

Can you convert a string s into a list L?

A

Yes, using list(s) –> returns a list with every character from s as an element in L

  • -> use split to split a string on character parameter
  • -> use ““.join to turn a list of characters into a string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Tuple?

A
  1. A tuple is an ordered sequence of elements (can be of mixed type)
  2. immutable - cannot change values
  3. represented with parentheses
  4. (2, “mit”, 3) + (5, 6) = (2, “mit”, 3, 5, 6)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are tuples often used for?

A
  1. Used to return more than one value from a function
    return (q, r)
  2. To swap variable values
    (x, y) = (y, x)
17
Q

What is recursion?

A

Algorithmically: a way to design solutions to problems by
divide-and-conquer

Semantically: a programming technique where a
function
calls itself
–> must have a base case

18
Q

What is a dictionary?

A

They store pairs of data in the form {key : value}
looks up the key –> return value
i.e. grades = {‘Ana’ : ‘B’, ‘John’ : ‘A+’}
grades[‘John’] –> returns ‘A+’

19
Q

Can values in a dictionary have mutable or immutable types?

A

Both!

20
Q

Can Values in dictionaries be duplicates?

A

Yes

21
Q

What properties do dictionary keys have?

A
  1. Keys must be unique

2. Must be of immutable type (int, float, string, tuple, bool)

22
Q

What are the differences of lists and values?

A

Lists:

  • ordered sequence of elements
  • look up elements by an integer value
  • indices have an order
  • Index is an integer

Dict:

  • matches keys to values
  • look up one item by another item
  • no order is guaranteed
  • key can be any immutable type