Procedural Programming Flashcards

(25 cards)

1
Q

What is unit testing used for in software development?

A

Verifying that individual units of code function correctly. Also helps with:
* early bug detection
* create more easily maintained and refactored code

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

Snake Case Naming Convention

A

Used in PEP8 style guide, all letters should be lowercase and words separated by underscores - applies to variable, function & method names.

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

Why are style guides used?

A
  • Code is easier to maintain and update by different members of a team
  • Makes errors more obvious
  • Code more easily understood by others
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

REPL

A

Read-evaluate-print loop: a computer environment where user inputs and read & evaluation, with results then returned to the user.

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

What is a virtual environment? Why is it used?

A

An isolated workspace separate from the system’s global Python installation - used to manage different libraries and interpreters for various projects.

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

What does if\_\_name\_\_ == "\_\_main\_\_" do in Python?

A

Code inside this block only runs if the file is executed as a script, not when it’s imported as a module.

When you import code as a module, Python sets \_\_name\_\_ to the name of the module not \_\_main\_\_ as it does when you run a script.

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

What is Test-Driven Development?

A

A process for developing software where tests are written before the actual code for the software program.

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

Describe the red, green, refactor cycle.

A
  1. First write tests that fail.
  2. Write the minimum code necessary to pass the tests.
  3. Improve code readability and design while still passing tests.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are typehints?

A

Indicators for the data types of inputs and outputs in functions and class methods.

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

Why are typehints beneficial?

A
  • Can help catch errors
  • Encourage developers to be intentional with function design
  • Improve code readability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the correct ordering for the following imports - personal libraries, Python standard libraries, installed libraries?

A

Standard libraries
e.g. import random
Installed libraries
e.g. import pytest
Personal libraries
e.g. import triangle from triangle

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

What are the primitive data types in Python?

A

The most basic data structures:
* integer
* float
* string
* bool

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

What is the purpose of a code-style linting tool?

A

They automate the process of checking code for potential bugs, syntax errors - ensures code quality and adherence to style guide.

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

What is a positional argument in Python?

A

An argument passed to a function where its meaning is determined by its position in the function call.

Short example:
def greet(name, age): print(f"Hello, {name}! You are {age} years old.")

‘John’ is the first positional argument, ‘30’ is the second.
greet("John", 30) # Output: Hello, John! You are 30 years old.

Reversing the order changes the result.
greet(30, "John") # Output: Hello, 30! You are John years old. (Incorrect!)

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

What is a keyword argument in Python?

A

An argument which is identifiable by a specific parameter name when passed into a function.

Using the name=value syntax.

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

Which of the following Python list comprehensions is syntactically correct?
A) ["Even" if x % 2 == 0 else "Odd" for x in range(5)]
B)["Even" for x in range(5) if x % 2 == 0 else "Odd" ]

A

A) The if-else expression should come before the for clause in a list comprehension.

17
Q

Referring to scope:

What is a global variable?

A

A variable that can accessed anywhere in a program, typically declared outside a function.

18
Q

Referring to scope:

What is a local variable?

A

A variable declared inside a function which can only be used within the scope of that function.

19
Q

Why is changing a variable at global scope within a function generally bad practice?

A

Especially in larger projects, this can lead to unintentionally overwriting a previously defined variable’s value.

See this page for more details.

20
Q

Algorithm

A

A procedure or formula used for for problem-solving or performing a computation.

21
Q

What is a ‘pure’ function?

A

A function which:
* always produces same output with the same input.
* has no side effects.

Side effects: a pure function does not alter any external state or interact with anything else - no modifying variables outside scope, no database or API interactions etc.

22
Q

Describe ‘pass by value’ in Python.

A

Occurs when passing immutable or primitive data types into a function - a copy of the data is passed, so any changes to the data will only exist in the scope of the function.

23
Q

Describe ‘pass by reference’ in Python.

A

Occurs when passing mutable data types into a function - a reference of the data is used, so if it is changed inside the function, it will change outside the function too.

Occurs with lists, dictionaries for example.

24
Q

What is an edge case in the context of TDD?

A

An unusual scenario that falls outside typical operating conditions for software.
Testing edge cases is checking how software behaves under unexpected/rare conditions.

25
What are the benefits of TDD?
* Code quality - more maintainable and testable code. * Lowers debugging time. * More intentional design. * Potentially faster development overall. ## Footnote Faster development from reduced debugging time and better maintainability.