Search TekSocial
Stay Connected

Enter your email address:

(We respect your privacy!)

Or subscribe with your favorite RSS Reader

  

« QR Code Generator | Main | Five Sites to Go to when you're bored »
11:39AM

Programming with Python- def tags

Today we are going to be covering one of my favorite tags in python, the def tag. The best way to think of this tag is being able to give a single word a defined function. Unlike giving a variable a meaning, defining a word as a function gives you the option of giving two set variables equations. This may seem confusing at first, but let’s look at the code and see if I can explain it better.

The Code

1.       def addition(a, b):
a.       print ”We are going to add %d and %d together.” %(a, b)
b.      return a+b

2.       def subtraction(a, b):
a.       print ”We are going to subtract %d and %d.” %(a, b)
b.      return a-b
3.       def multiplication(a, b):
a.       print “We are going to multiply %d and %d.” %(a, b)
b.      return a*b
4.       def division(a, b):
a.       print “We are going to divide %d and %d.” %(a, b)
b.      return a/b

So in this string we have four tags that are defined. Those tags are addition, subtraction, multiplication, division. As you see, the variables are a and b. Meaning the two numbers that will be added together are the two letters. The return function at the end of the string will give the result of what a and b will be. In this case for example, addition. It’s going to return the function a + b. So whatever the two percent d values are is what will be added up for variables a and b. So let’s continue down the code.

5.       print “Let’s do some math with just functions”
6.        
7.       age = add (20, 1)
8.       height = subtract (70, 4)
9.       weight = multiply (90, 2)
10.   iq = divide (100, 2)
11.    
12.   print “age %d, height %d, weight %d, IQ %d.” % (age, height, weight, iq

Now to explain this a bit better, the words age, height, weight, and iq were defined with the functions add, subtract, multiply, and divide. At the end we printed those functions out with their defined variables making it a complete function. There you have it! Now you understand a little bit of the def tags. I hope you found this somewhat helpful or useful.

If you liked this video come check us out!
http://www.youtube.com/user/thedeskgeek/

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>