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.

No comments:

Post a Comment