In [61]:
a = 5
b = 6.0
a + b
hi = 'hello'
#hi+a
a,b = 3,6
print a,b
print range(3)
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
In [9]:
def hello_function(name ='Sergi'):
""" I hate documentation buuuuuuuuu"""
print 'hello '+name
hello_function()
hello_function('Someone')
In [10]:
nice = 'F**** awesome!!'
print nice.__doc__
In [77]:
multi = """
This is a
multiline sentence guys
"""
#substrins
animal = "elephant"
print animal[:1]
print animal[2:]
print animal[3:4]
#reverse
print animal[-1]
print animal[2:-1]
#operations
print 'hi'*3
In [29]:
word = "supercalifragilisticoexpialidocious"
print len(word)
ImSpanish = 'Yo soy español'
print ImSpanish
ImSpanish = u'Yo soy español'
print ImSpanish
#format
instrument = 'guitar'
sentence = 'I like playing {0}'.format(instrument)
print sentence
instrument = 'with fire'
sentence = 'I like playing {instrument}'.format(instrument=instrument)
print sentence
In [20]:
import os
print os.__name__
print os.__file__
print os.__package__
dir(os)
#os.path.abspath(__file__)
#import sys
#sys.path.append("/home/me/mypy")
#if __name__ == "__main__":
# run_my_app()
Out[20]:
In [73]:
a_dictionary = {'aKey':'aValue', 'anotherKey': 'anotherValue' }
#print dir(a_dictionary)
print a_dictionary
print a_dictionary['aKey']
#modify an element
a_dictionary['aKey'] = 'valueChanged'
print a_dictionary['aKey']
#add an element several types
a_dictionary['aNumber'] = 4
print a_dictionary
#for
for key,value in a_dictionary.items():
print key, value
#remove items
del a_dictionary['aNumber']
print a_dictionary
a_dictionary.clear()
print a_dictionary
In [51]:
#print dir(list)
list = [1,2,'three']
print list
print list[0]
print list[1:]
print list[-1:]
In [ ]:
list.append('added item')
print list
list.insert(1,'insertAnItem')
print list
list.insert(1,['insertItemList','anotherAnotherItemFromAddedList'])
print list
list[1:1] = ['insert','twoItems']
print list
In [ ]:
list = ['firstItem','secondItem','thirdItem']
print list.index('secondItem')
print list.index('thirdItem')
#list.index('fourthItem')
print 'fourthItem' in list
print 'secondItem' in list
In [54]:
list = ['firstItem','secondItem','thirdItem']
list.remove('secondItem')
print list
last_element = list.pop()
print last_element
In [76]:
a = [1,2]
b = [3,4]
c = a+b
print c
print a*2
In [ ]:
t = (1,'two','last')
#print dir(t)
print t[1]
print t[-1]
print t[1:3]
In [ ]:
li = [3,4,5,6]
#li = range(3,7)
print li
print [item*10 for item in li]
print [item*10 for item in li if item > 4]
robots = { 'Mazinger': 'Z', 'Robotech': '3000'}
#print [ 'Robot {name} is type {type}'.format(name=key,type=value) for key,value in robots.items()]
In [93]:
import types
print dir(types)
l = [0,1]
print types.BooleanType == type(l)
print types.ListType == type(l)
print callable(l)
print callable(l.append)
Out[93]:
In [110]:
hi = 'hi bro'
print getattr(hi,'split')
l = ['hi','bro']
print '-'.join(l)
print getattr('_','join')(l)
import types
print type(getattr(l,'index'))
type(getattr(l,'index')) == types.BuiltinFunctionType
print getattr(l, 'sort').__doc__
In [5]:
class Vehicle:
def start(self):
print 'RUNNNNNNNNN'
def accelerate(self):
print 'speed of light'
vehicle = Vehicle()
vehicle.start()
class Car(Vehicle):
wheels = 4
speed = 0
accel = 2
def __init__(self, brand):
self.brand = brand
def accelerate(self):
self.speed += self.accel
def get_velocity(self):
return self.speed;
car = Car('Jonda')
car.start()
car.accelerate()
print car.get_velocity()
car.accelerate()
print car.get_velocity()
class Robot:
def __init__(self, name, weapon):
self.name = name
self.weapon = weapon
def shot(self):
print 'shotgun devastator with my weapon {weapon}'.format(weapon = self.weapon)
def __str__(self):
return 'I am {0} and I am going to destroit you with my {1}'.format(self.name, self.weapon)
wally = Robot('Wally', 'BigGun')
wally.shot()
class Transformer(Car,Robot):
def __init__(self,brand, name, weapon):
self.name = name
self.brand = brand
self.weapon = weapon
optimus = Transformer('Pegasus','Optimus','EvilGrenade')
print optimus
optimus.start()
#Others : __new__(cls, args) __del__(self) __str__(self) __cmp__(self, arg) __len__(self)
from IPython.display import Image
i = Image(filename='files/Optimus-Prime.jpg')
i
Out[5]: