PYTHON
Python
Creado en ~1990 por Guido van Rossum
The ZEN of python
- Beautiful is better than ugly
- Explicit is better than implicit
- Simple is better than complex
- Complex is better than complicated
- Readability counts
https://www.python.org/dev/peps/pep-0020/
Python
Características
- Multiplataforma
- Multiparadigma
- Dynamic typing
- Open source
- Extensible
http://www.python.org.ar/aprendiendo-python/
https://www.learnpython.org/
Recursos
Python
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.'
Python
Tipos de datos
- Numbers (int, float)
- Strings (", ')
- Boolean (True/False)
- Es posible aplicar operaciones
# 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
Python
Operaciones (algunos)
- Asignación ( = )
- Comparación ( == )
- Operadores lógicos (|,&, not, or, and)
# 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
Python
Palabras reservadas
Python
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)
Python
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.
>>>
Python
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")
Python
Control de flujo: loops [for]
primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
Python
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
Python
Estructuras: Array
- estructura de datos contigua que puede accederse con un índice
- Todos los objetos son del mismo tipo
Python
Estructuras: Lista
-
Estructura de datos que puede recorrerse secuencialmente
-
Pueden tener diferente tipo de dato
Python
Estructuras: array&&list
>>> l = []
>>> l.append('bart')
>>> l.append('milhouse')
>>> 'bart'
['bart', 'milhouse']
>>> for e in l:
... print e
'bart'
'milhouse'
Python
Leyendo input del usuario
name=raw_input('What is your name : ')
print ("Hi %s! I am fortune teller Idris" % name)
Ejercicio
Fortune teller
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')
Python
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
Python
By Gabriel Fusca
Python
- 402