Introduction to Python Functions: Defining and Using Functions for Calculations
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:
- Identify and use basic functions in Python programming.
- Understand and apply Python’s built-in functions like
print()
andinput()
. - Create and define their own functions in Python.
- Write simple Python programs for arithmetic calculations (addition, subtraction, multiplication, and division).
- 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
- Computers with Python installed or access to online Python interpreters, e.g., Repl.it or Google Colab.
- Projector or whiteboard to demonstrate code examples.
- Additional resources:
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
- Lagos State Unified Schemes of Work for Senior Secondary Schools
- Python Programming Textbooks
- Python Documentation: Python Official Documentation
Instructional Materials
- Visual aids displaying examples of function definitions and arithmetic operations in Python.
Content Outline
- Understanding Python Functions
- A function is a block of reusable code that performs a specific task.
- Python has built-in functions like
print()
andinput()
that perform predefined operations. - We can also create custom functions to carry out specific tasks.
- Defining a Function
- Use the
def
keyword to define a function. - Syntax:
- Example of a basic function that prints a greeting:
- Calling a function:
- Use the
- Simple Functions for Arithmetic Operations
- We can define functions for basic arithmetic like addition, subtraction, multiplication, and division.
- Example functions for each operation:
- 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:
Objective Questions
- What keyword is used to define a function in Python?
a)define
b)def
c)function
d)func
- Which of the following is NOT an arithmetic operation function?
a)add()
b)subtract()
c)divide()
d)print()
- What will the following code return if
divide(10, 0)
is called?a)
10
b)Error
c)Cannot divide by zero
d)None
- 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 - Which of the following correctly calls a function named
multiply
with parameters5
and3
?
a)multiply[5, 3]
b)multiply(5, 3)
c)multiply{5, 3}
d)multiply-5-3
Class Activity Discussion
- What are the advantages of using functions in programming?
- Why is
def
used in Python functions? - How would you modify the
divide
function to handle non-numeric inputs gracefully? - What is the output if
multiply(4, 2)
is called in the function provided? - Why is a dictionary useful in programming, and how can it assist in organizing data?
- What does the
return
statement do in a Python function? - How does defining a function make a program more efficient?
- What would happen if a function is called without providing required parameters?
- Why should you avoid division by zero in a Python program?
- In what situations might you use Python’s embedded dictionary in programming?
Evaluation Questions
- Define a function in Python and provide a simple example.
- Write a function in Python that adds two numbers and returns the result.
- What will the output be if
subtract(10, 5)
is called? - Explain the use of the return statement in Python functions.
- Write a Python function named
multiply
that takes two arguments and returns their product. - How can dictionaries in Python be useful for storing and retrieving data?
- Write a Python program that defines a function to divide two numbers and handles division by zero.
- How does a dictionary differ from a list in Python?
- Explain how you would use a function to organize repetitive tasks in a program.
- 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.