Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Python Functions, Classes,

and Modules
This chapter covers the following topics:
■■ Python Functions: This section provides an overview of working with and building
Python functions.
■■ Object-Oriented Programming and Python: This section describes key aspects of
using object-oriented programming techniques.
■■ Python Classes: This section provides an overview of creating and using Python
classes.
■■ Working with Python Modules: This section provides an overview of creating and
using Python modules.
This chapter moves away from the basics introduced in Chapter 3, “Introduction to Python,”
and introduces Python functions, classes, and modules. Building Python functions allows
for the creation of reusable code and is the first step toward writing object-oriented code.
Classes are the Python tools used to construct Python objects and make it easier to produce
scalable applications that are easy to maintain and readable. Finally, this chapter introduces
the wide world of Python modules and how they can extend the capabilities of Python and
make your job of coding much easier.
“Do I Know This Already?” Quiz
The “Do I Know This Already?” quiz allows you to assess whether you should read this
entire chapter thoroughly or jump to the “Exam Preparation Tasks” section. If you are in
doubt about your answers to these questions or your own assessment of your knowledge
of the topics, read the entire chapter. Table 4-1 lists the major headings in this chapter and
their corresponding “Do I Know This Already?” quiz questions. You can find the answers in
Appendix A, “Answers to the ‘Do I Know This Already?’ Quiz Questions.”
Table 4-1 “Do I Know This Already?” Section-to-Question Mapping
Foundation Topics Section Questions
Python Functions 1–3
Object-Oriented Programming and Python 4–5
Python Classes 6–8
Working with Python Modules 9–10
FromFoundation Topics
Python Functions
In Python, a function is a named block of code that can take a wide variety of input parameters
(or none at all) and return some form of output back to the code that called the function
to begin with. It represents a key concept in programming sometimes referred to as DRY,
which stands for Don’t Repeat Yourself. The idea behind DRY is that if you perform some
particular operations in your code multiple times, you can simply create a function to reuse
that block of code anywhere you need it instead of duplicating effort by typing it each time.
Python offers two types of functions: built-in functions that are part of the standard library
and functions you create yourself. The standard library includes a huge number of functions
you can use in your program, like print(), many of which you have already been introduced
to in Chapter 3. Building your own functions is how you construct capabilities that are not
already present within the Python language.
To define a function in Python, you use the keyword def, a name for the function, a set of
parentheses enclosing any arguments you want to pass to the function, and a colon at the
end. The name of a function must follow these rules:
■■ Must not start with a number
■■ Must not be a reserved Python word, a built-in function (for example, print(), input(),
type()), or a name that has already been used as a function or variable
■■ Can be any combination of the A–Z, a–z, 0–9 and the underscore (_) and dash (-)
From the Library of Koenig Solutions Pvt Ltd The following is an example of an incredibly simple function that could be
entered into the
interactive Python interpreter:
Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>> def devnet():
'''prints simple function'''
print('Simple function')
>>> devnet()
Simple function
This function prints out the string “Simple function” any time you call it with devnet().
Notice the indented portion that begins on the next line after the colon. Python expects
this indented portion to contain all the code that makes up the function. Keep in mind that
whitespace matters in Python. The three single quotation marks that appear on the first line
of the indented text of the function are called a docstring and can be used to describe what
the function does.
As shown in the following example, you can use the built-in Python function help() to learn
what a function does and any methods that can be used:
>>> help(devnet)
Help on function devnet in module __main__:
devnet()
prints simple function
Using Arguments and Parameters
An argument is some value (or multiple values) that you pass to a function when you call
the function within code. Arguments allow a function to produce different results and make
code reuse possible. You simply place arguments within the parentheses after a function
name. For example, this example shows how you can pass multiple numeric arguments to the
max() function to have it return the largest number in the list:
>>> max(50, 5, 8, 10, 1)
50
Each function must define how it will use arguments, using parameters to identify what
gets passed in and how it gets used. A parameter is simply a variable that is used in a function
definition that allows code within the function to use the data passed to it. To get
results back from the function, you use the keyword return and the object you want to pass
4
From the Library of Koenig Solutions Pvt Ltd

You might also like