• Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Trading Card Game 2 protagonist in the poll by clicking here.
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Programming Python lesson 1 (by me).

  • 175
    Posts
    19
    Years
    • Age 35
    • Seen Mar 29, 2007
    Welcome to programmers-ville, population: you.

    Okay. I've started this series of lessons to use as beta test writings for my website and to help you guys make some kick *** games and learn things that will especially good for making stuff like pokemon systems, real time mmorpg's and even 3D games. Yes even real time war games and all are possible. I'll teach you how by teaching you the art of programming in the python programming language. It's hella easy seriously. Come in and feast your minds on what will soon give you real power, flexibility and control over your game creating. The first lesson is really simple though and we wont really get down into the nitty gritty of python until lesson2 but none the less this lesson includes vital info and includes a small intro on writing your very first python program. This series will hopefully go on until I feel I've taught you sufficiently to make real programmed games. So without further ado let's get started.

    Python Tutorial 1

    Installing Python

    The first step you will need to take is to actually download the python interpretor to your computer. You can get it from www.python.org. The latest stable release is version 2.4.3 which is the release I recommend (obviously because it's a stable release). If you are using microsoft windows then you can just download the python 2.4.3 Windows Installer from the download page and install just like any other normal windows install. If you are using a mac then you probably already have it installed but it is probably an outdated version. See https://homepages.cwi.nl/~jack/macpython/ for more info on getting the newest version for your mac. If your on linux you probably already have it installed anyhoo and it mgith be a fairly up to date version. You can get the newest version though usually through your linux distros update method. But anyhoo once you have got it installed then were good to go.

    Interactive Python

    There are two ways to run interactive python (at first I will only go over the mian way). Firstly though... what is interactive python? It is where you can test small snippets of code by typing them in and then getting immediate results of what the snippet of code will do. For example start the interactive python prompt using one of these two ways. Open a command prompt window (for windows users who aren't always familiar with command line/DOS you can do this by going on Start > accessories > command prompt. You can also go on run and then type in cmd.). The type in python and this will start interactive python. The screen will look a little like this.

    [PokeCommunity.com] Programming Python lesson 1 (by me).


    Now when you type some python code into the interactive python prompt you will get immediate results of this. To give you an example of this let's put in a line of python code to show this in action. Type in the following line.

    print 'Hello, World!'

    This is an example of the print command. The print command prints whatever you feed it to the screen. In this case we fed it the string 'Hello, World!'. A string in programmers terms is a string of letters (or a few words or what you will). Such as 'Hello, World!'. In python a string is made by putting either double or single quote marks around the string you want to make. For example to make the string Hello, World you could use 'Hello, World!' or "Hello, World"!. I hope you got that. But anyhoo the interactive python prompt will print Hello, World! to the screen. So now you at least know your first bit of python. The print command. So now you know about both the print command and the interactive python prompt, but... I'm sure you would rather actually get to making programs instead of testing snippets of code with the interactive python prompt. But at least you now know where you can go to test little bits of python.

    Making your own python programs

    Okay first to make python programs you are going to need a basic text editor. Not a word processor like Microsoft Word or OpenOffice Writer. Just a simple text only editor like notepad (windows), simple text (macs) or Kwrite (KDE Linux). They will do for this example but at the end of this lesson I will go over getting improved text editors that are made specifically to make the life of a programmer easier for you. But anyhoo open up a plain text editor and enter the following code.

    #!/usr/bin/python
    # this is a comment


    print 'Hello, World!'

    Save it to your computer somewhere and make sure you give it the file extension .py. For this example just call it hello.py or something along those lines. The file is now a python program. Easy, huh? Well I have already gone over the print command and strings so now what do the first 2 lines of the program do? They are comments. A comment is just something that is ignored when the program is runnign but are basically just there to help you document your code so when you look through your code you can have little reminders of what you intended things do. The first line though isn't just a normal comment. It is widley nicknamed the unix shebang. This is because it is a line you should put as the very first line of all your python programs because it basically helps people who are using unix based operating systems (because the line helps specifiy what interpretor for unix to use). You don't need to remember it off by heart but even if you don't try to you will easilty remember it after a while (cause it's not really that complicated). But anyhoo now you know the code does I'm going to tell you how to actually run a python program.

    Running a python program

    Go back to your command prompt (not the interactive python just the normal command prompt) so we can run our program (don't worry, nearly all programming languages programs you write when getting started will use command prompt, not just python). Now this is going to be a quick lesson in command prompt (very quick just so you know how to switch folders so you can navigate to your python program).

    [PokeCommunity.com] Programming Python lesson 1 (by me).


    Above is a picture of my folder where I keep some of my python programs. You can see the file we just made (hello.py) there. At the top in the address bar you can see where the folder is on my computer. This is the location you need to go to in the command line so then you are in the same folder as your python programs (on windows the path will be something like C:\Documents and Settings\User\My Documents but obviously User will be whatever your user name is, but read ahead because its not the same for all users, non XP users I think it may be different). But anyhoo the file path is in the address bar anyhoo so open windows explorer or konqueror or the likes naviaget to your folder where you put your python stuff and have look in the address bar (if the address bar isn't there and you are on windows. Go to something like view > toolbars > address bar and make sure you have unlocked all tollbars and dragged the address bar out into view). Then in your command prompt type the two letters:

    cd

    Directly followed by the address in your address bar (make sure you spell everything correctly) and press enter. The command prompt will now go to that folder so you can run your python programs. Anyhoo type the name of the file into the command prompt (remember the .py and if you are on linux or a mac then type python before the filename) and it will run. Simple as that. Long explanation for something so short. Yes I know... I'm terrible. But that's it. Naviaget to the folder and type in the name preceeded by python on linux or macs (on windows just typing the name is fine).

    Better text editors

    As promised I'll give a quick explanation of better text editors. Python comes with one called IDLE which you can find in the normal way you find normal applications on your system (IDLE when first opened up will be a fancier looking interactive python but if you go to file > new then it will bring up a text editor. For windows users I highly recommend notepad2 though. It has really good python syntax highlighting and line numbers (for those of you like me who like seeing the exact number of lines your code is). You can get notepad2 at https://www.flos-freeware.ch/notepad2.html. Just unzip the file and click on the notepad2 icon and notepad2 will open up.

    Well that's it for now. Sorry for not really getting nitty gritty for this lesson but you can be assurred next lesson we will be getting our heads down and writing some code (plus it's a bit of a short lesson cause it's after midnight here in the UK). And sorry for those of you who already know how to use the command prompt, this was probably a very short lesson for you but I had to take into consideration most average computer users never even knew it existed. Well until next time this is stuart and I'll see you round.
     
    Python's extremely object orientated in the fact that absolutely everything is an object.
     
    Back
    Top