Saturday, October 23, 2010

TAPOS NA!!!!!!!!!!!!!!!

          Hay salamat!!! Natapos ko na rin ang napakahabang blogspot na toh.Makakapag rest in peace na ko. Joke lang! Ayan ah sir!!! Sunday palang tapos ko na... Ibig sabihin umabot ako kasi sabi niyo hanggang weekend.. hehehe... xP..

  
    Salamat pala sa "Sponsor" ko, ang KITKAT. Salamat rin kay ano, tsaka kay ano. Alam niyo na yun... hehehe..

Function

       In the simplest of terms, a function is just a chunk of code that does something. It’s a small piece that you can use to build a bigger program. You can put the piece together with other pieces, just like building something with toy blocks. You create or define a function with Python’s def keyword. You then use or call the function by using its name. Let’s have an example.


    

Easygui

     You can download easygui.py or a zip file that contains easygui.py. To install it, you just have to put the file easygui.py in a place where Python can find it. Python has a list of places on the hard drive where it looks for modules it can use. This can be a bit complicated, because it’s different for Windows, Mac OS X, and Linux. But if you put easygui.py in the same place where Python itself is installed, Python will find it.
    Before anything else, make sure that you imported Easygui by typing “import easygui” at the beginning of the program for the program to be able to run it. In Easygui, print command has a counterpart. It is the msgbox. Also, raw_input has its counterpart as enterbox, input with integerbox. There are also more things you can do with easygui. Try these.
  
      You can also use button box or choice box to have multiple choices.

Looping

     In the last chapter, we learned about conditions and testing and the “if statement”. Instead of counting how many times to run a loop, while loops use a test to decide when to stop a loop. “While loops” are also called conditional loops. A conditional loop keeps looping while some condition is met.



      
You can also jump out of a loop when a certain condition is met. You can use the command “break”.

Conditions

    Programs need to be able to do different things based on their input. Here are a few examples:
ª      If Zai’s answer is correct, add one point to his score.
ª      If he got hit, close the program.
Programs check (do a test) to see if a certain condition is true or not. In the first example above, the condition is “got the right answer.” Python has only a few ways to test something, and there are only two possible answers for each test: true or false. Look at this example.
 


Notice that in the ‘if statement’, there are two equal signs. “=” means assigning, while “==” means checking. Also, don’t forget to put colon after the statement or else it will now work. Aside from if and else, you can also use elif. It means else + if. It is usually used in conditions under conditions.



Basic Input / Output Commands

        Input means getting something, some kind of information, into a program while it is running. Python has a built-in function, called raw_input that is used to get input from the user. Try this example.
    





       
       


      
        We use raw_input for strings or words. On the other hand, we use input for integers. Try this example.




Intro to Programming


     There are a couple of ways to start using Python. One is called IDLE, and that’s the one we will use for now. In the Start menu, under Python 2.5, you will see IDLE (Python GUI). Click this option, and you will see the IDLE window open up. It should look something like the window below.



     
     Let’s give Python our first instruction. Type print “e0w p0whz!” in the end of the “>>>” prompt.



     
In programming, print often means to display text on the screen, instead of printing it on a piece of paper using your printer. Let’s try another one. Type print 4 + 6 in the end of the “>>>” prompt.




Python can do addition, subtraction, multiplication and division. Try these. Type print 1234567898765432123456789 * 9876543212345678987654321 in the end of the “>>>” prompt.


The examples we’ve looked at so far are single Python instructions (in interactive mode). While that’s great for checking out some of the things Python can do, those examples are not really programs. As I mentioned before, a program is a number of instructions collected together. So let’s make our first Python program. First, you need a way to type in our program. If you just type it in the interactive window, Python won’t “remember” it. You need to use a text editor (like Notepad for Windows or TextEdit for Mac OS X) that can save the program to the hard drive. IDLE comes with a text editor that is much better for what you need than Notepad. To find it, select File > New Window from IDLE’s menus. You will see a window like in the figure below. The title bar says “Untitled” because you haven’t given it a name yet.



 
Try this example.






Click “f5” to run your program, but make sure you save your project first. Add *.py to the name of your project.  This will be the output.


 
This is your intro to your programming experience. Be back for other lessons.