Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # print
- print 123
- print 123,
- # dictionary
- mydict = {"name": "John Doe", "age": 30}
- for key in mydict.keys():
- print key
- for value in mydict.values():
- print value
- for key, value in mydict.iteritems():
- print key, value
- # raw_input => input
- name = raw_input("What is your name: ")
- print name
- # StringIO
- import StringIO
- stream = StringIO.StringIO("A string input.")
- print stream.read()
- # xrange => range
- for i in xrange(1, 5):
- print i,
- # map, filter, reduce, zip operations
- my_list = ["1", "22", "333", "4444"]
- my_list_mapped = map(int, my_list)
- my_list_filtered = filter(lambda x: x < 10 or x > 100, my_list_mapped)
- my_list_reduced = reduce(lambda x, y: x + y, my_list_filtered)
- my_list_zip = zip(my_list, xrange(4))
- # Class
- class MyClass:
- def __init__(self, name):
- self.name = name
- def __str__(self):
- return self.name
- # Iterator
- class MyIterator:
- def __init__(self, iterable):
- self._iter = iter(iterable)
- def next(self):
- return self._iter.next()
- def __iter__(self):
- return self
- my_iterator = MyIterator([1, 2, 4, 5])
- my_iterator.next()
- print my_iterator
- # exception
- try:
- b = 1 / 2.0
- c = 1 / 0
- except ZeroDivisionError, e:
- print str(e)
- # Attention here. This can not be automatically done properly and
- # will break your code.
- # bytes and unicode
- assert isinstance("a regular string", bytes)
- assert isinstance(u"a unicode string", unicode)
- assert isinstance("a regular string", basestring)
- # string codec: encodeing and decoding
- my_bytes1 = "\xc3\x85, \xc3\x84, and \xc3\x96"
- my_unicode1 = my_bytes1.decode("utf-8")
- assert isinstance(my_bytes1, bytes)
- assert isinstance(my_unicode1, unicode)
- my_unicode2 = u"Å, Ä, and Ö"
- my_bytes2 = my_unicode2.encode("utf-8")
- assert isinstance(my_unicode2, unicode)
- assert isinstance(my_bytes2, bytes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement