1/159
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is a function in programming?
A reusable block of code that performs a specific task, using inputs and producing an output.
What principle do functions help adhere to?
The DRY (Don't Repeat Yourself) principle.
What keyword is used to define a function in Python?
def
What is the purpose of the return keyword in a function?
To send data back to the code that called the function.
What is a void function?
A function that performs an action but does not return a value.
What does the main() function represent?
The entry point for a script, typically expecting no arguments and returning no value.
What does the __name__ variable indicate?
It indicates how a script is executed; it equals __main__ when run directly.
What is the purpose of default argument values in functions?
To allow a function to be called with fewer arguments than defined, using a preset value if none is provided.
What does the calc_total function do?
Calculates the total price including tax based on price, quantity, and tax rate.
What happens if you try to access a local variable outside its function?
It results in a NameError.
What are global variables?
Variables defined outside of any function that can be accessed anywhere in the script.
What is the effect of a local variable shadowing a global variable?
The local variable will take precedence over the global variable with the same name.
What is the purpose of the if __name__ == '__main__': construct?
To allow a script to be run directly or imported as a module without executing its main code.
What is a module in Python?
A Python file containing function definitions and statements that can be reused across different scripts.
How do you import a module in Python?
Using the import statement, e.g., import my_module.
What is the importance of code documentation?
It helps in understanding code functionality and maintaining it over time.
What are comments in Python?
Lines starting with # that explain code logic and are ignored by Python during execution.
What are docstrings?
Triple-quoted strings that describe a function's purpose, parameters, and return values.
What does the return statement do in a function?
It exits the function and sends a value back to the caller.
What is an example of a simple function?
def add(num1, num2): total = num1 + num2; return total
What is the output of the function call add(5, 3)?
8
What happens if a void function does not have a return statement?
It implicitly returns None.
What is the purpose of the update_list function?
To append an item to a list.
What is the significance of variable scope?
It defines the visibility and lifetime of a variable in the code.
What is an example of a function that accepts multiple arguments?
power(base, exp) calculates the power of a base raised to an exponent.
What is the role of the make_book_list() function?
To create a list of books from a CSV file.
What is the purpose of using the help() function in Python?
To provide information about functions using their docstrings.
What are docstrings in Python?
Triple-quoted strings that describe a function's purpose, parameters, and return values.
What is the Google-style docstring format?
A structured format that includes sections for a summary, parameters, and return values.
Why is error handling essential in Python?
It ensures programs fail gracefully, maintaining user trust and data integrity.
What are the three main types of errors in Python?
Syntax Errors, Runtime Errors, and Logic Errors.
What is a Syntax Error?
An error that occurs when the code structure is invalid, preventing execution.
What is a Runtime Error?
An error that occurs during execution, such as TypeError or ZeroDivisionError.
What is a Logic Error?
An error where the code runs but produces incorrect results.
What is the purpose of try/except blocks?
To handle errors gracefully by wrapping code that may fail in a try block.
What is the difference between debugging and testing?
Debugging is reactive, fixing bugs after they occur, while testing is proactive, checking code behavior.
What is Unit Testing?
Testing individual functions for expected behavior to ensure logic correctness.
What is Integration Testing?
Testing how multiple components work together to ensure proper interactions.
What is System Testing?
Evaluating the entire application from end to end to confirm overall functionality.
What are best practices for testing in Python?
Write short, independent tests, use descriptive names, and test both normal and edge cases.
What is the unittest module?
A built-in module in Python for creating and running tests.
How do you create a test case using unittest?
Define a class that inherits from unittest.TestCase.
What is the purpose of assertions in unittest?
To check if a condition is true, such as assertEqual to compare values.
What is a ValueError?
An error raised when a function receives an argument of the right type but inappropriate value.
What is a TypeError?
An error raised when an operation is applied to an object of inappropriate type.
What is a ZeroDivisionError?
An error raised when attempting to divide by zero.
What is defensive coding?
A practice of writing code that anticipates and handles potential errors.
What are lambda functions?
Small, unnamed functions defined using the lambda keyword, ideal for short operations.
What does the map() function do?
Applies a function to each item in an iterable, returning an iterator of results.
What does the filter() function do?
Filters items in an iterable based on a condition, returning only those that meet the criteria.
What does the sorted() function do?
Sorts elements of an iterable based on a key.
What do max() and min() functions do?
Find maximum or minimum values in datasets.
What is the purpose of raising custom errors?
To enforce business rules and ensure valid input.
What is the EOW10 plan focused on?
Implementing business rules for shipping calculations, including free shipping and discounts.
What function is used to find maximum values in datasets?
max()
What function is used to find minimum values in datasets?
min()
What are list comprehensions used for in Python?
To generate or transform sequences compactly, replacing multi-line loops.
How do you create a list of squared sales using list comprehension?
squared_sales = [sale**2 for sale in sales]
How can you apply sales tax to a list of prices using list comprehension?
taxed = [price * 1.10 for price in prices]
What is the purpose of filtering data in list comprehensions?
To create new lists based on specific conditions.
How do you filter high sales using list comprehension?
high_sales = [sale for sale in sales if sale > 250]
What is a dictionary comprehension?
A method to create new dictionaries from existing ones using a compact syntax.
How can you convert prices from USD to EUR using dictionary comprehension?
prices_eur = {item: round(price * 0.9, 2) for item, price in prices_usd.items()}
How do you filter items in a dictionary comprehension?
By applying conditions, e.g., low_stock = {item: qty for item, qty in inventory.items() if qty < 10}
What function is used to pair lists into key-value dictionaries?
zip()
How do you create a price dictionary using zip()?
price_dict = {item: price for item, price in zip(items, prices)}
How can you apply a markup to a dictionary of prices?
Using dictionary comprehension: new_prices = {k: v * 1.05 for k, v in old_prices.items()}
What is the result of applying a 5% markup to old prices?
Updated prices for each item in the dictionary.
What is the purpose of filtering data in dictionaries?
To create a new dictionary that only includes items meeting specific criteria.
How do you create a premium dictionary from old prices?
premium = {k: v for k, v in old_prices.items() if v > 1.00}
What is the datetime module used for in Python?
For manipulating dates and times.
What does the datetime.now() function do?
Retrieves the current date and time.
How can you format dates as strings in Python?
Using the .strftime() method.
What is UTC?
Coordinated Universal Time, the global time standard.
How do time zones relate to UTC?
Time zones are defined as offsets from UTC.
What is the significance of the ZoneInfo module?
It helps manage time zone conversions accurately.
Why is logging important in Python applications?
For troubleshooting, system maintenance, and auditing.
What are the logging levels in Python?
DEBUG, INFO, WARNING, ERROR, CRITICAL.
What does the logging.basicConfig() function do?
Sets up a basic logging configuration.
What is refactoring in programming?
The process of improving existing code without changing its external behavior.
What is a common practice in refactoring?
Simplifying complex functions or renaming variables for clarity.
What does D.R.Y. stand for in programming?
D.R.Y. stands for 'Don't Repeat Yourself', emphasizing the importance of avoiding code duplication.
When should refactoring be considered?
Refactoring should be considered when variable names are vague, code is repetitive, or functions are overly complex.
What is the purpose of the filter() function in Python?
The filter() function tests each item with a function and retains those that return True.
How does the map() function work in Python?
The map() function applies a specified function to every item in an iterable, returning a map object that can be converted to a list.
What is a lambda function?
A lambda function is a small, anonymous function defined with the lambda keyword, allowing for quick function creation.
What is the syntax for a lambda function?
The syntax is lambda arguments: expression.
How can you filter employees with sales above 1,000 using a lambda function?
Use: high_sales = list(filter(lambda emp: emp.sales > 1000, employees)).
What is the purpose of list comprehensions in Python?
List comprehensions provide a concise way to create lists, often more readable than using map() or for loops.
What is the syntax for a list comprehension?
The syntax is [expression for item in iterable if condition].
What does the datetime module in Python do?
The datetime module is essential for handling dates and times, allowing for parsing, formatting, and date arithmetic.
How do you get the current time using the datetime module?
Use: now = datetime.datetime.now().
What is the logging module used for in Python?
The logging module is used to track events during program execution, providing a robust alternative to print() statements.
What are the key components of the logging module?
Key components include Logger (to log messages), Handler (to determine where messages go), and Formatter (to format log messages).
How can you log an informational message using the logging module?
Use: logger.info('Application started').
What is the purpose of the strptime() function in the datetime module?
The strptime() function parses a date string into a datetime object using a matching format string.
What does the strftime() function do?
The strftime() function formats a datetime object back into a string.
How can you perform date arithmetic using the datetime module?
Use datetime.timedelta to represent durations and perform calculations, e.g., next_week = now + datetime.timedelta(days=7).
What is the benefit of using comprehensions in Python?
Comprehensions enhance code readability and performance, making them a preferred choice for many developers.
What is an example of using a lambda function with the sorted() function?
Use: sorted(grades, key=lambda student: student[1], reverse=True) to sort by grades.