Functions, Structured Types, Mutability, Sequences, Lists Flashcards
What makes good programming?
- amount of functionality
- introduce functions
- mechanisms to achieve decomposition and abstraction
–> more code not necessarily better
What do you use abstraction for in programming?
How do you achieve it?
–> 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
What do you use decomposition for?
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
What are functions?
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 do you write a function?
def (): body return
How do you invoke a function?
you call it by writing its name with round backets and parameters if needed
i.e. is_even(3)
What is the difference between a formal and a actual parameter?
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
What happens if you do not define a return statement ?
Python automatically returns value None, if no return is given
What is the difference between print and return?
Return:
- Only has a meaning inside a function
- only one return executed inside a function
- code after return statement not executed
- Has a value associated with it and gives it to the function caller
- Can be used outside functions
- Can execute several print statements within a function
- Code inside a function can be executed after a print statement
- The value associated to it will only be outputted to the console
What are higher-order functions?
Functions that take functions as argument/parameter
What is a list?
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 do you iterate through a list L?
for elem in L:
body
How do you access objects (like list instances)?
using the dot (.)
list_name.append(something)
Can you convert a string s into a list L?
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
What is a Tuple?
- A tuple is an ordered sequence of elements (can be of mixed type)
- immutable - cannot change values
- represented with parentheses
- (2, “mit”, 3) + (5, 6) = (2, “mit”, 3, 5, 6)