Created
February 27, 2016 04:08
-
-
Save aduartem/ffd6f8aab7c569ae6782 to your computer and use it in GitHub Desktop.
Python - Ejemplos
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# probado Python 2.7.6 | |
x = 2.5 | |
y = 7.5 | |
total = x + y | |
print '%g + %g = %g ' % (x, y, total) | |
frutas = ['manzana', 'pera', 'platano', 'papaya'] | |
print '\nFrutas: ' | |
i = 0 | |
for fruta in frutas: | |
print fruta | |
personas = [ | |
{ | |
'nombre' : 'Andrés', | |
'pais' : 'Chile', | |
'edad' : 24 | |
}, | |
{ | |
'nombre' : 'Marley', | |
'pais' : 'Chile', | |
'edad' : 20 | |
} | |
] | |
print '\nPersonas: ' | |
print 'Nombre | Pais | Edad' | |
i = 0 | |
for persona in personas: | |
print '%s | %s | %i' % (persona['nombre'], persona['pais'], persona['edad']) | |
i = i + 1 | |
def sumar(numeros): | |
suma = 0; | |
for numero in numeros: | |
suma = suma + numero | |
return suma | |
print '\n' | |
numeros = [10,20,30,40,50] | |
print numeros | |
resultado = sumar(numeros); | |
print 'La suma de los números es igual a : %i' % resultado | |
def multiplicar(n1, n2): | |
return n1 * n2 | |
resultado = multiplicar(5, 4) | |
print '5 x 4 = %i' % resultado | |
print '\n' | |
class mascota: | |
numero_de_patas = 0 | |
color = '' | |
def dormir(self): | |
print 'ZzzZzzZ' | |
def ladrar(self): | |
print 'guau! guau!!' | |
perro = mascota() | |
perro.numero_de_patas = 4 | |
perro.color = 'blanco' | |
print "El perro tiene %d patas y es de color %s" % (perro.numero_de_patas, perro.color) | |
print "El perro dice: " | |
perro.ladrar() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment