lunes, 19 de octubre de 2015
viernes, 9 de octubre de 2015
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?”))
Comments on Python
Sometimes
we need to pass our code so that someone else can see it and make some sort of
improvements. Sometime we just need someone else to tell us what we are doing
wrong.
Unfortunately,
not all programmers program the same. Each one of the does this artwork how
they believe is the best way to do it; this makes it almost impossible for
someone else to understand what we are doing in each line of code.
For this,
we use comments. You can add comments on a python file just by adding “#”
before what you try clarify. This way, your comment do not affect the
functioning of the program but it makes it clear to understand.
For
example, this is the code of a program without comment. As you can see, it is
hard to understand what is going on.
But
once we add the comments it’s easy to understand what´s happening on every line.
Just
remember, these comments DO NOT affect the code, so don´t be afraid to use them
more often.
User Input
Sometimes,
as a programmer, we need to consider that every user needs a program for them.
However, this is not possible, but it is possible to use the same program for different
users. For example we would like a program to greet everyone by their name, but
not everyone’s name is the same. For this, we use variables.
Variables called this way because they can keep changing
along with a program. Reviewing the example before; we would like this program
to greet everyone by their name. If the value that keeps changing is the name,
then the name has to become a variable. To convert the name into a variable we
type it like this:
Name=
However, we
cannot just type a random name after the equal sign. If we do this, the program
will always greet a person in particular. For changing this, we need to state
the variable as an input:
Name=input(“What´s
your name?”)
When
running this, the user will see the following text on their screen:
What´s your
name?
After this,
the user is allowed to type their respective name and the program will register
it as the value of name. Therefore, when we print the variable, the user can
see the text they typed:
For
example, a float number will be 3.2837 and its integer will be 3.
For turning
the input into a number we need to type “float” if we want it to be a float
number or “int” if we wat it to be an integer between the equal sign and the “input”.
For
example, this program asks for the value of 2 numbers that will be float
numbers and shows you the sum of those.
The same
code can be used for integer numbers. You just need to change “float” for “int”
and the result will be different:
Suscribirse a:
Entradas (Atom)




























