Lesson 8 - Functions, Classes, and Objects PDF

Summary

This document provides a lesson on functions, classes, and objects in Python programming. It explains how to define and call functions, and also details how to create and use classes with objects and demonstrate examples of various Python programming concepts.

Full Transcript

Lesson 8: Functions, Classes, and Objects In this lesson, we will be learning how to create and use functions, classes, After studying this lesson, you should be able to: ✓ define functions, classes, and objects; FPPT.com ✓ create samples of functions, classes, and...

Lesson 8: Functions, Classes, and Objects In this lesson, we will be learning how to create and use functions, classes, After studying this lesson, you should be able to: ✓ define functions, classes, and objects; FPPT.com ✓ create samples of functions, classes, and objects; and ✓ use them in programs. Functions allow us to perform various tasks without having to code the instructions repeatedly. All we have to do is call the function and pass on some arguments to get some form of result. We can create our own custom functions in Python. And, it is a language capability that programmers should take advantage of. By breaking down our programs into smaller chunks of reusable code, we can make them more maintainable and flexible. Defining and Calling Functions To define a function, we use the def keyword. We specify the function name, its input parameters in parentheses (if needed), and a colon (:). Example: OUTPUT: Defining and Calling Functions We can also create functions that return a value. Example: OUTPUT: In this example, the function simply returns the value 5. We had to call the print() function to display the value that the function returns. Arguments and Parameters We can also define functions that take in arguments. The values indicated in the argument are passed on to the corresponding parameter within the function. For example, let’s create a function called times_five(), which multiplies any value passed into it through the argument “number” by five. Code: OUTPUT: Arguments and Parameters Let’s try converting the vowel checker program that we had in the previous unit into a function. The function takes on the argument “letter”. Code: OUTPUT: Positional Arguments In Python, functions can have positional arguments and keyword arguments. Positional arguments pass on values based on their position. Example: OUTPUT: ❖ When it is time to call the function, you have to remember what each argument represents in order. Positional Arguments ❖ Now, let’s try calling the function again and intentionally mix up the order. Example: OUTPUT: ❖ Notice how the keys and values are now mismatched because the position is important. Positional Arguments Keyword arguments allows you to indicate the argument names and their corresponding parameters. Example: OUTPUT: ❖ By using keyword arguments, you’d be assured that the values get assigned to the proper parameters Positional Arguments What if you forgot to add providing a value for an expected argument? In this example, the function expects to take on two arguments, but we only indicated one when we called it. Example: OUTPUT: Taking On a Variable Number of Arguments What if we need our function to be flexible and be able to take on a variable number of arguments? Fortunately, Python allows us to do this by using *args, which generates a tuple as values. OUTPUT: Example: Taking On a Variable Number of Arguments What if we need our function to be flexible and be able to take on a variable number of arguments? You can also use **kwargs to create a dictionary from the arguments. Example: OUTPUT: Global vs. Local Variables Global variables are accessible throughout the program. Local variables are only accessible within the function in which they are declared. Example: To Declare variables: To call a function with variables: Objects and Classes As we discussed in the first unit, Python can be used in different programming models, including Object-Oriented Programming (OOP). OOP is a programming paradigm where software is designed based on real-world objects rather than simply as sequences of instructions and logic. Objects in OOP are data structures that have attributes and methods. Class is the characteristic of an object. Example: Dog > Bantay > brown fur and four legs Defining Classes To create a class, we use the class keyword. The __init__ method is called a constructor in OOP. It allows the class to create an object and initialize the attributes that the object can take. The self keyword allows us to access the attributes and methods that we will be creating for the class. Example: Creating Objects To create an object from a class, we call the class and assign it to a variable. Example: We can access the attribute values of p1 by using dot notation. Objects and Classes Example # 1: Output: Objects and Classes Example # 2: Output: Adding and Using Methods Next, using the CODE in Example # 2 let’s add an ability to our DOG class. Example: Output: This allows all dog objects to bark and can be triggered by calling the bark() method of our object. Changing Object Properties You can also change the values of object Example: properties using the assignment operator. Output: Deleting an Object Example: To delete an object, use the del keyword. Output: To delete object properties using the del keyword. Example: Output: To delete object properties using the del keyword. Example: Output: Child Classes and Inheritance Python also allows you to Example: create child classes that can inherit or share attributes and methods from its parent class. Example, let’s create a new child class from Dog, say a breed called Pug. Output: Overriding Using Child Classes We can have our child classes Example: override attributes and functions. Output:

Use Quizgecko on...
Browser
Browser