knowt logo

Lecture-18 Modularisation 24-25

Lecture Overview

  • Topic: Modularisation in Python Functions

  • Focus: Understanding modularisation, function definitions, and practical examples.

Page 1: Introduction

  • Course Code: COMP101

  • Lecture Number: 18

  • Year: 2024-25

  • Topic: Modularisation (Python functions)

Page 2: What is Modularisation?

  • Definition: Modularisation is the process of breaking code into smaller, manageable segments called functions.

  • Terminology:

    • Common Terms: Functions, Procedures, Routines, Subroutines, Modules, Methods.

    • Python's Preferred Terminology: Functions.

    • Related Concepts: Function Definition, User-defined Function.

Page 3: Functions vs. Procedures

  • Functions:

    • Serve specific tasks and return values.

  • Procedures:

    • Serve specific tasks but do not return values.

    • Python allows both functionalities in functions.

  • Modules:

    • Collections of functions performing related tasks.

  • Importance:

    • Functions improve code efficiency, maintenance, and reusability.

Page 4: Function Declaration

  • Syntax: def funct_name():

    • def: Keyword to declare a function.

    • funct_name: User-defined function name.

    • Parentheses () and colon : follow the name.

    • Indented code: Code to be executed when called.

  • Function Execution:

    • Control returns to the statement after the caller post execution.

Page 5: Non-Modular vs. Modular Code

  • Non-Modular Code Example:

    • Directly prints menu and handles options within main body.

  • Modular Code Example:

    • Introduces the use of functions to handle repetitive tasks like displaying under-development messages.

    • Function named stub can be reused as needed.

Page 6: Flow of Control in Modular Program

  • Function Definition:

    • Uses def followed by function name.

  • Flow of Control Steps:

    1. Call function using its name and parentheses.

    2. Function executes its indented code.

    3. Upon completion, control returns to the caller.

Page 7: Structure of a Modular Program

  • Starting a program with main() function:

    • Use def main(): to define the main function.

    • Always call main() at the end to start program execution.

    • Import statements precede function definitions.

Page 8: Calling Functions

  • Example Functions:

    • howToDoIf() and howToDoIfElse() demonstrate conditional statements.

    • Main function displays menu and calls respective functions based on user input.

  • Program Execution Pattern:

    • Python scans for the main() call and executes the structured indented code.

Page 9: Step-by-Step Modular Program Development

  • Step 1: Define the main function and call it.

    • Ensures that the program starts from a defined entry point.

Page 10: Implementing a While Loop

  • Step 2: Adding a while loop in main() to ensure continuous menu display until exit.

    • User can only exit with 'X'.

Page 11: Input Validation

  • Step 3: Include input validation for option selection.

    • Prompt re-entry for invalid inputs.

Page 12: Adding Functions and Testing

  • Step 4: Define specific functions (e.g., partyDrinks(), findTheCards()) for each menu option.

    • Ensure functions are defined before use in the main program.

Page 13: Iterative Functionality

  • Step 5: Add loop functionality in specific functions to allow user re-entry.

    • Example: partyDrinks() is tailored to run multiple times.

Page 14: Simple Program Structure

  • Demonstrated the modular program using entry condition check via if __name__ statement.

    • Example of merged operational and standalone functions.

Page 15: Understanding __name__ Concept

  • Explanation:

    • Python identifies .py files as modules, executing the code starting from main().

  • Importance of if __name__ == '__main__'::

    • Distinguishes main program execution vs. when imported elsewhere.

    • The convention of utilizing double underscores:

      • Called "dunder" methods, which Python recognizes for specific functionalities.

YP

Lecture-18 Modularisation 24-25

Lecture Overview

  • Topic: Modularisation in Python Functions

  • Focus: Understanding modularisation, function definitions, and practical examples.

Page 1: Introduction

  • Course Code: COMP101

  • Lecture Number: 18

  • Year: 2024-25

  • Topic: Modularisation (Python functions)

Page 2: What is Modularisation?

  • Definition: Modularisation is the process of breaking code into smaller, manageable segments called functions.

  • Terminology:

    • Common Terms: Functions, Procedures, Routines, Subroutines, Modules, Methods.

    • Python's Preferred Terminology: Functions.

    • Related Concepts: Function Definition, User-defined Function.

Page 3: Functions vs. Procedures

  • Functions:

    • Serve specific tasks and return values.

  • Procedures:

    • Serve specific tasks but do not return values.

    • Python allows both functionalities in functions.

  • Modules:

    • Collections of functions performing related tasks.

  • Importance:

    • Functions improve code efficiency, maintenance, and reusability.

Page 4: Function Declaration

  • Syntax: def funct_name():

    • def: Keyword to declare a function.

    • funct_name: User-defined function name.

    • Parentheses () and colon : follow the name.

    • Indented code: Code to be executed when called.

  • Function Execution:

    • Control returns to the statement after the caller post execution.

Page 5: Non-Modular vs. Modular Code

  • Non-Modular Code Example:

    • Directly prints menu and handles options within main body.

  • Modular Code Example:

    • Introduces the use of functions to handle repetitive tasks like displaying under-development messages.

    • Function named stub can be reused as needed.

Page 6: Flow of Control in Modular Program

  • Function Definition:

    • Uses def followed by function name.

  • Flow of Control Steps:

    1. Call function using its name and parentheses.

    2. Function executes its indented code.

    3. Upon completion, control returns to the caller.

Page 7: Structure of a Modular Program

  • Starting a program with main() function:

    • Use def main(): to define the main function.

    • Always call main() at the end to start program execution.

    • Import statements precede function definitions.

Page 8: Calling Functions

  • Example Functions:

    • howToDoIf() and howToDoIfElse() demonstrate conditional statements.

    • Main function displays menu and calls respective functions based on user input.

  • Program Execution Pattern:

    • Python scans for the main() call and executes the structured indented code.

Page 9: Step-by-Step Modular Program Development

  • Step 1: Define the main function and call it.

    • Ensures that the program starts from a defined entry point.

Page 10: Implementing a While Loop

  • Step 2: Adding a while loop in main() to ensure continuous menu display until exit.

    • User can only exit with 'X'.

Page 11: Input Validation

  • Step 3: Include input validation for option selection.

    • Prompt re-entry for invalid inputs.

Page 12: Adding Functions and Testing

  • Step 4: Define specific functions (e.g., partyDrinks(), findTheCards()) for each menu option.

    • Ensure functions are defined before use in the main program.

Page 13: Iterative Functionality

  • Step 5: Add loop functionality in specific functions to allow user re-entry.

    • Example: partyDrinks() is tailored to run multiple times.

Page 14: Simple Program Structure

  • Demonstrated the modular program using entry condition check via if __name__ statement.

    • Example of merged operational and standalone functions.

Page 15: Understanding __name__ Concept

  • Explanation:

    • Python identifies .py files as modules, executing the code starting from main().

  • Importance of if __name__ == '__main__'::

    • Distinguishes main program execution vs. when imported elsewhere.

    • The convention of utilizing double underscores:

      • Called "dunder" methods, which Python recognizes for specific functionalities.

robot