Procedural Programming Flashcards
(25 cards)
What is unit testing used for in software development?
Verifying that individual units of code function correctly. Also helps with:
* early bug detection
* create more easily maintained and refactored code
Snake Case Naming Convention
Used in PEP8 style guide, all letters should be lowercase and words separated by underscores - applies to variable, function & method names.
Why are style guides used?
- Code is easier to maintain and update by different members of a team
- Makes errors more obvious
- Code more easily understood by others
REPL
Read-evaluate-print loop: a computer environment where user inputs and read & evaluation, with results then returned to the user.
What is a virtual environment? Why is it used?
An isolated workspace separate from the system’s global Python installation - used to manage different libraries and interpreters for various projects.
What does if\_\_name\_\_ == "\_\_main\_\_"
do in Python?
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.
What is Test-Driven Development?
A process for developing software where tests are written before the actual code for the software program.
Describe the red, green, refactor cycle.
- First write tests that fail.
- Write the minimum code necessary to pass the tests.
- Improve code readability and design while still passing tests.
What are typehints?
Indicators for the data types of inputs and outputs in functions and class methods.
Why are typehints beneficial?
- Can help catch errors
- Encourage developers to be intentional with function design
- Improve code readability
What is the correct ordering for the following imports - personal libraries, Python standard libraries, installed libraries?
Standard libraries
e.g. import random
Installed libraries
e.g. import pytest
Personal libraries
e.g. import triangle from triangle
What are the primitive data types in Python?
The most basic data structures:
* integer
* float
* string
* bool
What is the purpose of a code-style linting tool?
They automate the process of checking code for potential bugs, syntax errors - ensures code quality and adherence to style guide.
What is a positional argument in Python?
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!)
What is a keyword argument in Python?
An argument which is identifiable by a specific parameter name when passed into a function.
Using the name=value
syntax.
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) The if-else
expression should come before the for
clause in a list comprehension.
Referring to scope:
What is a global variable?
A variable that can accessed anywhere in a program, typically declared outside a function.
Referring to scope:
What is a local variable?
A variable declared inside a function which can only be used within the scope of that function.
Why is changing a variable at global scope within a function generally bad practice?
Especially in larger projects, this can lead to unintentionally overwriting a previously defined variable’s value.
See this page for more details.
Algorithm
A procedure or formula used for for problem-solving or performing a computation.
What is a ‘pure’ function?
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.
Describe ‘pass by value’ in Python.
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.
Describe ‘pass by reference’ in Python.
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.
What is an edge case in the context of TDD?
An unusual scenario that falls outside typical operating conditions for software.
Testing edge cases is checking how software behaves under unexpected/rare conditions.