Introduction to Python Functions: Defining and Using Functions for Calculations Information Technology SS1 First Term Lesson Notes Week 11

Subject: Information Technology
Class: SS1
Term: First Term
Week: 11
Age: 14-16 years
Topic: Introduction to Python II
Sub-topic: Defining Functions, Using Simple Functions for Arithmetic Operations in Python
Duration: 80 minutes


Behavioral Objectives

By the end of the lesson, students should be able to:

  1. Identify and use basic functions in Python programming.
  2. Understand and apply Python’s built-in functions like print() and input().
  3. Create and define their own functions in Python.
  4. Write simple Python programs for arithmetic calculations (addition, subtraction, multiplication, and division).
  5. Make use of the Python embedded dictionary for referencing terms and definitions.

Keywords

  • Functions
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Embedded Dictionary

Set Induction

Begin the lesson by asking students how they might organize tasks they perform daily. Relate this concept to Python functions, which help organize code by grouping instructions together. Explain that functions can perform tasks like addition or displaying information, making programs easier to read and manage.

Entry Behavior

Students are expected to have a basic understanding of Python syntax, including variables, data types, and the use of input/output functions. This lesson will expand their programming knowledge by introducing them to defining and using functions.

Learning Resources and Materials

Building Background/Connection to Prior Knowledge

This lesson builds on students’ understanding of basic Python operations by introducing them to defining and using functions. Students will see how functions can simplify coding tasks, allowing for more efficient and organized code.

Embedded Core Skills

  • Problem Solving: Creating and using functions for arithmetic operations.
  • Analytical Thinking: Applying function definitions to solve computational tasks.
  • Digital Literacy: Understanding Python’s syntax and programming structure.

Learning Materials

  • Lagos State Scheme of Work
  • SS1 Information Technology textbooks
  • Python interpreter or online platform access

Reference Books

Instructional Materials

  • Visual aids displaying examples of function definitions and arithmetic operations in Python.

Content Outline

  1. Understanding Python Functions
    • A function is a block of reusable code that performs a specific task.
    • Python has built-in functions like print() and input() that perform predefined operations.
    • We can also create custom functions to carry out specific tasks.
  2. Defining a Function
    • Use the def keyword to define a function.
    • Syntax:
      python
      def function_name():
      # code block
    • Example of a basic function that prints a greeting:
      python
      def greet():
      print("Hello, welcome to Python!")
    • Calling a function:
      python
      greet() # Output: Hello, welcome to Python!
  3. Simple Functions for Arithmetic Operations
    • We can define functions for basic arithmetic like addition, subtraction, multiplication, and division.
    • Example functions for each operation:
      python
      def add(a, b):
      return a + b
      def subtract(a, b):
      return a – b

      def multiply(a, b):
      return a * b

      def divide(a, b):
      if b != 0:
      return a / b
      else:
      return “Cannot divide by zero”

  4. Using Python’s Embedded Dictionary
    • Python’s embedded dictionary is a useful feature to store and access key-value pairs.
    • Example of using a dictionary to store and retrieve definitions:
      python
      terms = {
      "function": "A block of code that performs a specific task",
      "variable": "A storage location in a program"
      }
      print(terms["function"]) # Output: A block of code that performs a specific task

Objective Questions

  1. What keyword is used to define a function in Python?
    a) define
    b) def
    c) function
    d) func
  2. Which of the following is NOT an arithmetic operation function?
    a) add()
    b) subtract()
    c) divide()
    d) print()
  3. What will the following code return if divide(10, 0) is called?
    python
    def divide(a, b):
    if b != 0:
    return a / b
    else:
    return "Cannot divide by zero"

    a) 10
    b) Error
    c) Cannot divide by zero
    d) None

  4. In Python, what does return do in a function?
    a) Displays output on the screen
    b) Ends the function
    c) Outputs a value from the function
    d) Deletes variables
  5. Which of the following correctly calls a function named multiply with parameters 5 and 3?
    a) multiply[5, 3]
    b) multiply(5, 3)
    c) multiply{5, 3}
    d) multiply-5-3

Class Activity Discussion

  1. What are the advantages of using functions in programming?
  2. Why is def used in Python functions?
  3. How would you modify the divide function to handle non-numeric inputs gracefully?
  4. What is the output if multiply(4, 2) is called in the function provided?
  5. Why is a dictionary useful in programming, and how can it assist in organizing data?
  6. What does the return statement do in a Python function?
  7. How does defining a function make a program more efficient?
  8. What would happen if a function is called without providing required parameters?
  9. Why should you avoid division by zero in a Python program?
  10. In what situations might you use Python’s embedded dictionary in programming?

Evaluation Questions

  1. Define a function in Python and provide a simple example.
  2. Write a function in Python that adds two numbers and returns the result.
  3. What will the output be if subtract(10, 5) is called?
  4. Explain the use of the return statement in Python functions.
  5. Write a Python function named multiply that takes two arguments and returns their product.
  6. How can dictionaries in Python be useful for storing and retrieving data?
  7. Write a Python program that defines a function to divide two numbers and handles division by zero.
  8. How does a dictionary differ from a list in Python?
  9. Explain how you would use a function to organize repetitive tasks in a program.
  10. Write a Python function called add_and_print() that takes two numbers, adds them, and prints the result.

Conclusion

To conclude the lesson, emphasize the value of functions in simplifying complex tasks and organizing code. Reinforce the significance of functions in Python as tools for structuring code effectively and encourage students to experiment with creating their own functions to strengthen their programming skills.