So. I'll admit I've been neglecting this thing. That clearly isn't what I meant to do when I started it, was it? Heck no! So, without further ado, HERE'S A PYTHON3 TUTORIAL!!!!
So. Today, I'm going to teach you something about functions and classes. Hopefully it will make sense.
First, open IDLE, and copy and paste this code into it:
def hello():
who = input('What\'s your name?' )
print('Hello, ' + who)
Now press enter a few times, and then do the same with this:
hello()
From here on, I assume you decided to answer the question, and press enter. If you did not do this, then you failed the ultimate test, and are not equipped with the mind-power necessary. Begone with you! If however, you did as I assumed. Good for you. Unlike many Americans, it seems, you have a functional brain! Now if that brain of yours is functional enough, you might be asking why the computer just said hello to you. Do not be alarmed! This is actually what you just told the computer to do! "So how does it work?", you might ask. Well here's a lengthy answer that should teach you something if you are (unlike many people) patient enough to read it:
First, you defined a function called hello(). It's just like when you told your parents about your imaginary friend Bob. In the case of the imaginary friend, you created two things: First, you told your parents about your imaginary friend, and then you told them his name is Bob. Just like doing that, you told Python about a function, and told it it's called hello(). But how did hello() talk to you? Well, unlike your parents and Bob, Python actually believed you! (Sucker!) The next thing you suckered Python into believing is that hello() has a concept of the word "who", and can substitute that word with a name, after he asks you "What's your name?". It can then say Hello and then that name. The way hello() can do this is with a variable called who, and something someone suckered Python into believing called input(). From now on, I'd like you to know that we, the people who sucker Python call these things functions. This function listens to what you say, and assigns what you said to the variable before the =. It also will say what you put in quotes before it listens to you. Things in quotes are called strings. You can do cool things with strings like add them together like numbers. This means that 'Hello,' + 'World' would be 'Hello World'. (What a stupid thing to say! We all know the world doesn't have ears!) The next thing hello() does is uses a function called print() to make Python say 'Hello, ' + who, which means Hello, your_name.
So now that we have suckered Python into believing hello(), we can tell Python hello(), and then our name, and it will act as if hello() is making it say hello to you! (Ha ha!)
Here's another function to confuse Python into doing math for you:
def add(x,y):
return x+y
You might be thinking, "What's all that stuff in the parentheses?" They are special variables called arguments. They are set when you call the function. This means that when you say add(1,2) (That means you should probably type that into IDLE) it assigns x to 1, and y to 2. The next thing add() does is returns a value. To make it short I just told it to return 1 + 2. That's why when you said add(1,2) it said 3.
Now that you know about functions, it's time to get into classes. So copy this code into IDLE:
class Awesome():
"""I'm Awesome"""
def __init__(self):
print("Holy crap, I'm awesome!!")
def who_is_awesome(self):
return 'Me!'
Wait! Copy the first part first, press enter twice, then copy this:
x = Awesome()
x.__doc__
x.who_is_awesome()
Now you did a lot of awesome stuff there. You made a class called Awesome(). Notice that Awesome() is capitalized. You don't technically have to do this, but everyone will hate you if you don't. This is because capitalizing classes discerns them from functions, and makes us (not Python) less confused. Then you put a string there. But wait! Why? We'll see that later, just know it's a special thing with classes. The next thing you did with Awesome() was made some functions inside of it. The first one is special because it's called __init__(). I'll explain that later too. The next odd thing about __init__() is the argument called self. These are special things that we'll get into in the next tutorial. The next thing you did was told Python that x = Awesome(). You might notice that Python said "Holy crap, I'm awesome!!". That was the __init__ function. It executes when you assign a variable to Awesome(). Since assigning variables to classes is so awesome, we call these variables methods. That means x is a method now. The cool thing about methods is namespaces. When you say x.who_is_awesome(), it executes the function who_is_awesome() inside of Awesome(), which is now inside of x. The next thing is x.__doc__. That is what you put in quotes at the beginning of Awesome(). Cool eh?
I hope you enjoyed the tutorial, and I hope I can get myself to keep making them.
Thursday, August 19, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment