Mientras tanto…

--Originally published at Eduardo's Projectz

La herramienta de “while” es una de las más importantes y utilizadas en la mayoría de los lenguajes de programación. Esta permite que se repita un ciclo mientras una condición sea verdadera, y salga de este en cuanto la condición sea falsa.

while-loop

Para establecer un “while” se necesita escribir la palabra while seguido de la condición que se deseé establecer y, al último, dos puntos.

captura-de-pantalla-de-2016-09-15-21-22-43

En este caso, el código establecido para este “while” se correrá siempre y cuando la variable “x” sea menor que 10.

El código designado para un “while” debe de estar abajo de este con cuatro espacios de diferencia respecto a la posición del “while”:

captura-de-pantalla-de-2016-09-15-21-27-09

En caso de que “x” sea menor que 10, se le añadirá 1 a el valor de “x”. Esto continuará sucediendo hasta que el valor de “x” ya no sea menor de 10. En caso de que el valor de “x” nunca fuese menor de 10, el código del “while” nunca se ejecutaría.

Aquí un ejemplo de un pequeño programa utilizando “while”:

captura-de-pantalla-de-2016-09-15-21-39-38

Este programa te dirá cuantas semanas faltan para que consigas el dinero suficiente para algo.

Aquí un ejemplo de como funciona:

captura-de-pantalla-de-2016-09-15-21-42-38

 

 

Para más información a cerca de “while” y otros bucles: https://geekytheory.com/bucles-en-python/ https://www.tutorialspoint.com/python/python_while_loop.htm https://docs.python.org/3/tutorial/controlflow.html http://www.mclibre.org/consultar/python/lecciones/python_while.html

Si te es más fácil seguir un video-tutorial: https://youtu.be/D0Nb2Fs3Q8c https://youtu.be/XkDOJC4hpw0

 

8573117_orig

 

 

 


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

 

 

 

 


FOR ALL THOSE PROCRASTINATORS

--Originally published at Coding The Future

If you are reading this right now, you were probably procrastinating, and just started studying for tomorrow's test, right?

Well, as always, I've got you covered. Here's a quick video summary of what you need to know for the first partial. I hope things are not too strange.

Good luck!

I WILL LOVE YOU WHILE [condition]:

--Originally published at Coding The Future

Image via GIPHY

I know. I've gotta stop with the song references. But today's topic reminds me of Whitney Houston's "Love You". That song is so beautiful, so inspiring, but it's all FALSE! Well, kind of... Why? Because nothing lasts forever. Well, maybe true love does. But in code, the chances you'll encounter a loop that never ends are super rare.

Today we'll be talking about while loops, which helps us run a piece of code while a certain condition is met.

For example, let's say we wanted to print a countdown. Instead of writing a single line for each number, we would just go to while loops!

While loops are super simple in Python. Begin with the keyword while and then write the condition –without brackets! The following lines, which are obviously indented, is where you put the code that'll loop. Take a look:

count = 0
while (count > 0): print ('The count is:' + count) count = count - 1

As you can see, the loop will run while count is grater than 0, and will stop running once count reaches 0. The count is decreased by 1

For your condition, you can use counters like the example above, or you could use a boolean value by nesting if-statements inside of your while loop. You've gotta be careful though, because as I said, you could get stuck into an infinite loop, which could break your program.

That's pretty much it for today. Until next time!
@emamex98

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"

I DON’T SPEAK GERMAN (BUT I CAN IF YOU LIKE)

--Originally published at Coding The Future

If you are a Lady Gaga fan, you are probably familiar with her famous line "I don't speak German, but I can if you like" from her song SchieBe. This lyric line has always intrigued me, but it has acquired a new meaning since I started programming. Why? Because once you learn an object-oriented language, all others come naturally in a matter of days.

I started coding on Python 3 for the first time last week, and coming from C# on Visual Studio 2008 felt like a natural transition. All the syntax is pretty much the same, and some is even simpler, which is an amazing thing!

I have taken the time to compile the most basic syntax that I've learned on Python so far. Enjoy!

1. Comments

When we think about coding, commenting often goes as an underestimated feature. Consequently, I decided to start my post with comments.

#COMMENTS: To comment, use a number sign and then write your comment.

2. Declaring variables (and arrays)

Unlike other programming languages, I've realized that on Python, you don't have to declare the variable type when declaring a variable. To declare a variable, just type the name, and if you want to, give it a value. Even though you don't need to, I usually give integer-type variables a default value of zero. You can also declare strings and arrays using the appropriate brackets.

i = 5 j = 6
k = i + j
y = [1,3,5,8]
x = ('Emanuel')

3. Output

To output a variable or just some text, you can use the print class. Remember you can always combine several variables or variables with text by concatenating using the plus sign.

print ('Hello World!') print ('Hello, ' + x)
print (k)
print (y)
print (y[2])

4. Input

I'll be Continue reading "I DON’T SPEAK GERMAN (BUT I CAN IF YOU LIKE)"

I DON’T SPEAK GERMAN… But I can if you like

--Originally published at Coding The Future

If you are a Lady Gaga fan, you are probably familiar with her famous line "I don't speak German, but I can if you like" from her song SchieBe. This lyric line has always intrigued me, but it has acquired a new meaning since I started programming. Why? Because once you learn an object-oriented language, all others come naturally in a matter of days.

I started coding on Python 3 for the first time last week, and coming from C# on Visual Studio 2008 felt like a natural transition. All the syntax is pretty much the same, and some is even simpler, which is an amazing thing!

I have taken the time to compile the most basic syntax that I've learned on Python so far. Enjoy!

1. Comments

When we think about coding, commenting often goes as an underestimated feature. Consequently, I decided to start my post with comments.

#COMMENTS: To comment, use a number sign and then write your comment.

2. Declaring variables (and arrays)

Unlike other programming languages, I've realized that on Python, you don't have to declare the variable type when declaring a variable. To declare a variable, just type the name, and if you want to, give it a value. Even though you don't need to, I usually give integer-type variables a default value of zero. You can also declare strings and arrays using the appropriate brackets.

i = 5
j = 6
k = i + j
y = [1,3,5,8]
x = ('Emanuel')

3. Output

To output a variable or just some text, you can use the print class. Remember you can always combine several variables or variables with text by concatenating using the plus sign.

print ('Hello World!')
print ('Hello, ' + x)
print (k)
print (y)
print (y[2])

4. Input

I'll be Continue reading "I DON’T SPEAK GERMAN… But I can if you like"

I DON’T SPEAK GERMAN… But I can if you like

--Originally published at Coding The Future

If you are a Lady Gaga fan, you are probably familiar with her famous line "I don't speak German, but I can if you like" from her song SchieBe. This lyric line has always intrigued me, but it has acquired a new meaning since I started programming. Why? Because once you learn an object-oriented language, all others come naturally in a matter of days.

I started coding on Python 3 for the first time last week, and coming from C# on Visual Studio 2008 felt like a natural transition. All the syntax is pretty much the same, and some is even simpler, which is an amazing thing!

I have taken the time to compile the most basic syntax that I've learned on Python so far. Enjoy!

1. Comments

When we think about coding, commenting often goes as an underestimated feature. Consequently, I decided to start my post with comments.

#COMMENTS: To comment, use a number sign and then write your comment.

2. Declaring variables (and arrays)

Unlike other programming languages, I've realized that on Python, you don't have to declare the variable type when declaring a variable. To declare a variable, just type the name, and if you want to, give it a value. Even though you don't need to, I usually give integer-type variables a default value of zero. You can also declare strings and arrays using the appropriate brackets.

i = 5
j = 6
k = i + j
y = [1,3,5,8]
x = ('Emanuel')

3. Output

To output a variable or just some text, you can use the print class. Remember you can always combine several variables or variables with text by concatenating using the plus sign.

print ('Hello World!')
print ('Hello, ' + x)
print (k)
print (y)
print (y[2])

4. Input

I'll be Continue reading "I DON’T SPEAK GERMAN… But I can if you like"

I DON’T SPEAK GERMAN… But I can if you like

--Originally published at Coding The Future

If you are a Lady Gaga fan, you are probably familiar with her famous line "I don't speak German, but I can if you like" from her song SchieBe. This lyric line has always intrigued me, but it has acquired a new meaning since I started programming. Why? Because once you learn an object-oriented language, all others come naturally in a matter of days.

I started coding on Python 3 for the first time last week, and coming from C# on Visual Studio 2008 felt like a natural transition. All the syntax is pretty much the same, and some is even simpler, which is an amazing thing!

I have taken the time to compile the most basic syntax that I've learned on Python so far. Enjoy!

1. Comments

When we think about coding, commenting often goes as an underestimated feature. Consequently, I decided to start my post with comments.

#COMMENTS: To comment, use a number sign and then write your comment.

2. Declaring variables (and arrays)

Unlike other programming languages, I've realized that on Python, you don't have to declare the variable type when declaring a variable. To declare a variable, just type the name, and if you want to, give it a value. Even though you don't need to, I usually give integer-type variables a default value of zero. You can also declare strings and arrays using the appropriate brackets.

i = 5
j = 6
k = i + j
y = [1,3,5,8]
x = ('Emanuel')

3. Output

To output a variable or just some text, you can use the print class. Remember you can always combine several variables or variables with text by concatenating using the plus sign.

print ('Hello World!')
print ('Hello, ' + x)
print (k)
print (y)
print (y[2])

4. Input

I'll be Continue reading "I DON’T SPEAK GERMAN… But I can if you like"