Python 10 个最好的特性

>>> a =  "codementor"
>>> print "Reverse is",a[::-1]
Reverse is rotnemedoc

>>> mat = [[1, 2, 3], [4, 5, 6]]
>>> zip(*mat)
[(1, 4), (2, 5), (3, 6)]

>>> a = [1, 2, 3]
>>> x, y, z = a
>>> x
1
>>> y
2
>>> z
3

a = ["Code", "mentor", "Python", "Developer"]
>>> print " ".join(a)
Code mentor Python Developer

List 1 = [‘a’, ‘b’, ‘c’, ‘d’]
List 2 = [‘p’, ‘q’, ‘r’, ‘s’]

Write a Python code to print

ap
bq
cr
ds

>>> for x, y in zip(list1,list2):
...    print x, y
...
a p
b q
c r
d s

>>> a=7
>>> b=5
>>> b, a =a, b
>>> a
5
>>> b
7

>>> print "code"*4+' '+"mentor"*5
codecodecodecode mentormentormentormentormentor

a = [[1, 2], [3, 4], [5, 6]]

>>> import itertools
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

def is_anagram(word1, word2):
"""Checks whether the words are anagrams.
word1: string
word2: string
returns: boolean
"""

from collections import Counter
def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)
>>> is_anagram('abcd','dbca')
True
>>> is_anagram('abcd','dbaa')
False

input: “1 2 3 4 ”
return [1,2,3,4]

>>> result = map(lambda x:int(x) ,raw_input().split())
1 2 3 4
>>> result
[1, 2, 3, 4]