meetup.com/Django-Cali
Instalación en Windows
El instalador viene con una aplicación de consola, la cual permite probar python en tiempo de ejecución
brew install python3
Por ejemplo:
Ubuntu 14.04
# Operaciones básicas
>>> 1 + 1
2
# Imprimir Texto
>>> print('Hello world!')
Hello world!
# Ayuda de Python
>>> help
Type help() for interactive help, or help(object) for help about object.
# Comentario simple
def approximate_size(size, a_kilobyte_is_1024_bytes=True):
'''Convert a file size to human-readable form.
Keyword arguments:
size -- file size in bytes
a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
if False, use multiples of 1000
Returns: string
'''
Comentando código
>>> import sys ①
>>> sys.path
Importar librerías
>>> import humansize
>>> print(humansize.approximate_size(4096, True))
4.0 KiB
>>> print(humansize.approximate_size.__doc__)
Todo es un objeto
def approximate_size(size, a_kilobyte_is_1024_bytes=True):
if size < 0:
raise ValueError('number must be non-negative')
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
for suffix in SUFFIXES[multiple]:
size /= multiple
if size < multiple:
return '{0:.1f} {1}'.format(size, suffix)
raise ValueError('number too large')
Identación de código
>>> 2+2
4
>>> 2+4*8
34
>>> (100-20)/6
13
>>> (100-20)%6
2
>>> (100.0-20)/6
13.333333333333334
>>> 2**3
8
>>> 0xFF ^ 0xF0
15
>>> oct(87)
’0127’
Operaciones numéricas
###Programa que imprima los 25 primeros numeros naturales
n = 1
while n <= 25:
print n,
n += 1
###Imprimir los numeros impares desde el 1 al 25, ambos inclusive
n = 1
h = ''
while n <= 25:
if n%2 != 0:
h += ' %i' % n
n += 1
print h
###Imprimir los numeros pares desde el 40 hasta el 60, ambos inclusive
n = 40
h = ''
while n <= 60:
if n%2 == 0:
h += ' %i' % n
n += 1
print h
meetup.com/Django-Cali