In this video I’ll explain how to use functions in Python 2.5 through 2.7. I’m going to cover how to:
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
else:
return finalValue
def scopeFunction():
mainNumber = 10
print id(mainNumber)
print “mainNumber equals”, mainNumber, “in function scopeFunction”
return
def changeGlobal():
globals()['globalVariable'] = 20
return
def createDict(**kvargs):
for i in kvargs:
print i, kvargs[i]
print type(kvargs[i])
print type(kvargs)
print kvargs
return
def factorial(num):
if num == 1:
return 1
else:
return num * factorial(num – 1)
def printNames(first,last):
pass
def main():
# Demonstrate difference between global & local scope
mainNumber = 5
print “mainNumber equals”, mainNumber, “in function main”
scopeFunction()
print “mainNumber equals”, mainNumber, “after scopeFunction”
print id(mainNumber)
# Force a global variable to change
print “globalVariable before changeGlobal =”, globalVariable
changeGlobal()
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)
createDict(Cust1=(‘Derek’,35,1974),Cust2=(‘Sally’,25,1984),Cust3=(‘Paul’,15,1994))
# Demonstrate Recursion
print factorial(3)
“”"
Demonstrate Recursion
def factorial(3):
if 3 == 1:
return 1
else:
return 3 * factorial(2)
def factorial(2):
if 2 == 1:
return 1
else:
return 2 * factorial(1)
def factorial(1):
if 1 == 1:
return 1
“”"
if __name__ == ‘__main__’: main()
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.
Thank you so much. I’m very happy to help in anyway that I can. Nice comments like yours keep me going
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.
cheers.
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?
from one module–import and call an external module and/or method–in file
“run.py”, given an second file class_tuorial.py. somethinng like
import class_tutorial
from class_tutorial import animal
reload(animal)
thanks
Derek
You mention a tutorial on regular expressions – which one is that?
Graham
I did a couple. Here is part 1 of my regular expression tutorial series using Python Python Regular Expressions
If you go here and search for regular expressions you’ll see a ton more New Think Tank Sitemap
Derek:
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.
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