Thursday, February 2, 2012

Lesson 3 - Writing a Program

Writing a Real Program

In Lesson 2, we learned about integers and functions and a little bit of how you can use them.

Up to this point, we haven't really been writing programs.  We've been using the command line interpreter to try out various lines of code.  Today we are going to break through to the next level and actually run a program.

To do this, you have to know a little bit about the console (the window we've been typing in.)  I'm going to try to show you how to get everything set up whether you're on Windows or Mac.  They're a bit different, so bear with me.

Windows

In Windows, we're going to create a folder in your Documents folder.

To do this, open up Windows Explorer.  (Want to know a shortcut?  Hold down the Windows key, and then the letter "e")



Go to your documents folder and create a new folder.  Type "python" for its name, and open that folder.



Create a new folder called "Lesson 3"; this is where your code files will go for this lesson.

Now open up the Command Prompt (just like we did in the other lessons).  You'll probably start out in your home folder (something like C:\Users\[yourname] on Vista and 7; on XP, it should be something like C:\Documents and Settings\[yourname]).  If you don't start there, don't panic.  Simply type this:

cd %HOMEPATH%

This will get you where you need to start.

The "cd" command isn't Python code.  It just tells the command prompt the directory you want to go to.  "%HOMEPATH%" is a system variable in Windows that is automatically set to your home directory.  (Pretty cool, huh?)

Now if you're on Windows Vista or 7, type

cd Documents

If you're on XP, type

cd "My Documents"

Now, on all versions of Windows type

cd python

and then

cd "Lesson 3"



You now have the Command Prompt set to run the script files you will place in your folder.


Mac

If you're on a Mac, the simplest thing you can do is open up a new Finder window.  Odds are you should find yourself in your home directory, but if you don't, just find the selection on the left-hand side that has your username on it.



The names have been marked out to protect the innocent :)


Anyway, now that you're there, create a new folder and type "python" for its name.  Now inside that folder, create a new folder and call it "Lesson 3"; it will contain the code you will write for this lesson.



Now open up Terminal.  You should automatically start in your home directory.  If you don't, type

cd ~

This will take you there.  (The ~ key is that funny looking key to the left of the 1 key, in case you've never used it before.)  The cd command isn't Python; it tells the terminal that we want to go to the directory "~"; "~" on a Mac is the shortcut for your user's home directory.

Now we'll use this command again to get to the folders we just made.  Type this

cd python

and then

cd Lesson\ 3


Why the backslash(\)?  You have to tell the terminal that the space is part of the name.  This is what is known as escaping, and it's something we do in Python as well (though generally not for spaces).





Ok now we're in the proper place.  Time to get to the coding.




Text Editors

In order to program more effectively, you'll need a good text editor.  A text editor will help you manage your files and format your code so you can read it more easily.  I recommend you use one of the following:  (They're all free by the way)

Notepad++ (Windows): http://notepad-plus-plus.org/download/

TextWrangler (Mac): Available in the Mac App Store (or from somewhere on the web; you'll have to Google it)

And this next one's a big bother to install, but if you want to try it you can.  There are Windows and Mac versions.

Eclipse: http://www.eclipse.org/
(And you'll have to install this with it: http://pydev.org/)

For beginners, I suggest you go with Notepad++ on Windows or TextWrangler on OS X.  It's easier to use and works more like a plain old word processor.  You have to set up projects in order to use Eclipse, and I don't want to get into that right now. You're free to try it out yourself if you want, but I'm going to use TextWrangler for right now.



Once you've installed your editor, open it up.  Create a new file and save it to your python/Lesson 3 folder.  Give it a good name like "lesson3.py"; the ".py" part is to remind you that the file contains python code.  You don't really need it (I don't think), but it's a good practice.

Your First Program

Now you have a nice clean file in front of you.  For your first program, type the following into the editor:

print "Hello World!"

Yes I know we've been through the print statement already, and you're an expert at it now.  This first file is just to show you how to run the program.



Save your file and switch back to your console (ie Terminal or Command Prompt).  I always like to use Command + Tab (on my Mac) to switch between apps really quickly.  (The shortcut on Windows is Alt + Tab.)

You should still be in the "Lesson 3" folder we created above.  (If you're not, do all the "cd" commands required to get you there (see above)).

Now type

python lesson3.py



Congratulations!  You just ran your first program!  And see? It printed "Hellow World!" on the screen just like the command line interpreter would have.

Now go back to the text editor and your "lesson3.py" file.  We're going to add several more lines of code:

string = "This is a string"
print string
num = 42
print num



Save the file (always save your files before you run them; in fact save them OFTEN), and return to the console window.  You could type "python lesson3.py" again, but here's a neat trick: press the up arrow key.



Tada!  The last command you entered appears!  You can actually keep pressing up to bring up the last commands you entered.  This saves a lot of time when you just want to type the same thing over and over again.

With the "python lesson3.py" command typed in, press return or Enter.



All the lines of code in the file ran all at once!  Python runs each line in sequence, and when it's done, it returns you to the console.  This is how we will write all future programs (though we may use the command line interpreter from time to time to test short pieces of code.)

I think that'll wrap it up for this lesson.  Practice getting used to running your ".py" files in the console, and you'll be ready for Lesson 4.

Review

  • Set up a folder called "python" in your home folder.  Create a new folder inside this folder for each lesson.
  • Use the console to navigate to the right folder by using the "cd" console command.
  • Run your code by typing "python" and then the name of the file
  • Python scripts usually end in ".py"
  • When you run a script file, all the lines of code run one right after the other until the end of the file.

1 comment:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    Python Training in electronic city, Bangalore | #pythontraininginelectroniccity #pythontraining

    DataSciencewithPythonTraininginelectroniccity,Bangalore | #datasciencewithpythontraininginelectroniccity #datasciencewithpythontraining

    ReplyDelete