Tag Archives: Python 2.7 Tutorial

Python 2.7 Tutorial Pt 7

PythonIn this video, I will explain the basics of Object Oriented Programming with Python. Understand that a Class is the blue print that defines what Attributes (variables) and Methods (functions), each object it builds must have.

Each Object that is created is said to be an Instance of the Class that built it. I’ll also go over what Encapsulation is.  Encapsulation is how you hide information  and constrain a user of your class to use it in the way you define. And, finally I teach you how to enforce Encapsulation by making your Attributes and Methods private.

Like always, a lot of 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:

Continue reading Python 2.7 Tutorial Pt 7

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
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()

Python 2.7 Tutorial Pt 5

Python How ToIn this Python Video Tutorial I focus on looping in Python 2.5 through 2.7. I explain how to use the while loop and the many ways to use the for loop.

Along the way I explain how the in operator is used in Python and the range function.

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:

Continue reading Python 2.7 Tutorial Pt 5

Python 2.7 Tutorial Pt 4

Python 2.7 TutorialI continue the Python 2.7 Tutorial by explaining a couple new things. I first go over how to turn your Python code into an executable application.

I then cover Python Conditionals. Conditionals are used to perform certain actions under one condition and other actions based on other conditions. In Python you can do this either with an IF Statement, or with something called a Conditional Expression.

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:

Continue reading Python 2.7 Tutorial Pt 4

Python 2.7 Tutorial Pt 3

PythonIn part 3 of my Python 2.7 Tutorial, I explain how to use the Dictionary, Print method and how to work with String methods.

If you haven’t seen part 1 or 2 definitely look at them first or you’ll be very confused Python 2.7 Tutorial Part 1 and Python 2.7 Tutorial Part 2.

All of the code I use in these tutorials follow the video and can be used however you’d like.

If you have any questions or comments leave them below.

Continue reading Python 2.7 Tutorial Pt 3