Python 2.7 Tutorial Pt 6

Python PictureIn this video I’ll explain how to use functions in Python 2.5 through 2.7. I’m going to cover how to:

  • Create Functions
  • Pass Values to Functions
  • Create Default Values
  • Except Unlimited Arguments
  • Except Unlimited Key Value Pairs
  • Use Recursion
  • and more…

Like always, the code follows the video. If you have any questions or comments leave them below. And, if you missed my other Python Tutorials they are available here:

Here is Some Code that Demonstrates using Python Functions

#! /usr/bin/python

# Define a docstring a special function attribute
globalVariable = 10

def addNumbers(NumOne=1,NumTwo=1):
‘Adds the numbers passed to it’
return NumOne + NumTwo

def addUndefNums(NumOne=1,NumTwo=1,*args):
‘Adds the numbers passed to it’
finalValue = NumOne + NumTwo
if args:
for i in args:
finalValue += i
return finalValue
return finalValue

def scopeFunction():
mainNumber = 10
print id(mainNumber)
print “mainNumber equals”, mainNumber, “in function scopeFunction”

def changeGlobal():
globals()[‘globalVariable’] = 20

def createDict(**kvargs):
for i in kvargs:
print i, kvargs[i]
print type(kvargs[i])

print type(kvargs)
print kvargs

def factorial(num):
if num == 1:
return 1
return num * factorial(num – 1)

def printNames(first,last):

def main():

# Demonstrate difference between global & local scope
mainNumber = 5
print “mainNumber equals”, mainNumber, “in function main”
print “mainNumber equals”, mainNumber, “after scopeFunction”
print id(mainNumber)

# Force a global variable to change
print “globalVariable before changeGlobal =”, globalVariable
print “globalVariable after changeGlobal =”, globalVariable

# Create a function with a docstring
print addNumbers(1)
print addNumbers.__doc__

# Add undefined number of numbers with a tuple
print addUndefNums(1,2,3,4,5,6,7,8)

# Create a dictionary
createDict(Name=’Derek’, Age=35, YearBorn=1974)

# Demonstrate Recursion
print factorial(3)

Demonstrate Recursion
def factorial(3):
if 3 == 1:
return 1
return 3 * factorial(2)

def factorial(2):
if 2 == 1:
return 1
return 2 * factorial(1)

def factorial(1):
if 1 == 1:
return 1


if __name__ == ‘__main__’: main()

11 Responses to “Python 2.7 Tutorial Pt 6”

  1. Omar says:

    I just wanted to say that, YOU ARE THE BEST who explains things so freaking good, and simple, and awesomely!
    Since the first tutorial through this one and the others to come, I understood everything so clearly, concise, and fast, You just talk about the main and important stuff we need to learn, you rock! I’m right now at my 3rd year taking Artificial Intelligence which we will use Python, instead of Prolog, and this has been the bestest”, because it’s fast and precisely the topics I need to know/learn.

  2. pjordan says:

    these vids are well done, thanks. this may be coming, but i wish you would deal with the issue of calling external modules and procedures.

    thanks also for the code snippets.


    • admin says:

      Thanks 🙂 I’m glad you like them. What do you mean by executing external modules? I cover using external frameworks in future tutorials. Is that what you mean?

    • pjordan says:

      from one module–import and call an external module and/or method–in file
      “”, given an second file somethinng like

      import class_tutorial
      from class_tutorial import animal


  3. Graham says:


    You mention a tutorial on regular expressions – which one is that?


  4. Don says:


    Quick Eclipse question.

    I’m attempting to make new practice files in Eclipse but these won’t run even though I’m seemingly in the PyDev. When I go to the menu and click ‘New”> ‘File”, the file it opens won’t run.

    Dumb question, but how do you start new files to run in PyDev (the library is included in the folder I started). I managed to start one practice file that works fine, but I’d obviously like to be able to create many.

    Many thanks.

    • admin says:

      You have to create your files in the Package Explorer part of Eclipse. Window – Show View – Package Explorer if it isn’t on your screen. Then create a new file by right clicking on the folder named src or default package and then New and then File. I hope that helps. This is a common problem

  5. Jaspreet Singh Kalsi says:

    I m compiling this code in python(2.7)IDLE. but everytime when i run this above code ,python IDLE do not show any output.
    It seems that ” if __name__ == ‘__main__’:main()” is creating some problum because when i make a normal call to function(eg:addNumbers(2,1)).then the code is working…
    kindly help me…

Leave a Reply

Your email address will not be published.