DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

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 (-)

The following is an example of an incredibly simple
function that could be entered into the interactive
Python interpreter:


Click here to view code image


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.

Free download pdf