You came here to learn Python. So let’s get right to it, shall we?
I’m gonna assume you have Python installed. If not, head over to mothership and get it. Just follow the instructions on the website.
Now, we can run Python through the shell by opening up the terminal and typing python, or we can get ourselves a nice IDE (Integrated Development Environment). I think the latter is in order! No one programs strictly through the shell. And if you do…how on earth do you manage to handle large programs? The answer is that you don’t. You use an IDE.
A list of IDEs can be found here. I recommend Wing or DrPython. All right, now that you’ve grabbed yourself an IDE, let’s finally begin! Open it up and stare at that blank page. Listen to it speak…can you hear that? It’s saying it wants to be written on. It’s almost like it read our minds because we were going to do just that, weren’t we? Yes, we were! Following tradition, we’re going to make a program that outputs “Hello World!” to the screen. So, let’s type this into our IDE:
1 | print "Hello World!" |
After you’re done, run it and see what you get. Woot! Your first program! However, it isn’t very exciting, is it? Let’s spruce it up, shall we?
1 | print "Hello Aleks! You rock my world!" |
Much better. By now you should have realized that you can display anything you want by using the print statement. You could even output lyrics if you really wanted to.
1 2 3 4 | print "I love Aleks. He is so great." print "Every girl wants to be his mate." print "Look, you can even see them waiting in line." print "What a guy! I think we should build him a shrine." |
Catchy, isn’t it? Now let’s move on to variables!
Variables
Think back to the kind of math you did in elementary school. You’ve been using variables all along. Really. When you were asked to find the value of X on that test, what you didn’t know (or probably did) is that the X is actually a variable. A variable is like a container. You can put things inside of it. Numbers, letters, you name it. You’ve probably already figured out that you can’t leave a variable empty on a test. Well you can if you like zeros. I personally tend to avoid them.
In Python, variables also act like containers. If you’ve programmed before, Python variables don’t need to be declared. If you have no idea of what I just said, don’t worry about it. The best way to learn is by example, so let’s see one.
1 | myVar = 1 |
When you run this, nothing happens. Or, at least that’s what it seems like. However, behind the scenes (things always seem to happen behind the scenes, don’t they?) Python did something. What happened was that a variable with the name myVar (short for my variable, if you’re a little slow) was created and assigned the number one. You can name variables whatever you like except key words Python uses. For example, you can’t assign the number 1337 to the variable name print:
1 | print = 1337 |
The above code doesn’t work because print is one of Python’s keywords. You also cannot name variables starting with a number or have any special characters anywhere. Only letters may be used at the beginning, and numbers and underscores everywhere but the beginning. Later on you’ll learn that you can use underscores at the beginning as well, but for now don’t worry too much about that, and don’t name your variables starting with underscores. Trust me on this one. Contrary to popular belief, I know what I’m talking about. Below is an example of valid variable names:
1 2 3 | theAnswer = 42 the_answer = 42 yoda = "Jedi Master" |
You may have noticed that the variable yoda doesn’t have a number assigned to it. What blasphemy is this!? What yoda contains isn’t a number but a string. Like a number, a string is just a different type of data. We’ll talk more about data types later on.
Let’s take a look at another example:
1 2 3 4 5 6 | myVar = 1 myVar2 = 3 myVar3 = 3 myVar4 = 7 total = myVar1 + myVar2 + myVar3 + myVar4 print total |
If you can tell what the above code does, then you’re already one step ahead of the game. But if you can’t, I’ll tell you anyway since I’m such a nice guy. We stored the numbers 1, 3, 3 and 7 into myVar1, myVar2, myVar3 and myVar4 respectively. We then added all those variables together and stored them in another variable called total. We then printed the variable total to the screen. The output of this program should be 14. If you’re getting something different then your computer hates you. You can fix this by showing it some love, or by making sure you typed everything as you see it in the box above. Also, I know that myVar2 and myVar3 are the same thing, you don’t need to tell me! There is a method behind the madness, so keep reading, you’re almost done.
Before moving on, I’d like to show you another example that will introduce data types rather nicely. Modify that code so all the numbers are surrounded by quotation marks. So the four numbers, 1, 3, 3 and 7 should all look like this: “1″, “3″, “3″ and “7″. In other words, you are turning the numbers into strings. This is the only thing you should change. Now run the program. You will notice that the output is “1337″. This is because strings are added by just sticking to one another. With that, we move into the final section of this tutorial…data types!
Data Types
There are many different data types available in Python. We’ll only look at three of them for now.
There are integers. Integers are just regular numbers without decimal points. Integers can also be negative. The program that gave us the output of 14 used integers.
There are also floating point numbers. These can have decimal points, and can also be negative.
And finally, there are strings. Strings are just words, but they can also be a bunch of characters.
The important thing to know with data types is how to work with them. For example, let’s divide 10 by 3.
1 | print 10 / 3 |
You’ll notice that the answer isn’t 3.33 (repeating, of course), but is instead 3! Why is this? Well, remember that integers are whole numbers. As a result, everything after that decimal point is thrown away. So 3.33 becomes 3, and an arbitrary number like 42.994328934 would become 42. To get Python to give us a more accurate output, we must include a floating point number in the calculation.
1 | print 10 / 3.0 |
Much better. One last thing you should be aware of is that you cannot add strings with integers or floating point numbers. However, you can do certain things like:
1 | print "I'm a cool string." * 3 |
Doing so will print three instances of this string onto your screen. Don’t be afraid to play around with different data types. You won’t break anything. The worst that could happen is that some error could rear it’s ugly head at you. Just rear your ugly head back. You might scare it away…or you could fix your code. Both are acceptable solutions.
Conclusion
And that’s it! You know what variables are, and can work with them at a basic level. You don’t know too much about data types, but you are certainly aware of them, and that’s all that matters at this point in your Python career. I really had a great time writing this, and I truly hope it helped someone. If you have questions, comments or concerns please don’t hesitate to send me an email ![]()