From the course: Introduction to Python: Learn How to Program Today with Python by Pearson
Unlock this course with a free trial
Join today to access over 25,400 courses taught by industry experts.
Write your own functions - Python Tutorial
From the course: Introduction to Python: Learn How to Program Today with Python by Pearson
Write your own functions
So now, let's look at creating our own functions from scratch. And so you can think of functions as like a recipe or a set of instructions that may take some inputs in and may produce some result or output afterwards. So we can test them out in the console, but it might end up being easier working in a file. Let's do that in a second. So to create a function, you're going to use a new keyword called def. That stands for define. You're defining a new function. Then you give it a name. And maybe I'll call it just a basic one called add. Then you put parentheses. Inside the parentheses are just what inputs you want your function to take. So if you don't want any inputs, you can leave it empty. And then at the end of your line, you add a colon. Now is when we start getting into code blocks, and these code blocks in Python are always indented. So the convention is to use four spaces for indentation, though if your code already has a different convention, like two spaces, just use that. So…
Contents
-
-
-
-
(Locked)
Learning objectives51s
-
(Locked)
Learn about types6m 35s
-
(Locked)
Work with variables14m 9s
-
(Locked)
Debug errors15m 44s
-
(Locked)
Manipulate strings13m 10s
-
(Locked)
Write a Mad Libs program7m 14s
-
(Locked)
Use modules16m 32s
-
(Locked)
Write your own functions16m 7s
-
(Locked)
Write a timer game12m 19s
-
(Locked)
More context: Learn about Python11m 9s
-
(Locked)
-
-
-
-
-
-