Tutorial by Examples

A for loop iterates over a sequence, so altering this sequence inside the loop could lead to unexpected results (especially when adding or removing elements): alist = [0, 1, 2] for index, value in enumerate(alist): alist.pop(index) print(alist) # Out: [1] Note: list.pop() is being used t...
def foo(li=[]): li.append(1) print(li) foo([2]) # Out: [2, 1] foo([3]) # Out: [3, 1] This code behaves as expected, but what if we don't pass an argument? foo() # Out: [1] As expected... foo() # Out: [1, 1] Not as expected... This is because default arguments of function...
Consider the case of creating a nested list structure by multiplying: li = [[]] * 3 print(li) # Out: [[], [], []] At first glance we would think we have a list of containing 3 different nested lists. Let's try to append 1 to the first one: li[0].append(1) print(li) # Out: [[1], [1], [1]] ...
Python uses internal caching for a range of integers to reduce unnecessary overhead from their repeated creation. In effect, this can lead to confusing behavior when comparing integer identities: >>> -8 is (-7 - 1) False >>> -3 is (-2 - 1) True and, using another example: ...
You might have heard that everything in Python is an object, even literals. This means, for example, 7 is an object as well, which means it has attributes. For example, one of these attributes is the bit_length. It returns the amount of bits needed to represent the value it is called upon. x = 7 ...
When testing for any of several equality comparisons: if a == 3 or b == 3 or c == 3: it is tempting to abbreviate this to if a or b or c == 3: # Wrong This is wrong; the or operator has lower precedence than ==, so the expression will be evaluated as if (a) or (b) or (c == 3):. The correct w...
The first element of sys.argv[0] is the name of the python file being executed. The remaining elements are the script arguments. # script.py import sys print(sys.argv[0]) print(sys.argv) $ python script.py => script.py => ['script.py'] $ python script.py fizz => script.py ...
You might expect a Python dictionary to be sorted by keys like, for example, a C++ std::map, but this is not the case: myDict = {'first': 1, 'second': 2, 'third': 3} print(myDict) # Out: {'first': 1, 'second': 2, 'third': 3} print([k for k in myDict]) # Out: ['second', 'third', 'first'] Py...
Plenty has been written about Python's GIL. It can sometimes cause confusion when dealing with multi-threaded (not to be confused with multiprocess) applications. Here's an example: import math from threading import Thread def calc_fact(num): math.factorial(num) num = 600000 t = Threa...
Consider the following list comprehension Python 2.x2.7 i = 0 a = [i for i in range(3)] print(i) # Outputs 2 This occurs only in Python 2 due to the fact that the list comprehension “leaks” the loop control variable into the surrounding scope (source). This behavior can lead to hard-to-find...
Function xyz returns two values a and b: def xyz(): return a, b Code calling xyz stores result into one variable assuming xyz returns only one value: t = xyz() Value of t is actually a tuple (a, b) so any action on t assuming it is not a tuple may fail deep in the code with a an unexpecte...
my_var = 'bla'; api_key = 'key'; ...lots of code here... params = {"language": "en", my_var: api_key} If you are used to JavaScript, variable evaluation in Python dictionaries won't be what you expect it to be. This statement in JavaScript would result in the params object ...

Page 1 of 1