Programming in Python

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/109

flashcard set

Earn XP

Description and Tags

1 Semester Module 6 ECTS

110 Terms

1
New cards

What other names are used for the von Neumann architecture?

Von Neumann model or Princeton architecture

2
New cards

Which three components are drawn inside the CPU in the von Neumann model?

Control Unit, Memory, Arithmetic-Logic-Unit (ALU)

3
New cards

What is the role of the Control Unit?

It orchestrates the program execution

4
New cards

What is the role of the Arithmetic-Logic-Unit (ALU)?

The "brain": it performs the arithmetic and logic operations

5
New cards

What is the role of the memory in the von Neumann model?

It stores the data and the program, i.e. the state of our computation

6
New cards

Name examples of input devices.

External drive, sensors, mouse, keyboard

7
New cards

Name examples of output devices.

Screen, external drive, printer

8
New cards

What is external memory in the von Neumann model (examples)?

Memory outside the CPU, e.g. hard disk and RAM

9
New cards

Which network technologies are named as connecting the computer to a network?

Wifi, Ethernet, 3G/4G/5G/6G

10
New cards
<p>Describe the von Neumann architecture diagram: how are the components connected?</p>

Describe the von Neumann architecture diagram: how are the components connected?

Input devices feed the CPU (control unit, memory, ALU); the CPU sends results to output devices; external memory (hard disk, RAM) is attached to the CPU; a network connection links the computer to other systems

11
New cards

How is an algorithm defined?

Any well-defined computational procedure that takes some value or set of values as input and produces some value or set of values as output

12
New cards

What is a program?

A series of instructions

13
New cards

Which typical operations/instructions do programs contain?

Manipulating data (read/store in memory); arithmetic (+, *, /, -, …); control flow of execution (e.g. if … else)

14
New cards

How are programs stored, and what is that called?

As data in memory; software

15
New cards

How are programs related to algorithms?

A program is the language-specific representation of an algorithm

16
New cards

What is the general structure of an algorithm?

input → instructions → output

17
New cards

Which steps lead from an algorithm to program execution?

Algorithm → (programming) → program/code in a programming language → (translation) → executable program in machine language → (interpretation by CPU) → program execution

18
New cards

What is the difference between code and an executable program?

Code is written in a programming language (e.g. Python); the executable program is in machine language

19
New cards

Why does a computer need a compiler or interpreter?

To understand our algorithm: they translate the readable source code into something the computer can execute

20
New cards

How does a compiler work (example language)?

It scans the entire program first, translates it completely and then executes the translated program, e.g. C/C++

21
New cards

Which stages does compilation go through?

Source language (readable source code) → optional intermediate language → target language (often machine language)

22
New cards

How does an interpreter work (example language)?

It scans and translates one statement at a time and directly executes each translated statement, e.g. Python

23
New cards

Compiler vs. interpreter: key difference?

Compiler translates the whole program before execution; interpreter translates and executes statement by statement

24
New cards

What does a computer system consist of?

Hardware, system programs, application programs

25
New cards

How is an operating system an "extended machine"?

It is an abstraction that hides the messy details of access to the hardware (hardware drivers)

26
New cards

How is an operating system a "resource manager"?

It virtualizes resources and assigns each program time with the hardware, e.g. multi-tasking

27
New cards

Python data type int

Integers: whole numbers, e.g. 3, 300, 200

28
New cards

Python data type float

Floating point: numbers with a decimal point, e.g. 2.3, 4.6, 100.0

29
New cards

Python data type str

Strings: ordered sequence of characters, e.g. "hello", 'Sammy', "2000"

30
New cards

Python data type list

Lists: ordered sequence of objects, e.g. [10, "hello", 200.3]

31
New cards

Python data type dict

Dictionaries: unordered key:value pairs, e.g. {"mykey": "value", "name": "Frankie"}

32
New cards

Python data type tuple (abbreviated "tup" on the slide)

Ordered immutable sequence of objects, e.g. (10, "hello", 200.3)

33
New cards

Python data type set

Unordered collection of unique objects, e.g. {"a", "b"}

34
New cards

Python data type bool

Booleans: logical value True or False

35
New cards

Which of list, tuple, dict and set are ordered, and which are not?

Ordered: list and tuple; unordered: dict and set

36
New cards

What is the difference between a list and a tuple?

Both are ordered sequences of objects, but a tuple is immutable

37
New cards

What does the data type determine, besides usage rules?

The amount of memory

38
New cards

x = 2, y = 5: what are x + y, x - y and x * y?

7, -3 and 10

39
New cards

x = 2, y = 5: what is x / y?

0.4

40
New cards

x = 2, y = 5: what is x % y (modulo)?

2 (modulo gives the remainder of the division)

41
New cards

x = 2, y = 5: what is x ** y (exponentiation)?

32 (2 to the power of 5)

42
New cards

x = 2, y = 5: what is x // y (floor division)?

0 (the division result rounded down to a whole number)

43
New cards

What is the shortcut for x = x + 1, and for which operators does it work?

x += 1; it works for all arithmetic operators (e.g. y -= 1)

44
New cards

x = 3, y = 2: what are x and y after x += 1 and y -= 1?

x = 4, y = 1

45
New cards

Which relational operators exist, and what type of result do they give?

==, !=,

46
New cards

i = 5, j = 7: what are i < j and i >= j?

True and False

47
New cards

Which logical operators exist?

and, or, not

48
New cards

b1 = True, b2 = False: what are b1 and b2, b1 or b2, b1 and not b2?

False, True, True

49
New cards

a = 5, then c = a: what is c?

5 (value assignment copies the value of a to c)

50
New cards

a = 'abc', b = "def": what is a + b (string concatenation)?

'abcdef'

51
New cards

a = 'abc': what is 3 * a (string repetition)?

'abcabcabc'

52
New cards

How do you convert the number 123 to a string?

str(123) gives '123'

53
New cards

a = 'abc', b = "def": what is a[1] + b[2] (extract a char)?

'bf' (indexing starts at 0)

54
New cards

What is the syntax for extracting a substring, and what is a[0::2] for a = 'abc'?

[start:end:step]; a[0::2] gives 'ac'

55
New cards

b = "def": what is b[::-1]?

'fed' (step -1 reverses the string)

56
New cards

Which three control structures are shown for Python?

Conditions (if … else), for loops, while loops

57
New cards

How does the flow of if … else work?

The condition is evaluated; if true, statement1 runs, if false, statement2 runs; then the next statement follows

58
New cards

How does the flow of a for loop work?

Evaluate the loop expression; if the loop condition is met, execute the loop body and change the loop counter, then check the condition again; if not met, continue the program

59
New cards

How does the flow of a while loop work?

Check whether the loop condition is met; if true, execute the loop body and check again; if false, continue the program

60
New cards

How does the for-loop version of factorial(number) work?

result = 1; for i in range(1, number + 1): result = result * i; return result

61
New cards

How does the while-loop version of factorial(number) work?

Start with result = 1 and i = 1; while i <= number: result = result * i and i = i + 1; then return result

62
New cards

Why does the factorial for loop use range(1, number + 1)?

range excludes its end value, so number + 1 is needed to include number itself

63
New cards

In which four ways can an algorithm be formulated?

Natural language, pseudocode, flowcharts, programming language (here: Python)

64
New cards

Natural language and pseudocode example for the sum of 1 to n?

Natural language: "Sum up all values from 1 to n"; pseudocode: sum(1..n) = 1 + (1+1) + (1+2) + … + (n-2) + (n-1) + n

65
New cards

How is the sum algorithm expressed as a Python function?

def sum(n): set sum = 0 and i = 1; for x in range(1, n+1): sum = sum + i and i = i + 1; return sum

66
New cards
<p>Describe the flowchart of the sum example.</p>

Describe the flowchart of the sum example.

Start → sum = 0, i = 1 → decision "i == n": if no, sum = sum + i and i = i + 1, then back to the decision; otherwise return(sum)

67
New cards

Programming = ?

Translation of an algorithm into running software

68
New cards

Which steps lead from Problem to Algorithm and from Algorithm to Code?

Problem → Algorithm: analysis / modelling; Algorithm → Code: programming / coding

69
New cards

Which steps lead from Code to Executable Program and to "Problem solved?"

Code → Executable Program: translation / compilation; Executable Program → Problem solved?: execution / interpretation

70
New cards

Which guiding question belongs to each stage: Algorithm, Executable Program, Problem solved?

Algorithm: How to solve the problem? Executable Program: How does the executable code look like? Problem solved?: Does it solve the problem?

71
New cards
<p>Describe the summary diagram of programming.</p>

Describe the summary diagram of programming.

Problem → Algorithm → Code → Executable Program → "Problem solved?", with the transitions analysis/modelling, programming/coding, translation/compilation and execution/interpretation

72
New cards
What are the two parts of a for loop?
Loop head and loop body
73
New cards
What is the loop head of a for loop?
A control expression consisting of three parameters: initialization expression, loop test expression and iterator
74
New cards
What is the initialization expression of a for loop?
It defines the initial condition for the loop
75
New cards
What is the loop test expression of a for loop?
The expression that is tested for every execution of the loop body
76
New cards
What is the iterator of a for loop?
It is executed at the end of each loop iteration and changes the variable which is tested
77
New cards
What is the general form of a for loop in Python?
for x in range: statement(s), where the statement(s) form the loop body
78
New cards
Describe the flow chart of a for loop.
Describe the flow chart of a for loop.
Evaluate loop expression → "loop condition met?"; if False: continue program; if True: execute loop body → change loop counter → back to the loop head
79
New cards
How does the iterative factorial function with a for loop work?
def factorial(number): result = 1; for i in range(1, number + 1): result = result * i; return result
80
New cards
Why does the factorial for loop use range(1, number + 1)?
range excludes its end value, so number + 1 is needed to include number itself
81
New cards
What does a while loop do?
It executes its body as long as the loop condition is met
82
New cards
When is the expression of a while loop tested, and what happens if it is False right at the start?
It is tested at the beginning of the loop; if it evaluates to False, the loop body is not executed
83
New cards
What happens once the loop condition of a while loop is violated?
Execution continues with the statement subsequent to the loop
84
New cards
What is the general form of a while loop in Python?
while expression: statement(s)
85
New cards
How does the iterative factorial function with a while loop work?
def factorial(number): start with result = 1 and i = 1; while i <= number: result = result * i and i = i + 1; return result
86
New cards
Transforming a for loop into a while loop (and vice versa): what are steps 1 and 2?
1. Check what the loop's functionality is; 2. Check how often the loop iterates
87
New cards
Transforming a for loop into a while loop (and vice versa): what are steps 3 and 4?
3. Write the loop head of the new loop; 4. Copy the loop body and adjust the loop counter's variable
88
New cards
How is the factorial for loop transformed into a while loop?
Initialize i = 1 before the loop; new loop head: while i <= number; copy the body result = result * i and add i = i + 1 as the loop counter adjustment
89
New cards
How do you write the for loop "for i in range(0, 10, 2): print(i)" as a while loop?
i = 0; while i < 10: print(i); i += 2 (range(0, 10, 2) yields 0, 2, 4, 6, 8)
90
New cards
What do the iterative and the recursive factorial function have in common?
The same signature (def factorial(number)), so the recursive version can be used just like the iterative one
91
New cards
When is an algorithm recursive?
If it calls itself
92
New cards
How is n! (factorial of n) computed?
n! = 1 * 2 * 3 * 4 * ... * n
93
New cards
What is the formal (recursive) definition of the factorial?
1! = 1; n! = n * (n-1)! for n > 1
94
New cards
How is the computation of n! reduced in the recursive definition?
n! is reduced to the computation of (n-1)!, which in turn is reduced to (n-2)!, and so on
95
New cards
Why does a recursive function require a termination condition?
Without it, the function would be an infinite loop
96
New cards
How does the recursive factorial function work?
def factorial(number): if number <= 1: return 1 (base case); else: return number * factorial(number - 1) (recursive call of the function itself)
97
New cards
What is the base case of the recursive factorial function?
number <= 1: the function returns 1
98
New cards
How does the recursive computation of factorial(5) unfold step by step?
factorial(5) → 5 * factorial(4) → 5 * 4 * factorial(3) → 5 * 4 * 3 * factorial(2) → 5 * 4 * 3 * 2 * factorial(1)
99
New cards
Which rule is applied in each expansion step of factorial(5), and what is checked before?
Before each step, number <= 1 evaluates to False (5, 4, 3, 2); then factorial(n) = n * factorial(n-1) is applied
100
New cards
How does the recursion of factorial(5) end?
1 <= 1 evaluates to True, so factorial(1) = 1; the result is 5 * 4 * 3 * 2 * 1 = 120 = factorial(5)