Python Functions Cheat Sheet

Posted by

I’m adding python functions to my Python Cheat Sheet series to provide the basics with samples when it comes to using Python Functions in your python projects. Python functions like functions in other programming languages, provide a way to write reusable code that requires multiple executions within a program.

Python Functions

Some important facts to consider when working with Python functions:

  • Functions are declared using the def keyword
  • Function names have () appended with a colon at the end
  • The body of a function is indented under the function (no curly braces)
  • Functions optionally can take any number of parameters
  • Functions optionally can return data to back to the caller

Create a Function

#### function that takes no arguments and returns None ####

def printme():
    print('function text here')
        
printme()

#output: function text here
    
     
#### function that takes two arguments and returns None ####

def addme(num1, num2):
    print(num1 + num2)

addme(5,2)        

#output: 7

      
#### function that takes no arguments and returns a string ####

def nameme():
    return "john"

print(nameme())

#output: john


#### function that takes arguments and returns a value ####

def addIt(num1, num2):
    return num1 + num2

res = addIt(5, 5)
print(res)

#output: 10

*args Parameter

It’s possible to have a function taken a dynamic number of arguments. Depending on the developers goal, it could be expected that a function will not know the number of arguments coming in and it could be random on every call. Fortunately, in Python you can simply use the *args parameter.

#Example - Add numbers that are passed as parameters

def addme(*args):
    print(sum(args))

addme(5,10,15,20)

#output: 50

"""        
Note 1: The args data type in this case is a tuple.  
Note 2: If you would want to use dynamic # of keyword arguments,you would use **kwargs as the parameter.
"""

Non-Keyword Arguments

Non-Keyword Arguments are known as positional arguments. The order you place an argument will map to a functions parameter based on position. If I have a function with two parameters and call it with two arguments, the first argument will map to the first parameter while the second argument will map to the second parameter.

## Non-Keyword Argument Example ##

def greet(fname, lname):
    print(f"Hello {fname} {lname}!")

greet("John", "Smith")

#output: Hello John Smith!

Keyword Arguments

Keyword Arguments are non-positional arguments. When calling a function, it’s possible to map an argument to a parameter regardless of the position of both.

## Keyword Argument Example ##

def greet(lname, fname):
    print(f"Hello {fname} {lname}!")

greet(fname="John", lname="Smith")

#output: Hello John Smith!

I created a simple python program to provide these details as help content in addition to other Python concepts.

https://github.com/RussMaxwell/PythonHelper

Python Cheat Sheet Series

Thank You!

Russ Maxwell