4.1 - Fundamentals of Programming Flashcards
(17 cards)
Why use constants?
- Difficulty to accidentally change value.
- More readable in code (i.e. constant PI is better than the value 3.14…. every time!)
Function to convert between a character and its ASCII code in Python
ord()
Function to convert between an ASCII code and its corresponding character in Python
chr()
DateTime object to a string in Python
Using the datetime library:
datetime.strftime()
String to DateTime object in Python
Using the datetime library:
datetime.strptime()
Why use local variables?
Avoid taking up excess memory when they are not needed!
What is the call stack?
A stack data structure that stores information on the subroutines that have been called.
What info is stored on the call stack?
Return addresses - to ensure control is returned to the correct call.
Parameters.
Local variables - efficient method of storage is on the call stack.
What is recursion?
When a subroutine is defined in terms of itself.
3 characteristics of a recursive subroutine…
- Base case - when it will stop calling itself and begin returning values.
- If the base case is not met, the subroutine must call itself.
- The base case must be met after a finite number of calls.
Example of algorithms that are often implemented recursively
Traversal algorithms
What is encapsulation?
Putting data, along with methods that work on it, together in an object.
What is inheritance?
When an object takes methods/attributes from a parent object. An “is a” relationship.
What is aggregation?
One class is a collection of other classes. A “has a” relationship.
What is composition?
One class is a collection of other classes, but if the container class is destroyed, so will the contained classes.
What is polymorphism?
When a programming language deals with certain objects differently to other objects.
What are the three principles of object oriented programming?
- Encapsulate what varies.
- Favor composition over inheritance.
- Program to interfaces, not implementation.