This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| my_list = [i**2 for i in range(1,11)] | |
| # Generates a list of squares of the numbers 1 - 10 | |
| f = open("output.txt", "w") | |
| for item in my_list: | |
| f.write(str(item) + "\n") | |
| f.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Car(object): | |
| condition = "new" | |
| def __init__(self, model, color, mpg): | |
| self.model = model | |
| self.color = color | |
| self.mpg = mpg | |
| def display_car(self): | |
| print "This is a %s %s with %d MPG." % (self.color, self.model, self.mpg) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Point3D(object): | |
| def __init__(self,x,y,z): | |
| self.x = x | |
| self.y = y | |
| self.z = z | |
| def __repr__(self): | |
| return "(%d, %d, %d)" % (self.x, self.y, self.z) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Fruit(object): | |
| """A class that makes various tasty fruits.""" | |
| def __init__(self, name, color, flavor, poisonous): | |
| self.name = name | |
| self.color = color | |
| self.flavor = flavor | |
| self.poisonous = poisonous | |
| def description(self): | |
| print "I'm a %s %s and I taste %s." % (self.color, self.name, self.flavor) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| threes_and_fives = [x for x in range(1,16) if x % 3 == 0 or x % 5 == 0] | |
| print threes_and_fives | |
| # Resultado sería: [3, 5, 6, 9, 10, 12, 15] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Datos de entrada: diccionario | |
| movies = { | |
| "Monty Python and the Holy Grail": "Great", | |
| "Monty Python's Life of Brian": "Good", | |
| "Monty Python's Meaning of Life": "Okay" | |
| } | |
| # Imprime el contenido de diccionario en tuplas de clave, valor | |
| print movies.items() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| squares = [i**2 for i in range(1,11)] | |
| # squares contiene [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] | |
| print filter(lambda x: 30 <= x <= 70, squares) | |
| # Resultado: [36, 49, 64] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Esta función: | |
| def by_three(x): | |
| return x % 3 == 0 | |
| # es equivalente a esta otra: | |
| lambda x: x % 3 == 0 | |
| # Ejemplo: | |
| my_list = range(16) | |
| print filter(lambda x: x % 3 == 0, my_list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Lista de compresion para rellenar la muestra | |
| l = [i ** 2 for i in range(1, 11)] | |
| # l contiene [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] | |
| # [start:end:stride] | |
| """ Donde start describe el comienzo de la seleccion (inclusive), | |
| end es el final (exclusive) y stride describe el espacio entre los | |
| elementos de la lista""" | |
| # En este ejemplo va a coger dede el elmento de la posicion 2 (empieza por 0) | |
| # la posicion 8 incluida, y con un saldo de dos elementos. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def print_grades(grades): | |
| for grade in grades: | |
| print grade | |
| def grades_sum(grades): | |
| total = 0 | |
| for grade in grades: | |
| total += grade | |
| return total | |