For loops with «for»

We have already seen how to do loops with “while”, and now we will learn how to use the “for” statement.

The way you write a for is the following: “for i in x:”, after that you fill up its guts with what action you want it to perform.

The “in” part makes it so that the “for” gets along especially well with ranges, list, tuples and dictionaries. What it does is that it makes your program search for a special something in the insides of another something, for example a range or a list, and then do something to that something it found, or simply perform an action according to it.

We can convine this with other stuff, like ifs for example:

L [1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 5]

For I in L:

If (I = 1):

Print (“yes”)

Elif (I = 0):

Print (“no”)

Else:

Print (“Get yourself together!”)

This program will check every value within the list “L”, and depending on its magnitude it will perform an action, printing “yes” if the value is one, “no” if the value is zero, or “Get yourself together!” if it finds another value.

Using for statements gets fun when you start working with calculations or “searches” with big amounts of data, it will save your butt a lot of pain.

The main difference between a “while” and a “for” is that the while will run definitively if given the chance, but the “for” fill always have a beginning and an end, since you work with finite things like lists and ranges. A “for” CAN be infinite, but only if the range you are using is infinite itself.

Publicado por: juansalvadorfernandez

Elegí estudiar ITE porque pienso que es una carrera versátil con la que se pueden lograr cosas muy interesantes. Aunque realmente no sé si alguna otra carrera me gustaría más, las personas terminan haciendo lo que les gusta, independientemente de la carrera que estudien. Si alguien me pregunta quien es mi héroe, sería mi padre, pues es una persona fuerte que en momento de necesidad sacó adelante a su familia, pagó los estudios de sus hermanos, es un profesionista que disfruta de su trabajo y que tiene metas fijas y aspira a ellas con todas sus fuerzas. Hace tiempo tocaba la flauta dulce e intenté aprender a tocar piano, teclado y guitarra. No me gusta el deporte. Realmente no tengo un autor ni un libro favorito, siento que me falta encontrar un tema o un estilo que realmente me guste.

Etiquetas, Deja un comentario

Deja un comentario