Bucle For:

--Originally published at Migue´s Blog

La estructura for sirve para repetir un bloque de instrucciones tantas veces como se encuentre un elemento.

Su estructura es la siguiente

For (variable) in (elemento)

(Cuerpo del ciclo)

Las palabras en negritas no cambian, la variable puede tomar cualquier nombre, una letra o una palabra, normalmente se utiliza la letra i.

El elemento indica las veces que se va a repetir el ciclo, si se escribe una palabra en cada repetición la variable tomará el valor de cada letra, si se escriben varios elementos como palabras o números la variable en cada repetición tomará el valor de cada elemento, también se puede escribir un rango para que se repita el número de veces del rango, o se puede escribir un rango entre dos números para que tome valores entre esos dos números.

Para poder obtenr una información más detallada y con más ejemplos puede visitar la página bucle for.

Para poder ejemplificar esto realicé un programa que cuenta el número de caracteres en una frase que ingrese el usuario.

captura-de-pantalla-de-2016-09-14-23-18-47

 

captura-de-pantalla-de-2016-09-14-23-19-37

Como en todos mis programas anteriores las primeras dos lineas sirven para crear un ciclo infinito y poder calcular diferentes valores.

captura-de-pantalla-de-2016-09-14-23-52-21

Después guardamos en la variable “y” la cadena de texto que vamos a analizar, lo tenemos que guardar asi para que se guarde como un sólo elemento.

También utilizamos un contador llamado letras que nos servirá para ir añadiendo 1 por cada vez que se repita el ciclo.

captura-de-pantalla-de-2016-09-14-23-55-48

Por último utilizamos el bucle for, como “y” lo almacenamos como 1 solo elemento, esto funciona igual que si escribieramos una sola palabra, por lo que el ciclo se repetira 1 vez por cada letra, y por cada repetición se añadirá 1 al contador.

Finalmente imprimimos el resultado final del contador.

captura-de-pantalla-de-2016-09-15-00-00-38

 

 

 

 


Vectors

--Originally published at Migue´s Blog

Vectors are quantities that have a magnitude and a direction and are represented with arrows.

imagen de vector
                                                                     Created in GlowScript by Rhett Allain

An vector is composed of two components, one component in the x-axis, and another one in the y-axis, this two components are the cathetus of right-angled triangle, and the vector is the hypotenuse.

To calculate this components we should use the functions “sin” and “cos”.

I have created a program that can calculate this components using the functions.

Captura de pantalla de 2016-08-29 10-33-38

To calculate the components python needs the functions, but python doesn’t have all the functions, because if python has them python would be enormous!

So we need to import the modules that we need.

The modules are files that contain Python definitions and statements.

In this case we need the functions “Sin” and “Cos” that are included in the module math, so we need to import them.

First we import the module math with the function “import” and after that from the module “math” we import the modules “Sin” and “Cos”

Captura de pantalla de 2016-08-29 10-48-29

Now we can use the functions that we need, and we can ask to the user to introduce the magnitude and the angle of the vectors.

But we have another problem, if we introduce the angle in degrees the program will give us results that are not correct because python works with radians, so we need to transform the degrees to radians.

To do that we only use a simply mathematical formula, we need to multiply the angle in degrees  by Pi and then divide the result by 180, to get a more accurate result we can use the constant Pi that python already knows, to call it we only write math.pi in the code.

Captura de pantalla de 2016-08-29 11-00-49

Now we can calculate the components without no problems.

To calculate the component

Captura de pantalla de 2016-08-29 11-04-34
Captura de pantalla de 2016-08-29 11-07-56
Captura de pantalla de 2016-08-29 11-12-09
Continue reading "Vectors"

Prime numbers

--Originally published at Migue´s Blog

A prime number, or also called prime, is a positive integer greater than 1, that number has no positive integer divisors other than 1 and itself, it means that it can’t be factored.Prime numbers.

This is a list of the first prime numbers.

numeros-primos

Yesterday I made a program to know if a number is prime.

To make this program I saw the tutorials located in the page sololearn.com.  This page is free and in this page you can complete tutorials to learn about how to program in phtyon, c++ and java.

Captura de pantalla de 2016-08-21 23-43-33

In my program I used while loops and if conditionals.

The first while loop, while z==1Captura de pantalla de 2016-08-22 10-44-44, is used to make an infinite program until the user press the combination ctrl+c that cancels the infinite loop and closes the program, or you can click on the “X” in your window Captura de pantalla de 2016-08-22 10-42-16.

This line,Captura de pantalla de 2016-08-22 10-46-56 prints the instructions to close the program in the screen for the users that don’t know how to cancel a loop in the terminal.

Captura de pantalla de 2016-08-22 10-50-52In the fourth line with the function “input” the user introduce  a value with the keyboard and the function “int”, integer, converts the value that the user introduced to an integer value that python can use to do mathematical operations, and the value is saved on the variable named “x”

In the fifth line the variable “a” gets a value of 2. This variable will help us to make the next while loop and to finish it when we reach a result for the number.

Captura de pantalla de 2016-08-22 11-11-15This lines exclude the numbers 0, 1 and the negative numbers from the loop because they are not prime numbers and if they enter to the loop can cause some problems when the program is evaluating the number that we introduced.

Captura de pantalla de 2016-08-22 11-04-51In this final while

Captura de pantalla de 2016-08-25 10-16-14
Captura de pantalla de 2016-08-21 23-45-12
Continue reading "Prime numbers"