Coding in Python 11 - Functions

From LearnLinux.tv Wiki
Jump to navigationJump to search

Overview

In video number 11 of the Coding in Python series, functions are explored.

Relevant Links
Original Video


Commands Used in this Video

Executing the same code more than once (the wrong way!)

#!/usr/bin/python3

mynum1 = 10
mynum2 = 30
print(mynum1 + mynum2)

mynum1 = 10
mynum2 = 30
print(mynum1 + mynum2)

Creating a function

#!/usr/bin/env python3

def my_function():
    mynum1 = 10
    mynum2 = 30
    print(mynum1 + mynum2)

my_function()

Repeating a block of code (using a function)

#!/usr/bin/env python3

def my_function():
    mynum1 = 10
    mynum2 = 30
    print(mynum1 + mynum2)

my_function()
my_function()

Function (with a doc string)

#!/usr/bin/env python3

def my_function():
    """This is a doc string.
    This can be useful.
    It's pretty cool, isn't it?
    """
    mynum1 = 10
    mynum2 = 30
    print(mynum1 + mynum2)

my_function()
my_function()