Functions Python Tutorial




The idea of a function is to assign a set of code, and possibly variables, known as parameters, to a single bit of text. You can think of it a lot like why you choose to write and save a program, rather than writing out the entire program every time you want to execute it.

To begin a function, the keyword 'def' is used to notify python of the impending function definition, which is what def stands for. From there, you type out the name you want to call your function. It is important to choose a unique name, and also one that wont conflict with any other functions you might be using. For example, you wouldn't want to go calling your function print.

def example():
    print('this code will run')
    z = 3 + 9
    print(z)

Here we've called our function example. After the name of the function, you specify any parameters of that function within the parenthesis parameters act as variables within the function, they are not necessary to create a function, so first let's just do this without any parameters.

Now if we just run this, we see nothing happens. We have to actually call this function to execute, because all we've done so far is just define the function and what it does. To run it, you can either type out the function in the console like so:

example()

This is notably a very basic function. We can put all types of code into a function. We can put if statements in there, run other functions in them, all sorts of things. Also, we can begin to learn how to use function parameters and then later function parameter defaults.

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial