Tuesday, June 25, 2013

Python Programming Tutorial #3

In the previous tutorial, we talked a little bit about printing stuff. But that's just really basic. Let's get deeper.

We can print multiple stuff together:
x = "James"
print "My name is", x

If you run this, you will get "My name is James". Try it out!
Also, since we talked about running Python scripts...if you follow my instructions, now you will be using IDLE to write and run your scripts. But you know what? Forget about IDLE. It may be convenient, but it will limit you in a lot of ways. So, from now on, we will be using the console. For starters, go ahead and download a text editor. You can download whatever you like, but I recommend Notepad++ for Windows and TextWrangler for Mac. If you are on a Windows machine, use the powershell (you can find it by searching "powershell" in the "Start" menu), and follow these instructions (check the "Windows" part) to make it work with Python. Navigate to the directory of your script using the "cd" command. Do the same on the Mac, using Terminal. If you're on a Mac and your file is on the desktop, you need to type "cd Desktop". It's more or less the same thing for Windows users. If you're having trouble with this, take a look at this link. Once you're in your file's directory, simply type "python yourfilename.py". Replace "yourfilename.py" with your file's name (duh) and include the ".py" at the end. Press enter, and your program is running.
I apologize for the hard transition from the nice and comfy IDLE to the cold and cruel console, but that's just the way it is. You may not realize it yet, but when you're getting more familiar and advanced with programming, you can't depend on IDLE. Instead, the console can give you everything you need. So bare with me on this one.

So now let's just go back to our .py file. Let me show you a couple more ways to do this:
x = "James"
print "My name is " + x # as you can see, the + does not put a
                        # space between the strings like the ,
print "My name is %s" % x

But wait...what happened there in the last line?! I guess it's time to talk about some "special characters", then. These characters are used in most programming languages, so expect to see them everywhere you go. The one you used is an "s" because it stands for "string". What you may have noticed it that it replaces "%s" with the string that comes after it; in our case, x, which is "James", of course. There are other "special characters", though, like "%d", which stands for "decimal integer" (or at least that's what I think, lol). Google them to find out more about them. There are also some things called "escape characters". Here are a couple of them:
# run these and find out what they do
print "This is on one line\nThis is on the next line"
print "This is here\t\t\t\tThis is kinda far from there"

# also, check this out
print 'Did you know that you can have %d "special characters" %s?' % (2, "at the same time")

Allright, you are most likely confused, so let's explain some stuff: Remember the backslash (\) characters you saw back there? Those were escape characters. The computer identifies them thanks to the backslash and treats them differently. "\n" is called the newline character, and it basically does whatever the enter key does on a text editor. The tab character (\t) on the other hand, simply adds a tab and indents your text.
print "You can also escape \"double quotes\""
print 'You can escape \'single quotes\', too'

You've got to be careful with combining (concatenating) strings, though. For example:
print "I have " + 3 + " apples" # this won't work
print "I have", 3, "apples" # but this will
print "I have " + str(3) + " apples" # this will work, too

What you need to understand is that you can't concatenate different data types, for example, strings and integers.

Wednesday, June 12, 2013

Python Programming Tutorial #2

So, since you've got your version of Python up and running, let's continue.

When you download a version of Python, it usually comes with three or four other programs; we don't need them. The only program we'll be using is the IDLE. So, open that up and let's get things running.

It is a tradition in programming that says that, when you learn a new language, the first program you're going to write must print "Hello, World!" on the screen. It's not necessary, but let's do this, so we can talk about some stuff.

Alright, here is a Python Hello World program:
print "Hello, World!"

[NOTE: Remember when I said that there would be differences in Python 3.0 and newer? If you use a version that is newer than 3.0, when you try to run this program, you will get an error, because they changed the syntax of the print statement to "print("Hello, World!")". Watch out for that.]

As you can see, there is the "print" function, that tells the computer to print what follows in the console, as well as some text, enclosed in quotes (""), which we call a "string" (I will talk more about that in a second).

Now, we can also add comments to the code. Comments are very useful, because they are just notes you leave to yourself or other programmers, and the computer just ignores them. Comments in Python can be added using a hashtag (#). Here is our commented Hello World program:
# prints "Hello, World!" in the console
print "Hello, World!"

Now, since we successfully wrote our first Python program, let's talk about variables and data types.

The definition of a variable in programming is very close to that one you learned in school. I like to think of a variable as an empty box, which can store one value. Later on in our program, we can change its value, but first we must declare it:
x = 0

There are some things you must watch out for when declaring a variable. First and foremost, you cannot use any keywords reserved by Python (for example, you cannot have a variable named "print", because print is a function of Python). You also cannot have a variable that begins with a number. A safe way to name a variable, is to think of a name that starts with a letter of the alphabet, contains letters of the alphabet, "_" and numbers ONLY. If your variable name consists of two words, there are many ways to declare it. For example: "myVar", "my_var". Also, keep in mind that Python is a case-sensitive language. That means that "myVar", "Myvar" and "MyVar" are all different variables.

But there are many data types a variable can support:
  • An integer (..., -3, -2, -1, 0, 1, 2, 3, ...)
  • A float (A floating point decimal number, like 3.2810378)
  • A character (A single letter, like "e")
  • A string (A sequence of letters, like "Hello, World!")

There are also built-in functions that can be used to change a data type to another, if possible. For example, test these:
print int(3.7)
print int(3.1)

x = 5.282719
x = int(x)
print x

# many people suggest that you should turn your numbers into
# strings if you plan to just print them
print str(0)

Python Programming Tutorial #1

Hello! So, since I have successfully finished my course in Python, and I have learned lots of useful stuff in that language, I've decided that I'll make some tutorials. So, here we go.

Alright, for starters, I think it's better to analyze some basic stuff. Those of you who are reading this and are more familiar with the basic stuff, just skip this. For the rest of you though...

Firstly, what is programming? Programming, according to Wikipedia, is "the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages (such as C++, C#, Java, Python, Smalltalk, etc.). The purpose of programming is to create a set of instructions that computers use to perform specific operations or to exhibit desired behaviors. The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic." A simpler way to put it is to say that programming is the way to give instructions to the computer to do things. We program computers using algorithms. An algorithm is a list of instructions the computer must follow (much like a shopping list, or a recipe). Now, to code or program (it's the same thing), we need four things:

1. A source code editor. The "source code" of a program is the code itself. This is to be seen by any people working on the project; that means that the source code is invisible to the users. A source code editor allows you to write code, and usually has some useful features, like key-coloring some important words, etc. You can basically code with Notepad, that comes with Windows, and just save your file with the proper extension (for example, a file with an extension ".c" would be a source code file written in C), but there are many free good source code editors, with a variety of useful features. An example of a multi-language source code editor is Notepad++

2. A compiler. So, you've written your code, and you're ready to see if it works. Unfortunately, that's beyond a source code editor's capabilities. In order to execute our code (aka run it), we need a program called a compiler. Basically, what this program does is that it "translates" the source code, that is words that we can understand, to binary, code that the computer can understand (of course, the translation procedure is much more complicated, and I won't go into more detail about that, as it will just disorient, discourage and confuse you). I don't think that there are any compilers who do just that; instead I think that there are many programs which include a source code editor, a compiler and debuggers (which we will talk about later) all together.

3. An interface designer (optional). From a certain point of view, we can see that there are programs which do not offer any visual feedback to the user. They are, instead, more practical. For example, let's say that you write a program that fixes an annoying bug in Firefox. Chances are, this program will just execute, kill the bug, and stop, all that in the console. There's no need for such a small program to have an interface. But if you are building a big program (or one of considerable "size"), you probably want this. Many programs/languages, like Visual Basic or Xcode make this easier, by including this to the pack of programs, as I said before (source code editor, compiler, debuggers). Other programs/languages include that in other parts that you need to import, like Python (these are modules, they will be explained later). Either way, an interface designer is essential in order to make your programs more attractive to the user.

4. Debuggers. These are programs that have to do with testing your code; they are pretty much like doctors. Most debuggers just check your code for errors. This is useful, because if you do not debug your program, and you have an error, it will just close without telling you why. Instead, when using a debugger, you can see where the error is, and why is there an error, which is very helpful. Other debuggers feature things like measuring how much CPU-power-expensive is your program, how long does the code run for and things like that. These things aren't, in most cases, necessary.

When talking about debuggers before, I mentioned errors. You must know that in programming, there are two types of errors: Syntax errors, and logical errors. As a proper language, every programming language has its own syntax and grammar. A syntax error is when you mistype something. For example, in the english language, a syntax error would be "I liek apples", or even "like I apples much very". A logical error, though, is when your code runs ok, your syntax and grammar are correct, but the outcome is not what you expected or what you wanted. Syntax errors are more serious, since you can't even run your code if you have them, but as you begin coding you will see this: Syntax errors may be serious, but they are solvable (by searching in Google, forums of the programming language you are using etc). Logical errors are very, very nerve-wracking, because you can't be sure they can be solved. And even if you do solve them, in most cases you will spend from hours to even months to do so.

But let's get back on track. In this first lesson I won't be talking about any actual Python code, I'm just going to tell you how to download and install it. It can be downloaded from here, and it is available for all platforms (I think). If you need any specific instructions on how to download and install Python, just search the website or Google for answers, although it's pretty easy to do it yourself. Also, watch out for this: If you go ahead and download the latest version, you may need to watch out for some things. Since Python 3.0, there have been some changes in the syntax of the language. I am using Python 2.7.4, because it is compatible with most modules (I'll explain what these are on future lessons), but you can download any version you want, and if you get errors, you can always Google them, or even ask any questions here. So, do that: Download a version of Python, and stay tuned for the upcoming tutorials!