d={} (d['a'],d['o'],d['g'])=("alpha","omega","gamma") print d for k in sorted( d.keys() ): print k," ",d[k] print d
Output:
{'a': 'alpha', 'g': 'gamma', 'o': 'omega'} a alpha g gamma o omega {'a': 'alpha', 'g': 'gamma', 'o': 'omega'}
d={} (d['a'],d['o'],d['g'])=("alpha","omega","gamma") print d for k in sorted( d.keys() ): print k," ",d[k] print d
{'a': 'alpha', 'g': 'gamma', 'o': 'omega'} a alpha g gamma o omega {'a': 'alpha', 'g': 'gamma', 'o': 'omega'}
a=[(1,"b"),(2,"a"),(1,"e")] print a print sorted(a) def myTSort(d): return d[0] # sort according to first value print sorted(a,key=myTSort) print sorted(a,key=myTSort, reverse=True) print a
[(1, 'b'), (2, 'a'), (1, 'e')] [(1, 'b'), (1, 'e'), (2, 'a')] [(1, 'b'), (1, 'e'), (2, 'a')] [(2, 'a'), (1, 'b'), (1, 'e')] [(1, 'b'), (2, 'a'), (1, 'e')]
a=["Life", "is", "very","easy", "with", "python"] def myFunction(data): return data[0] print sorted(a) print sorted(a,key=myFunction) print sorted(a,key=len) print sorted(a)
['Life', 'easy', 'is', 'python', 'very', 'with'] ['Life', 'easy', 'is', 'python', 'very', 'with'] ['is', 'Life', 'very', 'easy', 'with', 'python'] ['Life', 'easy', 'is', 'python', 'very', 'with']
a=[3,2,1,4,5] print a print sorted(a) print sorted(a,reverse=True) print a
[3, 2, 1, 4, 5] [1, 2, 3, 4, 5] [5, 4, 3, 2, 1] [3, 2, 1, 4, 5]
lock=threading.Lock() def writeToFile(): lock.acquire() try: //write data finally: lock.release()
Here some great python quotes. You may share yoursPerl is worse than Python because people wanted it worse - Larry Wall, Creator of Perl Python fits your brain - Bruce Eckel, Author: Thinking in Java Python is an excellent language & makes sensible compromises. - Peter Norvig. AI Life is better without braces