Computational Thinking Final

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/159

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

160 Terms

1
New cards

What is a function in programming?

A reusable block of code that performs a specific task, using inputs and producing an output.

2
New cards

What principle do functions help adhere to?

The DRY (Don't Repeat Yourself) principle.

3
New cards

What keyword is used to define a function in Python?

def

4
New cards

What is the purpose of the return keyword in a function?

To send data back to the code that called the function.

5
New cards

What is a void function?

A function that performs an action but does not return a value.

6
New cards

What does the main() function represent?

The entry point for a script, typically expecting no arguments and returning no value.

7
New cards

What does the __name__ variable indicate?

It indicates how a script is executed; it equals __main__ when run directly.

8
New cards

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.

9
New cards

What does the calc_total function do?

Calculates the total price including tax based on price, quantity, and tax rate.

10
New cards

What happens if you try to access a local variable outside its function?

It results in a NameError.

11
New cards

What are global variables?

Variables defined outside of any function that can be accessed anywhere in the script.

12
New cards

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.

13
New cards

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.

14
New cards

What is a module in Python?

A Python file containing function definitions and statements that can be reused across different scripts.

15
New cards

How do you import a module in Python?

Using the import statement, e.g., import my_module.

16
New cards

What is the importance of code documentation?

It helps in understanding code functionality and maintaining it over time.

17
New cards

What are comments in Python?

Lines starting with # that explain code logic and are ignored by Python during execution.

18
New cards

What are docstrings?

Triple-quoted strings that describe a function's purpose, parameters, and return values.

19
New cards

What does the return statement do in a function?

It exits the function and sends a value back to the caller.

20
New cards

What is an example of a simple function?

def add(num1, num2): total = num1 + num2; return total

21
New cards

What is the output of the function call add(5, 3)?

8

22
New cards

What happens if a void function does not have a return statement?

It implicitly returns None.

23
New cards

What is the purpose of the update_list function?

To append an item to a list.

24
New cards

What is the significance of variable scope?

It defines the visibility and lifetime of a variable in the code.

25
New cards

What is an example of a function that accepts multiple arguments?

power(base, exp) calculates the power of a base raised to an exponent.

26
New cards

What is the role of the make_book_list() function?

To create a list of books from a CSV file.

27
New cards

What is the purpose of using the help() function in Python?

To provide information about functions using their docstrings.

28
New cards

What are docstrings in Python?

Triple-quoted strings that describe a function's purpose, parameters, and return values.

29
New cards

What is the Google-style docstring format?

A structured format that includes sections for a summary, parameters, and return values.

30
New cards

Why is error handling essential in Python?

It ensures programs fail gracefully, maintaining user trust and data integrity.

31
New cards

What are the three main types of errors in Python?

Syntax Errors, Runtime Errors, and Logic Errors.

32
New cards

What is a Syntax Error?

An error that occurs when the code structure is invalid, preventing execution.

33
New cards

What is a Runtime Error?

An error that occurs during execution, such as TypeError or ZeroDivisionError.

34
New cards

What is a Logic Error?

An error where the code runs but produces incorrect results.

35
New cards

What is the purpose of try/except blocks?

To handle errors gracefully by wrapping code that may fail in a try block.

36
New cards

What is the difference between debugging and testing?

Debugging is reactive, fixing bugs after they occur, while testing is proactive, checking code behavior.

37
New cards

What is Unit Testing?

Testing individual functions for expected behavior to ensure logic correctness.

38
New cards

What is Integration Testing?

Testing how multiple components work together to ensure proper interactions.

39
New cards

What is System Testing?

Evaluating the entire application from end to end to confirm overall functionality.

40
New cards

What are best practices for testing in Python?

Write short, independent tests, use descriptive names, and test both normal and edge cases.

41
New cards

What is the unittest module?

A built-in module in Python for creating and running tests.

42
New cards

How do you create a test case using unittest?

Define a class that inherits from unittest.TestCase.

43
New cards

What is the purpose of assertions in unittest?

To check if a condition is true, such as assertEqual to compare values.

44
New cards

What is a ValueError?

An error raised when a function receives an argument of the right type but inappropriate value.

45
New cards

What is a TypeError?

An error raised when an operation is applied to an object of inappropriate type.

46
New cards

What is a ZeroDivisionError?

An error raised when attempting to divide by zero.

47
New cards

What is defensive coding?

A practice of writing code that anticipates and handles potential errors.

48
New cards

What are lambda functions?

Small, unnamed functions defined using the lambda keyword, ideal for short operations.

49
New cards

What does the map() function do?

Applies a function to each item in an iterable, returning an iterator of results.

50
New cards

What does the filter() function do?

Filters items in an iterable based on a condition, returning only those that meet the criteria.

51
New cards

What does the sorted() function do?

Sorts elements of an iterable based on a key.

52
New cards

What do max() and min() functions do?

Find maximum or minimum values in datasets.

53
New cards

What is the purpose of raising custom errors?

To enforce business rules and ensure valid input.

54
New cards

What is the EOW10 plan focused on?

Implementing business rules for shipping calculations, including free shipping and discounts.

55
New cards

What function is used to find maximum values in datasets?

max()

56
New cards

What function is used to find minimum values in datasets?

min()

57
New cards

What are list comprehensions used for in Python?

To generate or transform sequences compactly, replacing multi-line loops.

58
New cards

How do you create a list of squared sales using list comprehension?

squared_sales = [sale**2 for sale in sales]

59
New cards

How can you apply sales tax to a list of prices using list comprehension?

taxed = [price * 1.10 for price in prices]

60
New cards

What is the purpose of filtering data in list comprehensions?

To create new lists based on specific conditions.

61
New cards

How do you filter high sales using list comprehension?

high_sales = [sale for sale in sales if sale > 250]

62
New cards

What is a dictionary comprehension?

A method to create new dictionaries from existing ones using a compact syntax.

63
New cards

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()}

64
New cards

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}

65
New cards

What function is used to pair lists into key-value dictionaries?

zip()

66
New cards

How do you create a price dictionary using zip()?

price_dict = {item: price for item, price in zip(items, prices)}

67
New cards

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()}

68
New cards

What is the result of applying a 5% markup to old prices?

Updated prices for each item in the dictionary.

69
New cards

What is the purpose of filtering data in dictionaries?

To create a new dictionary that only includes items meeting specific criteria.

70
New cards

How do you create a premium dictionary from old prices?

premium = {k: v for k, v in old_prices.items() if v > 1.00}

71
New cards

What is the datetime module used for in Python?

For manipulating dates and times.

72
New cards

What does the datetime.now() function do?

Retrieves the current date and time.

73
New cards

How can you format dates as strings in Python?

Using the .strftime() method.

74
New cards

What is UTC?

Coordinated Universal Time, the global time standard.

75
New cards

How do time zones relate to UTC?

Time zones are defined as offsets from UTC.

76
New cards

What is the significance of the ZoneInfo module?

It helps manage time zone conversions accurately.

77
New cards

Why is logging important in Python applications?

For troubleshooting, system maintenance, and auditing.

78
New cards

What are the logging levels in Python?

DEBUG, INFO, WARNING, ERROR, CRITICAL.

79
New cards

What does the logging.basicConfig() function do?

Sets up a basic logging configuration.

80
New cards

What is refactoring in programming?

The process of improving existing code without changing its external behavior.

81
New cards

What is a common practice in refactoring?

Simplifying complex functions or renaming variables for clarity.

82
New cards

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.

83
New cards

When should refactoring be considered?

Refactoring should be considered when variable names are vague, code is repetitive, or functions are overly complex.

84
New cards

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.

85
New cards

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.

86
New cards

What is a lambda function?

A lambda function is a small, anonymous function defined with the lambda keyword, allowing for quick function creation.

87
New cards

What is the syntax for a lambda function?

The syntax is lambda arguments: expression.

88
New cards

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)).

89
New cards

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.

90
New cards

What is the syntax for a list comprehension?

The syntax is [expression for item in iterable if condition].

91
New cards

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.

92
New cards

How do you get the current time using the datetime module?

Use: now = datetime.datetime.now().

93
New cards

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.

94
New cards

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).

95
New cards

How can you log an informational message using the logging module?

Use: logger.info('Application started').

96
New cards

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.

97
New cards

What does the strftime() function do?

The strftime() function formats a datetime object back into a string.

98
New cards

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).

99
New cards

What is the benefit of using comprehensions in Python?

Comprehensions enhance code readability and performance, making them a preferred choice for many developers.

100
New cards

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.