Arthur Vuillard, #!⌨
def factorial(number):
if number in [0, 1]:
return 1
else:
return number * factorial(number - 1)
factorial(10)
# 3628800
class MyClass:
"""A simple example class"""
number = 12345
def __init__(self, name):
self.name = name
def method(self):
return 'hello %s %d' % (self.name, self.number)
object = MyClass('AFUP')
print object.name
# AFUP
print object.number
# 12345
print object.method()
# hello AFUP 12345
print object
# <__main__.MyClass instance at 0xb731a0ec>
print object.method
# <bound method MyClass.method of <__main__.MyClass instance at 0xb734c0ec>>
print type(1)
# <type 'int'>
print type(1.0)
# <type 'float'>
print type('a')
# <type 'str'>
print type(u'a')
# <type 'unicode'>
class Test(object):
pass
o = Test()
print type(o)
# <class '__main__.Test'>
type([0, 1, 'a'])
# <type 'list'>
type({'key': 'value'})
# <type 'dict'>
type({'element'})
# <type 'set'>
type(('e1', 'e2',))
# <type 'tuple'>
class Duck(object):
def quack(self):
print 'COIN'
class DuckRobot(object):
def quack(self):
print 'BIP'
def make_it_quack(o):
o.quack()
duck = Duck()
make_it_quack(duck)
# COIN
robot = DuckRobot()
make_it_quack(robot)
# BIP
def squares():
number = 1
while True:
print number
yield number ** 2
number += 1
for square in squares():
print square
if square == 25:
break
squares = [x ** 2 for x in range(10)]
print squares
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()