$ python$ ipython a = 12 b = a id(a) id(b) n = None # NoneType: special value meaning... nothing b = True # bool: boolean... True or False (case sensitive) i = 15 # int: integer f = 15.5 # float: non-integer values s = "string" # str: strings, written with "" or '' u = u"string" # unicode: unicode string, writh with u"" or u'' l = [] # list: list of objects (ordered) t = () # tuple: immutable list of objects (can't append to it) d = {} # dict: dictionary of data (unique, unordered) st = {} # set: a collection of objects (unique, unordered), set([]) coord = (45.30, 73.34) lat, lon = coord float(a) l = [[1,2,3],[4,'ohai',6],[7,8,9]] d = {1611: {'lastname':u'Gutiérrez Hermoso', 'firstname':u'Jordi'}, 123: {'lastname':u'Leduc-Hamel', 'firstname':u'Mathieu'}} l[2] d[1611] l[1][1] l[1:3] def my_function(param1, param2, param3=None, param4=0, *args, **kwards): """This is my function.""" output = True return output name = u"Jordi Gutiérrez Hermoso" firstname, paternalname, maternalname = name.split() paternalname.upper() paternalname.lower() paternalname.ljust(30) name = [firstname.lower(), paternalname.lower(), maternalname.lower()] username = ".".join(name) name = u"Jordi Gutiérrez Hermoso" username = ".".join(name.split()).lower() users = [] users.append(username) jordigh = {'firstname':u'Jordi', 'lastname':u'Gutiérrez Hermoso'} mathieu = {'firstname':u'Mathieu', 'lastname':u'Leduc-Hamel'} jp = {'firstname':u'Jean-Philippe', 'lastname':u'Caissy'} people = [] people.append(jordigh) people.append(mathieu) people.append(jp) status = [ (1, u'New'), (2, u'In progress'), (3, u'Rejected'), (4, u'Accepted'), ] numlist = range(6) if 5 in numlist: print 'hooray 5' elif 4 in numlist: print 'hooray 4' else: print 'not hooray :-(' 'hooray 5' if 5 in numlist else 'not hooray :-(' year = 2012 while year <= 2015: print year year = year + 1 # year += 1 for i in range(2012, 2016): print i #! /usr/bin/env python # -*- encoding: utf-8 -*- def sup(name): return u"Sup %s!" % (name) if __name__ == '__main__': print u"--------------------------------------------------" print u"START the script" print u"--------------------------------------------------" name = raw_input("What is your name? ") print sup(name) print u"-----------------------------------------------" print u"END the script" print u"-------------------------------------------------" run script$ python script.py class Person(object): def __init__(self, name, firstname, dob=None): self.name = name self.firstname = firstname self.dob = dob def age(self): # TODO : compute in function of dob and now return self.dob mathieu = Person("Leduc-Hamel", "Mathieu") davin = Person(firstname="Davin", name="Baragiotta") from datetime import date today = date.today() print today #year = ?? import sys sys.path __init__.py__name____main__$ pip install packagename f = open('python.txt') for line in f.readlines(): print line, f.close() f = open("python.txt") lines = f.readlines() f.close() target = 'python' context = [line for line in lines if target in line] comments = [line for line in lines if line.startswith('#')] new_list = [v for v in old_list if v < 2]
new_dict = {k, v for k, v in old_dict.items() if k < 2}
new_set = {v for v in old_list if v < 2}
for n in range(10): print "%d to the 3rd power is: %d" % (n, n**3) for p in people: print "Bonjour/Hello %s %s" % (p['firstname'], p['lastname'].upper()) try: 15/0 except (ZeroDivisionError,), e: print "Dividing by zero is bad, m'kay?" print e import pickle f = open('pickles', 'w') pickle.dump(status, f) pickle.dump(people, f) f.close() exit() import pickle f = open('pickles') pickle.load(f) #objects = [] #for obj in pickle.load(f): # objects.append(obj) f.close()