martes, 27 de octubre de 2015

IDLE


      There exist different ways to create a Python file and to run it in your computer. The most complicated ones involve installing a text editor that allows you to create a file written in Python language and save it as a Python file. This way, you will be able to open it as a program but edit it later if something goes wrong. Also it may be necessary to install some kind of shell in which your program will be running after you have done the file.
But there is an easier way to get through this process; through IDLE.

      IDLE is one of the programs you accepted to be installed during the “Terms and Conditions” amd the complements while installing Python 3. You probably didn’t notice, but this program works as an interactive console, a text editor, a shell, and it’s able to run your Python files.
In this post I will show you step by step how to do every of these things.

      To find this program, you can do it by the easy way which is just typing its name on the search bar for Windows. It looks like a Python file because it has the Python logo on the right just as the files created in Python.


      Once you select this option, this window will be opened:


      This is the Python Interactive Shell. It allows you to run small pieces of code such as it is shown in the following example:

      Later on, this can be used for trying new functions and experiment with the results. You can even use it as a calculator (as Ken Bauer recommends):


DO NOT CONFUSE THIS WITH CREATING A PYTHON FILE. THE CODE WRITTEN IN HERE CANNOT BE SAVED AND RUN LATER.  – THIS IS NOT ACTUAL CODE, JUST PIECES OF CODE.

      For actually creating a Python file you need to follow the next steps:

       On the “File” section located in the menu, there is an option which reads “New File”. You need to select it.


      This will open the following window:


      This one just looks like an empty space ready to be used for writing code on it. For the purpose of showing how this works, we will write an easy piece of code that most programmers know:


     This programs asks the user’s name and saves it as a variable called “x”. Later, the program shows the text “Hello, “ followed by the name you wrote when you were asked.

     For saving it we just select the same option we did before from the menu on the upper side; which is “File”. And the we select the option “Save As”.

     After this you can name your file however you want. The only condition is that the name has to end with “.py”, which means this file is a Python file and not just a .txt, .jpg, .png, etc file.


        Now that we have our Python file we just need to run it so we can see if it works. For these we select the option “Run” from the menu shown on the upper side of the window. After doing this we have two different options which are: Running the program from the Python Shell, or running the program from the Run Module. If we select the second option this is what will happen:


      Now we know how to create, save and run a python file, but we still need to know how to edit it. Let´s suppose that I have already closed IDLE, and let’s say we now want to say “Goodbye” to the same person. For this we open IDLE and select the option “File” followed by the option “Open…”.

Now we look for our file and select it. Once it opened you can edit it however you want.


      Once you are done remember to save the changes.


     Now we can run this file and the output will be different from before.


viernes, 18 de septiembre de 2015

If, Elif, and Else

     A condition is an operation used in programs to see if something is true or not. These conditionals help you doing ac action whenever they are true. During this post I will guide you on how to use them properly for the creation of a program:

     First of all we need to understand which ones are these conditionals.

      The first one is called IF. We place it in the middle of out code to see if a condition is true or false. We can only place it once, the rest of the conditions will be evaluated by a different function.

     The second one is ELIF. This one can be used to evaluate many conditions along the same IF. This function can be used as many times as you want, as long as you state the word "elif" followed by the statement you want to evalute.

      ELSE is the last conditional. It is use to stablish what will happen in case none of the conditions above is true.

      Now, we are creating a program that will evaluate a given number and show if it´s positive, negative or 0.

     So we start by asking the user for a number. 


    How we start with our first condition; if the number is greater than zero, then it´s a positive number. So, for this we need to type if followed by the condition that would be x>0. At the end of the line we place a colon ( : ).

     After this, we need the program to let the user know that the number is positive, se we create an output. This output need to be intended to show that is the action the computer should do in case the condition is true.


       If this condition is not true, then it means that the number is zero or a negative number. To differentiate if this number is zero or negative we will need another conditional. But as we explained before, this conditional cannot be an IF conditional, but an ELIF.

      An ELIF conditional works just an IF conditional: you place the word “elif” followed by the condition “x==0” and ending with a colon. Now the condition we will be evaluating is if the number is equal to zero. (It is necessary to place two equal signs to evaluate)


     If none of this conditions are true, then we know that the number given by the user if not zero or greater than zero; therefore, this number is lower than zero. We know that the only numbers that are lower than zero are negative numbers.

     Based on the previous reasoning, if this numbers fails to be true in both conditionals, then it is a negative number.

     Now we know we don´t need to evaluate in any other condition so we can use the conditional ELSE. For this, we only write the word “else” followed by a colon. That, we just need to type the action we want the computer to do.


      And this is what will happen when you run this program:




Reading and Writting Files on Python

     For opening a python file, first look for the file you want to open and make sure it is a Python File. You can prove this by seeing if it has the image of a python on it.


     All the files inside the circle are considered Python File. To continue with demonstration we will open the file with the name “Hello.py”. First, give it a right click. A new menu will open and you will select the following option:


     IDLE is included when you install Python so you should have it. This will help us read the file, edit it and run it whenever we want to. Once we select the option, a new window will open. In this window we can see the code and even edit if we want to.


      If we want to run it we just need to press F5 of select the following option


      After this, Python will open a shell that is currently running your program. This way you can make sure the program you read before works just fine



Creation and Use of Strings

       A string is an extract of text used in a program. This text can be displayed as an output, but it can also be taken from the user as an input. A string is considered to be everything that is written between quotation marks. For example: “House”, “car”, “Anna”, “187.0”, “+”, “.”, etc.


       Yes, punctuation marks, numbers, and even symbols as considered to be strings if they are written between quotation marks. To explain this more efficiently, you can see the following code in which a pair of numbers are considered as strings.


      This is what happens when you run it:


       As you can see, the program was expected to out the sum of x plus y, not to output the text “x+y”. This happened because we typed the function between quotation marks.

        When you want to get a number as an input, you need to place the word “float” or “int” before “input” so the computer knows what type of input is being taken:

X=float(input(“Give me a number”))
Y=int(input(“Give me another number”))

       Just like this, we can type “str” before the word “input” but it is not necessary. Whenever you set an input without placing any word before it, the computer automatically assumes it should be considered as a string. But in case you want to type it anyway, this is how you do it:

X=str(input(“What´s your name?”))