1. Home
  2. Explore
  3. Computer Science
  4. Python Programming Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python Programming Basics

  • 127Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Python Setup

What practical goals can learning Python help someone achieve, and what qualities make Python appealing to developers?

127 questions

  1. 1

    What practical goals can learning Python help someone achieve, and what qualities make Python appealing to developers?

    Model answer

    Python can support job-seeking, automation, and writing scripts or programs. It is described as powerful, easy to use, and relatively beginner-friendly because it requires comparatively little syntax.

    Explanation

    The material presents Python as popular for employment and practical programming, while emphasizing its accessibility and power.

  2. 2

    How does the course characterize Python 2 and Python 3 in terms of support and future use?

    Model answer

    Python 2 is described as a legacy version that is no longer officially maintained or supported, whereas Python 3 is the newer version being actively maintained.

    Explanation

    The course chooses Python 3 because it is the version being maintained going forward.

  3. 3

    What advantage of Python 2 does the course mention, and what drawback does it emphasize?

    Model answer

    Python 2 has more existing libraries and code because it has been around longer, but it is no longer officially supported and may lose users over time.

    Explanation

    The material contrasts Python 2's larger existing code base with Python 3's ongoing support.

  4. 4

    How transferable are skills learned in Python 3 to Python 2, according to the course?

    Model answer

    The differences are described as relatively small, involving some syntax and other details, so someone who learns Python 3 can move on to coding in Python 2.

    Explanation

    The course recommends Python 3 for beginners but says the learning is transferable.

  5. 5

    What is an IDE, and what does it provide beyond a basic text editor?

    Model answer

    An IDE, or integrated development environment, is a specialized environment for writing, running, and executing code. It can also point out errors and help guide debugging.

    Explanation

    The material distinguishes IDEs from general-purpose editors such as Notepad or TextEdit.

  6. 6

    Why does the course use PyCharm, and what distinction does it make between its editions?

    Model answer

    PyCharm is presented as a popular IDE for Python. Its Professional edition is paid, while its Community edition is free and open source.

    Explanation

    The course selects the Community edition as sufficient to get started.

  7. 7

    When setting up a PyCharm project for this course, which Python interpreter should be selected, and why?

    Model answer

    Select Python 3 as the interpreter so the project runs with the version used in the lessons.

    Explanation

    The material warns that selecting another interpreter, such as a default Python 2 installation, may prevent the learner from following along fully.

  8. 8

    What is the purpose of creating a Python file inside a project?

    Model answer

    A Python file holds the Python code for a program, which can then be run through the IDE.

    Explanation

    The course demonstrates creating a Python file in a project and executing its code.

  9. 9

    In programming, what is an instruction, and how does a computer use a program's instructions?

    Model answer

    An instruction is an operation specified in code. A computer executes the instructions given to it, and more complex instructions can enable more complex tasks.

    Explanation

    The material describes programming as giving a computer a set of instructions.

  10. 10

    What does Python's print statement do, and where does its output appear in the demonstrated IDE?

    Model answer

    It displays the value or text specified in the statement. In the demonstrated IDE, the output appears in the console.

    Explanation

    The course uses print to display text and a drawn text shape in the console.

  11. 11

    How does Python execute ordinary lines of code, and why can changing their order change a program's output?

    Model answer

    Python executes the lines in order. Changing their order changes which instructions run first and can therefore change the output.

    Explanation

    The triangle example changes when a print line is moved earlier in the file.

  12. 12

    How can several print statements be used to draw a simple shape?

    Model answer

    Print a different row of text on each line, positioning characters with spaces and symbols to form the desired shape.

    Explanation

    The course illustrates this by printing rows of slashes, vertical bars, and underscores to approximate a triangle.

  13. 13

    What is a variable, and why is it useful for managing program data?

    Model answer

    A variable is a named container for a data value. It makes data easier to manage and reuse than manually rewriting the value wherever it is needed.

    Explanation

    The story example stores a character's name and age in variables so changes can be made in one place.

  14. 14

    How is a variable assigned a value in Python, and what does reassignment allow a program to do?

    Model answer

    Use the variable name, an equals sign, and a value. Reassignment lets the program change the value stored in that variable.

    Explanation

    The course changes a character's name and age by assigning new values to their variables.

  15. 15

    What naming convention does the course recommend for separating words in a Python variable name?

    Model answer

    Separate words with underscores, as in character_name.

    Explanation

    The course uses underscores in names such as character_name and character_age.

  16. 16

    What three basic kinds of values does the course identify as common in beginner Python programs?

    Model answer

    Strings, numbers, and Boolean values.

    Explanation

    The material identifies text, numeric values, and true-or-false values as common basic data types.

  17. 17

    What is a Boolean value, and what are its two possible values in Python?

    Model answer

    A Boolean represents a true-or-false value. Its Python values are True and False.

    Explanation

    The course uses a Boolean variable to represent whether someone is male or on probation.

  18. 18

    How do strings differ from numeric values when written directly in Python code?

    Model answer

    Strings are text enclosed in quotation marks; numbers are written without quotation marks.

    Explanation

    The course stores a name in quotation marks and stores ages or other numeric values without them.

  19. 19

    How can an escape character be used to put a new line or a quotation mark inside a string?

    Model answer

    Use a backslash followed by n to insert a new line, or a backslash followed by a quotation mark to include that quotation mark literally.

    Explanation

    The backslash acts as an escape character, telling Python how to interpret the following character.

  20. 20

    How can a backslash itself be represented in a string?

    Model answer

    Use an escaped backslash so Python treats it as a literal backslash.

    Explanation

    The course demonstrates using the escape character to print a normal backslash.

  21. 21

    What is string concatenation?

    Model answer

    String concatenation is joining strings together, such as appending one string to another with the + operator.

    Explanation

    The course joins a phrase with another string to produce a longer phrase.

  22. 22

    What do the string methods lower() and upper() do?

    Model answer

    lower() converts a string to lowercase, while upper() converts it to uppercase.

    Explanation

    The course applies these methods to change the capitalization of an entire phrase.

  23. 23

    What Boolean result does isupper() provide?

    Model answer

    It returns True if the string is entirely uppercase and False otherwise.

    Explanation

    The course checks capitalization and shows that converting a string to uppercase before checking makes the check true.

  24. 24

    How can string methods be chained, and what happens when upper() is followed by isupper()?

    Model answer

    Call one method and then call another on its result. upper() first converts the string to uppercase, so isupper() then returns True.

    Explanation

    The course demonstrates using the result of one string method as the input to the next.

  25. 25

    What does the len() function return when given a string?

    Model answer

    It returns the number of characters in the string.

    Explanation

    The course uses len() to count the characters in a phrase.

  26. 26

    How do you access an individual character in a string, and what indexing convention does Python use?

    Model answer

    Use square brackets with the character's index. Python uses zero-based indexing, so the first character is at index 0.

    Explanation

    The course accesses the first character with index zero and explains that each following character's index increases by one.

  27. 27

    What does a string's index() method return, and what happens if the requested text is absent?

    Model answer

    It returns the position where the specified character or substring first begins. If the specified text is absent, it raises an error.

    Explanation

    The course finds both individual characters and substrings, and shows that searching for a missing character causes an error.

  28. 28

    What information must be supplied to the string replace() method?

    Model answer

    Supply the text to find and the text that should replace it.

    Explanation

    The course uses replace() to substitute one word in a phrase with another.

  29. 29

    What kinds of numeric values can Python represent in the examples, and how are they written?

    Model answer

    Python can represent whole numbers, decimal numbers, and negative numbers; they are written as numeric literals without quotation marks.

    Explanation

    The course demonstrates integers, decimals, and negative numbers.

  30. 30

    Which basic arithmetic operations does the course demonstrate in Python?

    Model answer

    Addition, subtraction, multiplication, and division.

    Explanation

    The course shows that arithmetic expressions can be evaluated directly in Python.

  31. 31

    How can parentheses affect a mathematical expression in Python?

    Model answer

    Parentheses group operations so the grouped expression is evaluated first, changing the order of operations when needed.

    Explanation

    The course contrasts multiplying before adding with adding inside parentheses before multiplying.

  32. 32

    What does the modulus operator return?

    Model answer

    It returns the remainder after dividing the first number by the second.

    Explanation

    For example, the course explains that dividing ten by three leaves a remainder of one.

  33. 33

    When might a number need to be converted to a string, and which function performs that conversion?

    Model answer

    Convert it when combining or printing it alongside text; use str().

    Explanation

    The course shows that attempting to combine a number directly with a string causes an error.

  34. 34

    What does abs() return for a number?

    Model answer

    It returns the number's absolute value.

    Explanation

    The course applies it to a negative number and gets its positive magnitude.

  35. 35

    What does pow() calculate, and what arguments does the example use?

    Model answer

    pow() raises a base number to a specified power; the example supplies the base and exponent as its two arguments.

    Explanation

    The course uses it to calculate powers such as three raised to the second power.

  36. 36

    How do max() and min() differ?

    Model answer

    max() returns the largest supplied value, whereas min() returns the smallest.

    Explanation

    The course compares two numbers with each function to demonstrate their opposite purposes.

  37. 37

    What does round() do to a decimal number in the examples?

    Model answer

    It rounds the number to the nearest whole number according to standard rounding rules.

    Explanation

    The course demonstrates rounding a value below the midpoint down and one above it up.

  38. 38

    Why might a program import Python's math module, and what functions does the course demonstrate from it?

    Model answer

    Importing math provides additional math functions. The course demonstrates floor(), ceil(), and sqrt().

    Explanation

    The material uses the module import to gain access to functions beyond the basic numeric operations.

  39. 39

    How do the floor() and ceil() functions differ?

    Model answer

    floor() returns the lower whole-number value, while ceil() rounds upward to the next whole-number value.

    Explanation

    The course shows floor() removing the decimal part and ceil() rounding a decimal value upward.

  40. 40

    What does sqrt() return?

    Model answer

    It returns the square root of the supplied number.

    Explanation

    The course gives the square root of thirty-six as an example.

  41. 41

    What does Python's input() function do, and how can its prompt help a user?

    Model answer

    input() pauses for a user's entry and returns what the user types. Its prompt tells the user what information to enter.

    Explanation

    The examples prompt for information such as a name, age, or number.

  42. 42

    What kind of value does input() return by default, and why does this matter when doing arithmetic?

    Model answer

    It returns a string by default. Values from input must be converted to numbers before they can be added arithmetically.

    Explanation

    The calculator example initially joins the inputs as text until they are converted to numeric values.

  43. 43

    How do int() and float() differ when converting user input?

    Model answer

    int() converts input to a whole-number integer, while float() converts it to a decimal-capable number.

    Explanation

    The course notes that converting decimal input with int() causes an error, whereas float() accepts it.

  44. 44

    What steps does the basic calculator use to add two numbers entered by a user?

    Model answer

    It collects two inputs, converts them from strings to numeric values, adds them, and prints the result.

    Explanation

    The calculator example stores the values, performs the addition, and displays the answer.

  45. 45

    How does the more advanced calculator decide which arithmetic operation to perform?

    Model answer

    It collects two numbers and an operator, then uses conditional checks to select addition, subtraction, division, or multiplication.

    Explanation

    The calculator checks the operator entered by the user and reports an invalid operator if it does not match one of the supported symbols.

  46. 46

    How does a Mad Libs program use user input and variables?

    Model answer

    It collects words from the user, stores them in variables, and inserts those values into a prepared story or poem.

    Explanation

    The example collects a color, a plural noun, and a celebrity name and uses them to complete a poem.

  47. 47

    Why does collecting input make a program more interactive?

    Model answer

    It lets a program use information supplied by the user to determine its output or behavior.

    Explanation

    The examples use user-provided names, numbers, and words to produce customized responses.

  48. 48

    What is a list, and what kinds of values can it contain?

    Model answer

    A list is a structure for storing and organizing multiple values. It can contain values such as strings, numbers, and Booleans.

    Explanation

    The course uses a list of friends and notes that lists can hold different kinds of values.

  49. 49

    How is a Python list written, and how does it differ from a variable holding one value?

    Model answer

    A list is written inside square brackets and can hold multiple values, while a simple variable assignment typically stores one value.

    Explanation

    The course creates a friends list with several entries inside its brackets.

  50. 50

    How do you access a particular list element by position?

    Model answer

    Use the list name followed by square brackets containing the element's index.

    Explanation

    The course retrieves particular friends by using their positions in the list.

  51. 51

    How are list indexes counted, and what is special about negative indexes?

    Model answer

    Positive indexes start at zero; negative indexes count from the end, with -1 referring to the last element.

    Explanation

    The course demonstrates accessing both the beginning and end of a list.

  52. 52

    How can slicing select part of a list, and what happens to the ending index?

    Model answer

    Use a start and end index separated by a colon. The slice includes the start position but stops before the ending index.

    Explanation

    The course demonstrates selecting elements from a list using an index range.

  53. 53

    How can an existing element in a list be changed?

    Model answer

    Assign a new value to the element at its index.

    Explanation

    The course changes the value at index one by assigning a different name to that position.

  54. 54

    What does extend() do to a list?

    Model answer

    It adds the elements of another collection, such as another list, to the end of the list.

    Explanation

    The course extends a friends list with the values from a lucky-numbers list.

  55. 55

    How does append() differ from insert()?

    Model answer

    append() adds one item at the end of a list. insert() adds an item at a specified index and shifts later items along.

    Explanation

    The course demonstrates appending a friend and inserting a new friend at index one.

  56. 56

    What do remove(), clear(), and pop() do to a list?

    Model answer

    remove() deletes a specified value, clear() removes all items, and pop() removes the last item by default.

    Explanation

    The course demonstrates each method on a list of friends.

  57. 57

    What does the list method index() return, and what happens if the value is absent?

    Model answer

    It returns the position of a specified value. If the value is not in the list, it raises an error.

    Explanation

    The course finds a friend's index and shows that searching for a missing name produces an error.

  58. 58

    What does a list's count() method return?

    Model answer

    It returns how many times a specified value occurs in the list.

    Explanation

    The course adds a duplicate name and uses count() to determine its frequency.

  59. 59

    What do sort() and reverse() do, and how do their effects differ?

    Model answer

    sort() arranges list values in ascending order; reverse() reverses the current order of the list.

    Explanation

    The course shows sorting names alphabetically and reversing the order of numbers.

  60. 60

    What does the list method copy() provide?

    Model answer

    It creates a copy of a list that can be stored in another variable.

    Explanation

    The course assigns a copied friends list to a second variable.

  61. 61

    What is a tuple, and what notation is used to create one?

    Model answer

    A tuple is a data structure for storing multiple values. It is written using parentheses.

    Explanation

    The course represents coordinates as a tuple of numbers.

  62. 62

    What does it mean that a tuple is immutable, and how does this contrast with a list?

    Model answer

    An immutable tuple cannot have its contents changed after creation. A list can be modified by adding, removing, or changing elements.

    Explanation

    The course contrasts the fixed coordinate tuple with a list whose elements can be reassigned.

  63. 63

    Why might coordinates be stored in a tuple, and when would a list be more appropriate?

    Model answer

    A tuple suits data such as coordinates that should not change. A list is more appropriate when the stored data needs to be modified.

    Explanation

    The course presents coordinates as an example of stable data and notes that lists are used more often when data changes.

  64. 64

    What is a dictionary, and what are its key-value pairs used for?

    Model answer

    A dictionary stores key-value pairs, allowing a value to be retrieved using its associated key.

    Explanation

    The course compares this organization to a conventional dictionary, where a word is associated with its definition.

  65. 65

    How is a Python dictionary written, and what separates a key from its value?

    Model answer

    A dictionary is written inside curly braces; a colon separates each key from its value, and commas separate entries.

    Explanation

    The month-conversion example maps abbreviated month names to full names.

  66. 66

    What constraint does the course place on dictionary keys?

    Model answer

    Dictionary keys must be unique.

    Explanation

    The course identifies duplicate keys as an error in the dictionary design.

  67. 67

    How can a dictionary value be accessed with square brackets or get()?

    Model answer

    Use the key inside square brackets or pass the key to get(); the dictionary returns the associated value.

    Explanation

    The course uses both approaches to retrieve full month names from abbreviations.

  68. 68

    What advantage does get() provide when a dictionary key may be missing?

    Model answer

    It can return a specified default value if the requested key is not present.

    Explanation

    The course uses a message such as 'not a valid key' as a fallback instead of receiving None.

  69. 69

    What kinds of values can be used as dictionary keys in the examples?

    Model answer

    The course demonstrates string keys and notes that numeric keys can also be used, provided the keys are unique.

    Explanation

    The dictionary examples use month abbreviations and discuss using numbers as keys.

  70. 70

    What is an if statement used for?

    Model answer

    It lets a program execute particular code when a condition is true and skip or choose other code when the condition is false.

    Explanation

    The course describes if statements as a way for programs to make decisions based on data.

  71. 71

    How does an else branch extend an if statement?

    Model answer

    It specifies what code should run when the if condition is false.

    Explanation

    The course uses an else branch to handle the alternative to the true condition.

  72. 72

    What is the purpose of elif in a conditional chain?

    Model answer

    elif checks another condition when the preceding if or elif condition was false.

    Explanation

    The course uses multiple conditional branches to handle different combinations of Boolean values.

  73. 73

    How do the and and or operators differ in a condition?

    Model answer

    and requires both conditions to be true; or is true when at least one condition is true.

    Explanation

    The course uses these operators to check combinations of two Boolean variables.

  74. 74

    What effect does not have on a Boolean condition?

    Model answer

    It negates the condition, turning true into false and false into true.

    Explanation

    The course uses not to test that a Boolean variable is false.

  75. 75

    Which comparison operators does the course identify for use in conditions?

    Model answer

    Equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to.

    Explanation

    The course uses comparisons between numbers or strings to produce conditions for if statements.

  76. 76

    What Boolean result does a comparison such as 'first value is greater than or equal to second value' produce?

    Model answer

    It produces either True or False, depending on the relationship between the values.

    Explanation

    Comparison results can be used directly as conditions in an if statement.

  77. 77

    What is a while loop, and what determines whether it repeats?

    Model answer

    A while loop repeatedly executes a block of code as long as its condition remains true.

    Explanation

    The course describes the condition as a loop condition or guard that is checked throughout repetition.

  78. 78

    Why must a loop's state change when necessary inside a while loop?

    Model answer

    If the condition never becomes false, the loop will continue repeating. Updating a variable used in the condition can eventually stop it.

    Explanation

    The counting example increments its counter until it is no longer less than or equal to ten.

  79. 79

    How does the guessing game use a while loop to keep asking for a secret word?

    Model answer

    It repeats the input prompt while the user's guess is not equal to the secret word.

    Explanation

    Once the guess matches the secret word, the loop ends and the program reports success.

  80. 80

    How can a guessing game limit the number of attempts?

    Model answer

    Track the number of guesses, compare it with a guess limit, and stop allowing guesses once the limit is reached.

    Explanation

    The example increments a guess counter and uses an out-of-guesses Boolean to stop the loop.

  81. 81

    How does the guessing game distinguish winning from losing after its loop ends?

    Model answer

    It checks whether the user ran out of guesses: if so, the user loses; otherwise, the correct guess ended the loop and the user wins.

    Explanation

    The course accounts for the two possible reasons the guessing loop terminates.

  82. 82

    What does a for loop do, and what role does its loop variable play?

    Model answer

    A for loop iterates over the items in a collection. Its loop variable represents the current item on each iteration.

    Explanation

    The course uses a loop variable to access each letter in a string and each friend in a list.

  83. 83

    How does range() behave when given one number or a start and end number?

    Model answer

    With one number, it counts from zero up to but not including that number. With a start and end, it counts from the start up to but not including the end.

    Explanation

    The course demonstrates ranges from zero and from a specified starting value.

  84. 84

    How can a for loop use a list's length to visit each list element by index?

    Model answer

    Loop over range(len(list)) and use each generated index to access the corresponding list element.

    Explanation

    The course uses this method to print every friend by indexing the friends list.

  85. 85

    What is a nested loop, and how can it process a two-dimensional list?

    Model answer

    A nested loop is a loop inside another loop. The outer loop can visit each inner list, while the inner loop visits each item in that list.

    Explanation

    The course uses nested for loops to print all values in a grid.

  86. 86

    How are values in a two-dimensional list accessed?

    Model answer

    Use one index for the outer list's row and another index for the inner list's column.

    Explanation

    The course accesses a grid value by specifying its row and column positions.

  87. 87

    How does the translator example decide whether to replace a letter?

    Model answer

    It loops through the phrase and checks whether each letter is a vowel; vowels become g, while other letters are added unchanged.

    Explanation

    The translator applies the stated rule to every character and builds a new translation.

  88. 88

    How can a translator preserve uppercase vowels while replacing vowels?

    Model answer

    Check whether a vowel is uppercase and replace it with uppercase G; otherwise replace it with lowercase g.

    Explanation

    The course adds a capitalization check so the translation can preserve uppercase form.

  89. 89

    How does a for loop help implement a function that raises a number to a power?

    Model answer

    Repeat multiplication of the base by itself as many times as indicated by the exponent, updating a result variable each time.

    Explanation

    The course's function starts with a result of one, loops according to the power, and returns the accumulated product.

  90. 90

    What is a function, and why can functions improve program organization?

    Model answer

    A function is a named collection of code that performs a specific task. It lets a program organize related code into reusable chunks.

    Explanation

    The course groups code for tasks such as greeting a user into functions.

  91. 91

    How is a function defined and called in Python?

    Model answer

    Define it with def, a name, parentheses, and a colon; call it by using its name followed by parentheses.

    Explanation

    The course shows that defining a function alone does not execute its body; calling it does.

  92. 92

    What indentation rule applies to code inside a function?

    Model answer

    The function's body must be indented beneath its definition.

    Explanation

    The course explains that indentation marks which lines belong to the function.

  93. 93

    What are function parameters, and how are they used?

    Model answer

    Parameters are named inputs listed in a function definition. Their values are supplied when the function is called and can affect what it does.

    Explanation

    The greeting function accepts a name and age so its output can vary for different callers.

  94. 94

    What does a return statement do in a function?

    Model answer

    It sends a value back to the code that called the function.

    Explanation

    The course's cube function returns the result so it can be printed or stored.

  95. 95

    What happens to a function's execution when Python reaches return?

    Model answer

    The function ends at the return statement, so code later in that function is not executed.

    Explanation

    The course demonstrates that a print statement placed after return is unreachable.

  96. 96

    How can a caller store a function's returned value?

    Model answer

    Assign the function call to a variable; the variable stores the value returned by the function.

    Explanation

    The course assigns the result of a cube function call to a result variable.

  97. 97

    What is a class, and what is an object created from it?

    Model answer

    A class defines a data type or template; an object is a particular instance of that class with its own data.

    Explanation

    The course defines a student class and then creates individual student objects.

  98. 98

    What is the purpose of the __init__ method in a class?

    Model answer

    It initializes a newly created object by assigning its attributes from the supplied values.

    Explanation

    The student example uses __init__ to set each student's name, major, GPA, and probation status.

  99. 99

    What does self refer to in a class method?

    Model answer

    self refers to the particular object whose method is running, allowing the method to access or set that object's attributes.

    Explanation

    The course uses self.name and self.GPA to refer to attributes of a specific student.

  100. 100

    How does an object attribute differ from a parameter passed into __init__?

    Model answer

    A parameter is an input value to initialization; an object attribute stores that value as part of the particular object.

    Explanation

    The student class assigns each parameter, such as name, to an attribute such as self.name.

  101. 101

    What is a class function or method, and what can it do for objects of that class?

    Model answer

    It is a function defined inside a class. It can provide information about an object or modify the object's values.

    Explanation

    The course's honor-roll method checks a student's GPA and returns whether the student qualifies.

  102. 102

    How does the example determine whether a student is on the honor roll?

    Model answer

    It returns true when the student's GPA is at least 3.5 and false otherwise.

    Explanation

    The course defines the honor-roll condition as a GPA of 3.5 or higher.

  103. 103

    What is inheritance in object-oriented programming?

    Model answer

    Inheritance lets one class receive the attributes and methods of another class, so the new class can reuse its functionality.

    Explanation

    The Chinese-chef class inherits the general chef's methods rather than rewriting them.

  104. 104

    How can a subclass change an inherited method's behavior?

    Model answer

    Define a method with the same name in the subclass, overriding the inherited method.

    Explanation

    The Chinese-chef class overrides the special-dish method so it makes orange chicken instead of barbecue ribs.

  105. 105

    What is the purpose of using a question class in a multiple-choice quiz program?

    Model answer

    It provides a question data type that can store each question's prompt and answer together.

    Explanation

    The quiz uses question objects in a list and checks each user's response against the associated answer.

  106. 106

    What is a comment in Python, and what symbol begins a single-line comment?

    Model answer

    A comment is text for people reading the code that Python ignores during execution. A hash symbol begins a single-line comment.

    Explanation

    The course uses comments to leave notes or explain code.

  107. 107

    How can a programmer comment out code, and why might that be useful?

    Model answer

    Place a comment marker before the code so Python ignores it without deleting it. This can help test how a program behaves without that line.

    Explanation

    The course describes commenting out code as a way to investigate whether a line is causing trouble.

  108. 108

    What is an exception, and why should programs sometimes handle exceptions?

    Model answer

    An exception is an error that can interrupt a program. Handling expected exceptions lets the program respond instead of stopping abruptly.

    Explanation

    The course uses invalid input as an example of an error that a program can catch.

  109. 109

    How does a try/except block handle an error?

    Model answer

    Code that might fail is placed in try; if a matching exception occurs, the corresponding except block runs.

    Explanation

    The course places input conversion in a try block and prints a response when conversion fails.

  110. 110

    Why is catching a specific exception generally preferable to a bare except?

    Model answer

    A specific exception handler responds only to the error it is intended to handle, while a bare except can catch many unrelated errors.

    Explanation

    The course warns that a broad exception clause may incorrectly treat unrelated problems, such as division by zero, as invalid input.

  111. 111

    How can a program access the message associated with a caught exception?

    Model answer

    Bind the exception to a variable with as in the except clause, then use that variable, for example, to print the error message.

    Explanation

    The course demonstrates storing an error in a variable and printing it.

  112. 112

    How does Python's open() function select a file and its access mode?

    Model answer

    Supply the file name or path and a mode indicating how the file will be used, such as read, write, append, or read/write.

    Explanation

    The course explains that a path can be relative or absolute, or just a name if the file is in the same directory.

  113. 113

    What do the file modes r, w, a, and r+ mean?

    Model answer

    r reads, w writes and overwrites, a appends to the end, and r+ allows both reading and writing.

    Explanation

    The course distinguishes these modes by whether they permit reading, replacing content, or adding content.

  114. 114

    Why should a program close a file after using it?

    Model answer

    Closing the file releases it when the program is finished working with it and is recommended as good practice.

    Explanation

    The course pairs opening a file with closing it after the file operations are complete.

  115. 115

    What does a file's readable() method report?

    Model answer

    It returns a Boolean indicating whether the file can be read in its current mode.

    Explanation

    The course shows it returning true for a file opened in read mode and false for one opened only for writing.

  116. 116

    How do read(), readline(), and readlines() differ?

    Model answer

    read() retrieves the file's contents, readline() retrieves one line at a time, and readlines() returns the lines together in a list.

    Explanation

    The course demonstrates reading the whole file, advancing through individual lines, and collecting lines in an array-like list.

  117. 117

    How can a for loop process lines from a file?

    Model answer

    Iterate over the file's lines and process or print each line during the loop.

    Explanation

    The course loops through the employee file and prints each employee line.

  118. 118

    What is the difference between appending to a file and writing to it in w mode?

    Model answer

    Appending adds content at the end of an existing file; w mode overwrites the existing contents.

    Explanation

    The course shows that repeated appending can add duplicate data, while writing replaces the file's previous contents.

  119. 119

    When appending a new record, how can a program ensure it begins on a new line?

    Model answer

    Include a newline character before the record when the file does not already end with a newline.

    Explanation

    The course demonstrates that omitting the newline can cause appended text to run together with the existing line.

  120. 120

    How can Python code create a new file, and what kinds of files does the example mention?

    Model answer

    Open a new file in write mode and write content to it. The course mentions creating text files and an HTML file.

    Explanation

    The course demonstrates writing an HTML page from Python as well as working with text files.

  121. 121

    What is a Python module, and how can code in one module be used from another file?

    Model answer

    A module is a Python file whose code can be imported into another file, allowing that file to use its functions and variables.

    Explanation

    The course imports a useful-tools file to call its dice-rolling function without copying the function into the main file.

  122. 122

    What kinds of content can a module provide to an importing program?

    Model answer

    A module can provide functions, variables, and other reusable code.

    Explanation

    The course's example module contains constants, a list, and functions.

  123. 123

    What is the difference between built-in and external Python modules?

    Model answer

    Built-in modules are included with Python, while external modules are separate packages that may need to be installed.

    Explanation

    The course distinguishes modules included with Python from third-party modules.

  124. 124

    What is pip, and what role does it play in managing Python packages?

    Model answer

    pip is a package manager used to install, update, manage, or uninstall Python packages.

    Explanation

    The course uses pip to install and uninstall an external package.

  125. 125

    How does the course demonstrate installing and uninstalling an external package with pip?

    Model answer

    Use pip install followed by the package name to install it, and pip uninstall followed by the package name to remove it.

    Explanation

    The course demonstrates both commands with an external document-generation package.

  126. 126

    What is the Python interpreter, and what is it useful for?

    Model answer

    It is an interactive environment for running Python commands directly, useful for quick tests and experimentation.

    Explanation

    The course describes it as a convenient sandbox for trying code without setting up a program file.

  127. 127

    How does using the interpreter differ from writing a full program in a text editor or IDE?

    Model answer

    The interpreter is convenient for small, immediate experiments, while a text editor or IDE is better organized for writing and maintaining a full program.

    Explanation

    The course recommends the interpreter for quick tests and a code editor for more substantial scripts.

1 of 127
Question 1 of 127

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent