This is a copy of my old Quora answer
1. Python is the only language to have an else condition for a for loop. It executes if the for loops terminates normally without any "break"
def contains_even_number(l): for elt in l: if elt % 2 == 0: print "list contains an even number" break else: print "list does not contain an even number"
The else will be fired if there's no even number in list.
2. You can assign
, but not the other way round.
>>>  = () >>> () =  File "<stdin>", line 1 SyntaxError: can't assign to () >>>
The reason is
 is an empty list and
() is an empty tuple. Tuples are immutable meaning you can't change them once created.
3. Python uses indentation to mark blocks whereas other languages uses braces. If you run
from __future__ import braces, you'll get -
>>> from __future__ import braces File "<stdin>", line 1 SyntaxError: not a chance
it's a joke, meaning even in future, Python won't use braces.
4. You can assign different values to different variables at the same time-
>>> a, b = 1, 2 >>> a 1 >>> b 2 >>>
As you can see, a gets the value 1 and b gets the value 2.
5. To swap variables in other languages, you need to have a temporary variable and do it like this -
temp = x; x = y; y = temp;
In python, just-
a, b = b, a
6.You can return multiple values at the same time.
def x(): return 1, 2, 3, 4
Now call and see.
a, b, c, d = x()
Actually, Python is not returning more than one values. It’s actually returning a tuple, which gets unpacked.
7. You can quickly reverse a string. Python treats strings as list of characters, so you can use list slicing to reverse strings.
>>> a = "Hello World!" >>> a[::-1] '!dlroW olleH' >>>
8. Quickly transpose a matrix using zip. Zip takes two equal-length collections, and merges them together in pairs.
>>> mat = [[1, 2, 3], [4, 5, 6]] >>> zip(*mat) [(1, 4), (2, 5), (3, 6)]
9. You can multiply strings with integers.
>>> "code"*4 'codecodecodecode'
As you can see, the string is repeated 4 times.
10. Python provides with block to work with resources that need to be free after you finish working with them, for example file
with open("test.txt") as f: #do something with f #do something else
Here the with block makes sure that the file is closed when the control exits the block. This saves you calling close on the file every time you open one.
You can create your own class which can be used with “with” block. You just have to define two methods -
__enter__(self): This method is called when the control reaches the with block. This is where you allocate the resource.
__exit__(self,type,value,traceback): This is called before exiting the with block. This is where you free up resources.
Let’s see an example -
class Test: def __init__(self): print("Creating object instance") def __enter__(self): print("Calling enter") def __exit__(self,type,value,traceback): print("Calling exit") with Test() as x: print("Inside with block") print("Outside with")
And the output -
Creating object instance Calling enter Inside with block Calling exit Outside with