PYTHON
Creado en ~1990 por Guido van Rossum
The ZEN of python
https://www.python.org/dev/peps/pep-0020/
Características
http://www.python.org.ar/aprendiendo-python/
https://www.learnpython.org/
Recursos
Hola Mundo
print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
Tipos de datos
# strings
hello = "hello"
world = "world"
helloworld = hello + " " + world
# booleans
v = True
f = False
d = v | f
d = v & f
# numbers
a = 1
b = 2
c = a + b
Operaciones (algunos)
# strings
hello = "hello"
world = "world"
helloworld = hello + " " + world
# booleans
v = True
f = False
d = v | f
d = v & f
# numbers
a = 1
b = 2
c = a + b
Palabras reservadas
Variables y scope
# This is a global variable
a = 0
if a == 0:
# This is still a global variable
b = 1
def my_function(c):
# this is a local variable
d = 3
print(c)
print(d)
# Now we call the function, passing the value 7
# as the first and only parameter
my_function(7)
# a and b still exist
print(a)
print(b)
# c and d don't exist anymore
# -- these statements will give us name errors!
print(c)
print(d)
Interprete
$ python3.7
Python 3.7 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Control de flujo: if/else
if a==1:
print("hey")
elif a==2:
print("bye")
else
print("I don't know what means this value =(")
print("all the best")
Control de flujo: loops [for]
primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
Control de flujo: loops [while]
# Prints out 0,1,2,3,4
count = 0
while count < 5:
print(count)
# This is the same as count = count + 1
count += 1
Estructuras: Array
Estructuras: Lista
Estructura de datos que puede recorrerse secuencialmente
Pueden tener diferente tipo de dato
Estructuras: array&&list
>>> l = []
>>> l.append('bart')
>>> l.append('milhouse')
>>> 'bart'
['bart', 'milhouse']
>>> for e in l:
... print e
'bart'
'milhouse'
Leyendo input del usuario
name=raw_input('What is your name : ')
print ("Hi %s! I am fortune teller Idris" % name)
Dadas unas series de respuestas posibles : "sí", "no", "tal vez", "no creo" y "peligro" genere un programa que reciba una pregunta y devuelva una respuesta que marcará el curso del destino del usuario
import random
answers = ["Sí", "Tal vez", "No", "No creo", "Peligro"]
question = 'Qué es lo que quieres saber? '
name=input('Cuál es tu nombre? : ')
print ('Hola %s! Un gusto en conocerte, mi nombre es Idris' % name)
can_continue = True
while can_continue == True:
response = input(question)
print(random.choice(answers))
want_to_continue = None
while not want_to_continue:
want_to_continue = input('Deseas preguntar algo más?')
if want_to_continue == "n" or want_to_continue == "N" \
or want_to_continue == "no" or want_to_continue == "No":
can_continue = False
print('Adios! que la fuerza te acompañe')
Estructuras: funciones
Bloque de código que se ejecuta cuando es llamado
Suelen representar una acción
def sum(a, b):
return a + b
def pow(a,pow):
return a**pow