Skip to Content
Automate the Boring Stuff with Python
book

Automate the Boring Stuff with Python

by Al Sweigart
April 2015
Intermediate to advanced
504 pages
12h 52m
English
No Starch Press
Content preview from Automate the Boring Stuff with Python

Chapter 3. Functions

You’re already familiar with the print(), input(), and len() functions from the previous chapters. Python provides several builtin functions like these, but you can also write your own functions. A function is like a mini-program within a program.

To better understand how functions work, let’s create one. Type this program into the file editor and save it as helloFunc.py:

➊ def hello():
➋     print('Howdy!')
       print('Howdy!!!')
       print('Hello there.')
➌ hello()
   hello()
   hello()

The first line is a def statement ➊, which defines a function named hello(). The code in the block that follows the def statement ➋ is the body of the function. This code is executed when the function is called, not when the function is first defined.

The hello() ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Automate the Boring Stuff with Python, 2nd Edition

Automate the Boring Stuff with Python, 2nd Edition

Al Sweigart

Publisher Resources

ISBN: 9781457189906Errata Page