Python Programming Final Exam Comprehensive Study Guide

Final Exam Structure and Grading

  • The final exam consists of four primary sections and a bonus section.

  • Section 1: Key Terms and Definitions

    • Format: 1010 multiple choice questions.

    • Weight: 11 point per question, totaling 1010 points.

    • Objective: Identify the correct definition of a specific Python term.

  • Section 2: Code Evaluation

    • Format: 1010 multiple choice questions.

    • Weight: 11 point per question, totaling 1010 points.

    • Objective: Mental evaluation of code to identify the correct output.

  • Section 3: Debugging

    • Format: 1010 multiple choice questions.

    • Weight: 11 point per question, totaling 1010 points.

    • Objective: Correctly identify the specific type of error within a provided code snippet.

  • Section 4: Programming

    • Format: Choice of questions to be solved by typing code into a text box.

    • Selection: You are provided with 55 options and must select 33 to solve.

    • Weight: 55 points per question, totaling 1515 points.

  • Bonus Section

    • Format: Choice of 22 complex programming options.

    • Selection: You may solve only 11 option. No points are awarded for attempting both.

    • Weight: 55 points.

  • Point Summary

    • Total Exam Points: 4545

    • Total Bonus Points: 55

    • Maximum Total Points Available: 5050

Essential Key Terms and Definitions

  • Variable: A name that refers to a value stored in memory.

  • Data Type: The kind of value a variable holds, such as integers, floats, strings, and booleans.

  • Function: A block of code that performs a specific task and can be reused.

  • Iteration: The repeated execution of a block of code, typically using a loop.

  • Conditional Statement: Code that executes based on whether a condition is true or false.

  • List: An ordered collection of items that can be of different data types.

  • Dictionary: A collection of key-value pairs, where each key is unique.

  • Tuple: An ordered collection of items that is immutable, meaning it cannot be changed.

  • Set: An unordered collection of unique items.

  • String: A sequence of characters.

  • Module: A file containing Python code that can be imported and used in other programs.

  • Class: A blueprint for creating objects, defining their properties and behaviors.

  • Object: An instance of a class.

  • Exception Handling: Code that manages errors and exceptions to prevent program crashes (e.g., using try and except).

  • Library: A collection of modules and functions that provide additional functionality.

  • Boolean: A data type with only two possible values: True or False. It is frequently used in conditional statements.

  • Index: The position of an item in an ordered collection, such as a list, string, or tuple. Indexing in Python starts at 00.

  • Slice: A method used to retrieve a subset of a sequence. The syntax for slicing is sequence[start:stop:step].

  • Comment: Text in the code preceded by the # symbol. It is ignored by the interpreter and used for human-readable explanations.

  • Import: A statement used to include external modules or libraries into a program.

  • Indentation: The whitespace at the beginning of lines that defines code blocks in Python.

  • Argument: A value provided to a function when it is called.

  • Return: A statement used within a function to send a result back to the caller.

  • Type Casting: The process of converting a value from one data type to another, such as converting a str to an int.

Code Evaluation and Data Types

  • The type() Function

    • This function is used to determine the data type of an object.

    • Example: x = 10 followed by print(type(x)) outputs <class 'int'>.

    • Example: y = "hello" followed by print(type(y)) outputs <class 'str'>.

    • Example: z = 10.5 followed by print(type(z)) outputs <class 'float'>.

  • Type Compatibility and Errors

    • Combining an integer and a string using addition (x+yx + y) results in a TypeError: unsupported operand type(s) for +: 'int' and 'str'.

    • Combining an integer and a float (x+zx + z) results in a float. For instance, type(b) where b = 10 + 10.5 is <class 'float'>.

  • The print() Function

    • This function outputs data to the standard output device, usually the console.

    • It can take multiple arguments, which are converted to strings and separated by a space by default. It is essential for debugging and displaying results.

  • String Concatenation and Formatting

    • Simple Concatenation: first_name = "John", last_name = "Doe", full_name = first_name + " " + last_name outputs John Doe.

    • Concatenation with Numbers: age = 25, message = "John is " + str(age) + " years old." outputs John is 25 years old. (Note the use of str() for type casting).

    • f-strings: name = "Alice", city = "Wonderland", greeting = f"{name} lives in {city}." outputs Alice lives in Wonderland.

Sequence Operations: Slicing and Indexing

  • Basic Slicing

    • text = "Hello, world!"

    • slice_text = text[0:5] outputs Hello.

  • Slicing with Step

    • text = "abcdef"

    • slice_text = text[::2] outputs ace.

  • Negative Indexing

    • text = "Hello, world!"

    • slice_text = text[-6:-1] outputs world.

  • String Reversal

    • text = "Python"

    • reverse_text = text[::-1] outputs nohtyP.

Math Functions and Logic

  • Addition: def add(a, b): return a + b. add(3, 5) returns 88.

  • Subtraction: def subtract(a, b): return a - b. subtract(10, 4) returns 66.

  • Multiplication: def multiply(a, b): return a * b. multiply(7, 6) returns 4242.

  • Division with Safety Check:

    • python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def divide(a, b): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if b != 0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a / b &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "Division by zero is not allowed"

    • divide(20, 4) returns 5.05.0

  • Squaring: def square(x): return x ** 2. square(5) returns 2525.

  • Square Root: Requires import math. math.sqrt(16) returns 4.04.0.

Identifying Python Error Types for Debugging

  • 1. Syntax Errors

    • Defined as code that violates the language rules. Detected before the program runs.

    • Example: if x > 5 (missing the colon :).

    • Message: SyntaxError: invalid syntax.

  • 2. Indentation Errors

    • Occur when code blocks for functions, loops, or conditionals are not properly aligned.

    • Example:       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def greet(name): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Hello, " + name) # Missing indentation

    • Message: IndentationError: expected an indented block.

  • 3. Index Errors

    • A runtime error occurring when attempting to access an invalid index.

    • Example: x = [1, 2, 3] then print(x[3]) (indices only go to 22).

    • Message: IndexError: list index out of range.

  • 4. Logical Errors

    • The program runs without crashing but produces the wrong result due to flawed logic.

    • Example: A function intended to add numbers return a - b by mistake.

  • 5. Type Errors

    • Occur when an operation is applied to an inappropriate data type.

    • Example: print("hello" + 5).

    • Message: TypeError: can only concatenate str (not "int") to str.

  • 6. Name Errors

    • Occur when accessing a variable or function that has not been defined.

    • Example: print(x) when x has no value assigned.

    • Message: NameError: name 'x' is not defined.

  • 9. Value Errors

    • Occur when a function receives an argument of the right type but with an inappropriate value.

    • Example: int("hello").

    • Message: ValueError: invalid literal for int() with base 10: 'hello'.

  • 10. ZeroDivision Errors

    • Occur when attempting to divide a number by zero.

    • Example: x = 10 / 0.

    • Message: ZeroDivisionError: division by zero.

Programming Logic Scenarios

  • Scenario 1: Age Classification

    • Logic: Ask for age; if less than 1818, print "Minor"; if less than 6565, print "Adult"; else print "Senior".

    • Implementation:       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age = int(input("Enter your age: ")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if age < 18: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Minor") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif age < 65: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Adult") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Senior")

  • Scenario 2: List Extremes

    • Logic: Find the largest and smallest numbers in a list.

    • Implementation: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9], print("Minimum:", min(numbers)), print("Maximum:", max(numbers)).

  • Scenario 3: Vowel Counting

    • Logic: Count how many vowels are in a user-provided string.

    • Method A (Generator): count = sum(1 for char in text if char in "aeiou").

    • Method B (Loop):       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = input("Enter a string: ") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vowels = "aeiouAEIOU" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for char in text: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if char in vowels: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count = count + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Number of vowels:", count)

  • Scenario 4: Random Number Filtering

    • Logic: Generate 1010 random integers between 11 and 100100; print those divisible by 33.

    • Method A (Comprehension):       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import random &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numbers = [random.randint(1, 100) for i in range(10)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Divisible by 3:", [n for n in numbers if n % 3 == 0])

  • Scenario 5: Multiplication Table

    • Logic: Prompt for a number nn; print table up to 1010.

    • While Loop Method:       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = int(input("Enter a number: ")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while i <= 10: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(n, "x", i, "=", n * i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1

  • Scenario 6: Reversing a Word

    • Logic: Manually reverse a string using a loop.

    • Implementation:       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word = input("Enter a word: ") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reversed_word = "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for letter in word: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reversed_word = letter + reversed_word &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Reversed:", reversed_word)

  • Scenario 7: Prime Number Finder

    • Logic: Print all prime numbers less than user input nn.

    • Implementation:       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = int(input("Enter a number: ")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for num in range(2, n): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is_prime = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in range(2, num): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if num % i == 0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is_prime = False &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if is_prime: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(num)

  • Scenario 8: Integer Classification Loop

    • Logic: Iterate through a list and label numbers as positive, negative, or zero.

    • Implementation:       python &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for n in numbers: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if n > 0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(n, "is positive") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif n < 0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(n, "is negative") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(n, "is zero")