CPSC Midterm review

Printing Example with Loops

  • The initial example involves a basic loop that prints numbers starting from index ( I = 0 ) up to the last index, exclusive.

    • The iterations of this loop can be outlined as follows:

    • First iteration: ( I = 0 ) → prints "0"

    • Second iteration: ( I = 1 ) → prints "1"

    • Continue until the last index, where it prints the final number before the last index.

Nested Loops

  • A nested loop consists of an outer loop and one or more inner loops.

    • The outer loop runs through values of ( I ).

    • Example range for ( I ): 0 to 3 (exclusive)

    • The inner loop runs through values of ( j ).

    • Example range for ( j ): 0 to 2 (exclusive)

  • Key Aspects of Nested Loops:

    • The inner loop must fully complete all its iterations before the outer loop can run its next iteration.

    • The iterations of nested loops can be traced as follows:

    • First iteration of outer loop: ( I = 0 )

      • First iteration of inner loop: ( j = 0 ) → prints "0 0"

      • Second iteration of inner loop: ( j = 1 ) → prints "0 1"

      • Third iteration of inner loop: ( j = 2 ) → prints "0 2"

    • After all inner iterations for ( I = 0 ) are complete, the outer loop iterates to ( I = 1 ).

Understanding Two-Dimensional Lists (2D Lists)

  • A two-dimensional list represents a table-like structure with elements accessible through row and column indices.

  • A visual representation includes:

    • Column indices: 0, 1, 2

    • Row indices: 0, 1, 2, 3

    • Each position in the 2D list is accessed using its row and column index.

  • Access Examples:

    • To access the element "4":

    • Row index: 1

    • Column index: 0 → Access with ( list[1][0] )

    • To access the element "8":

    • Row index: 2

    • Column index: 1 → Access with ( list[2][1] )

  • Using nested for loops to traverse through a 2D list:

    • Outer loop: Iterates through row indices (each iteration corresponds to one row).

    • Inner loop: Iterates through column indices of the current row.

    • Proceeding through each row and column systematically.

Functions and Their Benefits

  • Functions improve code quality by promoting:

    • Reusability: Avoid code duplication.

    • Modularity: Organize and break down complex tasks into simpler ones.

    • Ease of debugging: Bugs are localized in specific functions.

    • Stepwise refinement: Break tasks into smaller, manageable parts.

  • A proper function consists of:

    • Function definition (what it does): def function_name(parameters):

    • Function calls (invoking the function).

  • Important note: Calling a function that hasn't been defined results in a Runtime Error (Name Error).

Function Scope

  • Scope refers to the context within a program where a variable can be accessed.

  • Variables can be local (accessible only within a function) or global (accessible throughout the program).

  • Example showing scope:

    • Local Variables: Defined in a function and not accessible outside of it.

    • Global Variables: Available anywhere in the program unless shadowed by local variables.

    • Example:

    • ( name_1 ) is local to print_friends.

    • ( name_2 ) is local to print_family.

Parameter Passing

  • A distinction exists between parameters (placeholders in function definitions) and arguments (actual values passed into functions).

    • Example:

    • Function Definition: def function(param1, param2):

    • Function Call: function(value1, value2)

  • Parameters have local scope within the function.

  • When a function does not return a specific value, it returns None.

Global Scope and Variable Modifications

  • Global variables can be read from anywhere but require the global keyword if modifications are intended.

  • A local variable of the same name will shadow the global variable inside that specific function.

Function Structure with Optional Parameters

  • Python allows for optional parameters in functions.

    • Example syntax includes optional defaults: def function(a, b=default_value):

  • Parameters can be called in any order if specified explicitly using the parameter name in the function call:

    • Example: function(b=2, a=10).

Dictionary Operations

  • A dictionary consists of key-value pairs, whereas a list contains single elements.

  • Key identifiers must be unique; values can be repeated across different keys.

  • The values in a dictionary can be accessed via their respective keys, e.g.: dict[key].

  • Immutable data types can be used as dictionary keys (e.g., strings, tuples).

  • Dictionaries preserve the order of entries from Python 3.7 onwards.

Miscellaneous Concepts

  • Pass Statement: The pass statement allows for creating a function that does nothing, enabling structure compilation.

  • Optional parameters provide flexibility in function calls, allowing parameters to be specified in any order if explicitly defined.

Conclusion

  • Revisit the code, examples, and concepts frequently to ensure a thorough understanding. The topics covered relate closely to exam material, particularly functions and their scopes.