Search TekSocial
Stay Connected

Enter your email address:

(We respect your privacy!)

Or subscribe with your favorite RSS Reader

  

« WD® Gives Consumers A Cloud Of Their Own | Main | How to Setup and Use Virtual Box »
10:56AM

Programming with Python: Functions and Variables 

The title may make this lesson look like a scary mesh of gibberish to some of you. Not literally the title, but the general idea of what we are going to being working on today. But don’t worry; this isn’t a hard concept to get at all. So let’s get started on this lesson of programing with python. Firstly, we have to set up words to define. These words can be defined with both numbers and letters. What I mean by this is, we can have the word “Tom” equal “100” or for every time we have the function “print: Tom” we can have it print out the phrase “Is cool!” Sounds fun right? So, how do we achieve this? To put it quite simply, just as easy as I made it sound out to be. Look at the image below to get the idea of what the code will look like. 

 

I would like to note that with the defining word “Is cool!” I am using single quotes or ‘  ’. The reason to this is because single quotes are an easier way of defining the word. Okay, so maybe there is a better reason to it than that, but all I know is this: they work. Like I mentioned in the previous lesson, I am not a professional, but I am teaching people how I see it. So in this case, you DO NOT need single quotes for numbers, just words. Okay, with that out of the way, we now have tom meaning is cool, Jim means 100, and Larry means not the cable guy. Now we have minor functions. Nifty huh? So now what are we going to do with those functions? Why, we are going to print them out in the simplest way possible. Look at the image below. 

Although in this case I am on my windows computer, the concept still applies for the mac lesson. So here we have it as simple as one two three. But I wouldn’t want it to be all that simple would I? So now I am going to show you something a bit more complicated. It won’t make much sense at first, but just let it sink in for a moment and you’ll kind of get it at first. Now, we are going to make a small program explaining more about ourselves. I will type out the code below for you to type down, don’t copy and paste but get a feel for how programing works. So, let’s make a code to tell us more about you.

 The Code

  1. my_name = ‘Ryan’
  2. my_age = 21
  3. my_eyes = ‘grey’
  4. my_hair = ‘brownish blonde’
  5. my_teeth = ‘white’
  6. my_height = 72 #inches, and remember the pound sign is a comment tag
  7. my_weight = 207
  8.         
  9.     
  10. print “My name is %r” % my_name
  11. print “I am current %s years old and am %s inches tall” %(my_age, my_height)
  12. print “I have %r color eyes, and %d color teeth. Depending on the coffee that it.” % (my_eyes, my_teeth)
  13. print “My hair is %r” % my_hair
  14. print “I weight %s pounds” % my_weight
  15.     
  16. #Now this part can be a bit confusing.
  17. print “if I add %s %s and %s together, I get %s” % (my_age, my_weight, my_height, my_age + my_height + my_weight)
  18.  #Did you see what happened there?

Now take a look at the image below, this is what you should see. 

In short, the %r is the word defined functions, while %s is the number based functions. Words and numbers must be separate when given meaning, or else it will be a big mesh of confusion. Adding both words and numbers I like meshing up numbers with letters. It can look like “word938ofjwejffoe” and then where are we? Wonder land. That’s about it for this lesson. I did briefly go over user input, and some lines of text code. In which I will link that to you below all of this.

User input is nothing more than raw_input() with a prompt inside the brackets. But we will be using this more in the future, so don’t stress too much on it at the moment. Worry about getting the basics down first.

Again, if you found any of this useful, thanks for reading and watching. If you want to study a bit further, make sure you check out this link: http://learnpythonthehardway.org/book/ I promise you won’t regret it.

I want you to give this lesson a try on your own using your own words. Get a feel for what you’re doing and try to understand the concept a bit better. Practice this for the remainder of the week and we’ll continue next time.

ONE MORE THING

You might run into errors, in fact I would be surprised if you didn’t. But if you do, pay attention to what your terminal is telling you. Look at your code more carefully and see to it that there is no symbol out of place or even some declaration that was just missed. Be aware of it, because it will happen down the road at some point. 

 

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>