viernes, 30 de octubre de 2015
miércoles, 28 de octubre de 2015
Recursion
In some
programs we need to get a result that consists on repeating the same algorithm
certain amount of times. For obtaining the result we need to obtain the result
of the same problem in smaller instances.
For
example:
We want to
obtain the result of certain number raised to another number. This is the same
as multiplying the first number by itself during the second number of times.
But also,
this is the same as multiplying the first number times the first number raised
to second number minus 1.
For
example:
5^3 is the
same as:
5*5*5
Which is
the same as:
5*5^2 which
is the same as: 5*5*5^1 which is the same as 5*5*5*5^1
Now we just
need to know what 5^1 is equal to.
What we
just did is what we call recursion. We want to obtain the result of a problem
by solving the same problem in smaller instances. It can be applied to code in
this way:
And it
gives us the same answer:
Another
example is a number in the Fibonacci series.
This series
start with the value “0”, followed by “1”. After that, every number is equal to
the sum of the two previous numbers. This means that the third number will be
equal to 0+1 which is 1. And the fourth number will be 1+1 which is 2.
This can be
solved used recursion.
If we get
asked for the tenth number in the Fibonacci series, this will be the sum of the
ninth and the eight numbers.
And the ninth
number is the sum of the eight and the seventh.
The eight
is the sum of the seventh and the sixth…
And so on
until we get to the first two number, that are the ones that we actually know.
Here is the
series so you can prove the answer is correct
( 0, 1, 1, 2,
3, 5, 8, 13, 21, 34, 55, 89 )
Loops with While
A “loop” is
how we call a process that repeats itself as long as some condition is true. As
soon as this condition becomes false, the process continues as the loop is
being left behind.
We state this
condition by the usage of the word “while” followed by the condition that needs
to be true in order to initialize the loop.
For
example:
As you can
see, the condition is followed by “:”. This is the only way for the computer to
know which is the condition that needs to be true in order to continue with the
loop. The condition will always be the
one between the word “while” and “:”.
The
evaluation “!=” means “ is not ” and “==” means “is”.
The
previous conditions which is “x != 0” means “x is not 0”. But if we had “x==0”
it would mean “x is 0”.
We can also
use “<” (smaller than), “>” (greater than), “<=” (lower or equal to)
and “>=” (greater or equal to).
The actions
being done inside the loop will be held in the following lines. These need to
be intended to be considered as part of the loop.
What this
program does is that is asks the user for a number, this number is stored in
two diferent variables which are “x” and “x2”. The first one will remain the
same and the second one will be altered during the loop.
If the
value given by the user is not 0, then the program will enter the loop.
In this
loop, “x2” will be altered until it becomes “0”. The number of times this
variable is being changed will be determined by the variable “count”.
The next
line is not intended, which means is not part of the loop. Therefore, this
action will take place once “x2” is 0 (which is the condition that breaks the
loop).
In the end, what this program does is to
determine how far from 0 is the number the user gave. After that, it prints the
equation that need to be done to this number in order to turn it into 0.
Another
example more complicated than the one stated before ca be this one:
In this
program, the computer chooses a random number between 0 and 100 and it asks the
user to guess it. The user has an unlimited number of tries to guess the
number.
The loop
initializes when the number given by the user is not the same as chosen by the
computer and it finishes once the user guesses the number. During the loop,
there is a variable called “count” that will count how many times the loop is
repeated; which is the same number of tries the user had.
Creating and Calling Functions
A function
is a piece of code that has been written some time before it is being used.
Some of these already come with python just as the case of the “Random”
function that has been used in the following example:
These
functions can be created by the programmer in the same code in which he/she
will be using it.
To create a
function we need to start defining it. To define it we write “def” followed by
the name of the function and the parameters that will be used in this between parentheses.
The next
line of code needs to be indented as this is what the function is supposed to
do. This function will print the primary colors:
If we try
to run this program, this is what will happen:
As you can
see, this program does not print anything. The reason for this is that we have successfully
created a function; however, we never called it. So this program knows that it
has to print “Red, Blue and Yellow” at some point, it just doesn´t know when.
So we need
when we want the function to be called. In this case the function will be
called if the following condition is true:
Note that the
line in which we state the condition is not intended. This means that this like
and what is followed by it is no longer part of the function we called “Colors”.
In this
program, the user is asked if he/she wants to know which are the primary colors
and it receives an input in the form of a string. If the string it received is “yes”,
then the function is called. This is what will happen if the conditions stated
below are true:
This is another
example of a created function. This function has an argument called “x”. This
argument is the name of a person. The function outputs the text “My name is”
followed by the value given to "x".
Before
calling the function we need to define what will be the value of “x”. After
that, we call the function by typing the name of the function followed by
parentheses.
After running it, this is what will happen:
We can also
change what the function will print by changing what is being witten between
the parentheses. For example, if instead of typing “name(x)” we type “name(x*4)”.
The
argument this function will be taking as “x” is not “Frida”; it is “Frida”*4,
which is “FridaFridaFridaFrida”. This is what will happen:
Another way
to create a function is the one shown below:
The
variable “stars” was defined inside the function; however, it can only be used
inside the function. For printing variables that were defined inside the
function we need to type “return” followed by the name of the variable at the
end of the function.
This way,
the program can use the variable correctly even if it is inside a function:
As you can
see, this program asks the user for a number, then it print the same quantity
of “*” that the user stated.
If we
decide to omit the “return” part, the result will be totally different:
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.
miércoles, 21 de octubre de 2015
Suscribirse a:
Entradas (Atom)