Tips & Tricks for Python Developers
Python is one of the most popular programming languages today. It powers Google, YouTube, BitTorrent, Pinterest, Dropbox, Sublime Text among a host of other projects. It lets you work quickly and get things done easily.
Here is a collection of tips & tricks in Python 3. As of Jan 2017, there are 94 tips & tricks listed here. The list is split up in pages for convenient browsing. You may find the pages menu at the bottom of each page.
Page #1
- Repeating a string using the * operator
- The "if __name__ == '__main__':" construct
- Exploring an object: Using the builtin function dir()
- Getting help from Python: Using the builtin function help()
- Prettifying output with the builtin pprint module
- When to use which data structure
- Unpacking Iterables into individual variables
- Docstrings
- Duck Typing
- Python's Delimiters
Repeating a string using the * operator
The * operator can be used to repeat a string as many times you would like.
>>> 'hi' * 5 'hihihihihi' >>> '='*20 '====================' >>> def printHeader(headerString): print('#' * 12) print('# {} #'.format(headerString)) print('#' * 12) >>> printHeader('Comments') ############ # Comments # ############
The "if __name__ == '__main__':" construct
You may have encountered something like if __name__ == '__main__'. This if construct is used to execute the top-level code(i.e. at the first indent level) of a script ONLY IF it is being run directly(i.e. by double-clicking the .py file), and not if the module is being imported into another file. Let’s look at this in detail.
In other programming languages such as C++, there is a main() function (it has to named main, or else the code doesn't compile), declared explicitly by the user, from which the execution of a program begins. In Python, the main() function is composed of all the top-level code i.e. all statements written at indentation level 0. That’s it, you don’t have to declare the main() function, Python does it by itself.
The top-level code in a Python script gets executed as soon as it is run via command line (i.e. $ python myfile.py) or run directly by executing a .py file.
The __name__ dunder attribute of a module evaluates to the name of the module itself. However, if the module is being run directly by either of the two methods stated above, then the __name__ attribute is set to the string “__main__”. This enables the user to check if the script is being run directly (command line or execution of .py file) or it is being imported. The developer can place the functionality in this if clause ensuring that importing the module doesn't trigger the functionality. Only if the module is being executed will the functionality be triggered.
Below is a simple Python script that contains a function, a print statement, and the if construct. If the script is being run directly, the function is called with a sample value, and not if the script is being imported as a module.
# prime.py def primeOrNot(num): if num <= 1: print(str(num), "is not a prime number.") return False for factor in range(2, num): # if there exists any number greater than 2 that if num % factor == 0: # divides the given number evenly(with remainder 0, that is), print(str(num), "is not a prime number.") return False # then the given number is not prime print(str(num), "is a prime number.") return True print("Top-level code being executed...") if __name__ == '__main__': primeOrNot(23) # WHEN THE ABOVE SCRIPT IS RUN DIRECTLY VIA COMMAND LINE OR BY EXECUTING prime.py Top-level code being executed... 23 is a prime number. # WHEN THE ABOVE SCRIPT IS BEING IMPORTED AS A MODULE # One way of importing the module is by going to Run Menu in IDLE window of above script > Python Shell >>> import prime Top-level code being executed...
Exploring an object: Using the builtin function dir()
The dir(object) function returns a list of all the methods and associated with the mentioned object. Everything in Python is an object, be it variables, classes, functions etc. The parent data types are also objects, so if you pass 'str' to this function, it will list every method(function) and attribute associated with it. And to gain what all do these related objects do, you can use the builtin help() function.
>>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>> help(str.replace) Help on method_descriptor: replace(...) S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
The dir() function without any argument gives you all the objects in the current scope i.e. the variables you have declared after you opened the interpreter.
>>> dir() ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'c', 'coordinates', 'd', 'dimension', 'n', 'newSetOfAnimals', 'primes', 'set2', 'set3', 'setOfAnimals', 't']
Getting help from Python: Using the builtin function help()
Python's builtin help() function provides the description of the object passed to it, in addition to its basic usage. For example, if you were to know how to use the replace() method of strings, you would execute help(str.replace).
>>> help(str.replace) Help on method_descriptor: replace(...) S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
The help() function without any argument gives you the help console, where you type the object you seek help on.
>>> help() Welcome to Python 3.4's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/3.4/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help> str.replace Help on method_descriptor in str: str.replace = replace(...) S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
Prettifying output with the builtin pprint module
The builtin pprint module enables to prettify the output while printing lists, tuples & dictionaries. This is extremely useful while having a closer look at these data structures.
>>> years = { '1960s': [1961, 1962, 1963], '1970s': [1971, 1972, 1973], '1980s': [1981, 1982, 1983] } >>> years {'1970s': [1971, 1972, 1973], '1980s': [1981, 1982, 1983], '1960s': [1961, 1962, 1963]} >>> import pprint >>> pprint.pprint(years) {'1960s': [1961, 1962, 1963], '1970s': [1971, 1972, 1973], '1980s': [1981, 1982, 1983]} >>> pprint.pprint(years, indent = 8) { '1960s': [1961, 1962, 1963], '1970s': [1971, 1972, 1973], '1980s': [1981, 1982, 1983]} >>> pprint.pprint(years, width = 20) {'1960s': [1961, 1962, 1963], '1970s': [1971, 1972, 1973], '1980s': [1981, 1982, 1983]} >>> pprint.pprint(years, depth = 1) {'1960s': [...], '1970s': [...], '1980s': [...]}
When to use which data structure
Each data structure has an array of associated methods and attributes, and each one of them can do different things. Here are a few tips on how to choose the right one to suit your needs:
- Use a list if you have a mixed collection of data, capable of being modified and added to, that you want to be able to refer to using indexes.
- Use a set if you want a collection of unique yet mutable elements, and you require to perform mathematical operations like union, intersection etc. on the elements. Also, keep in mind, that sets cannot hold mutable types such as dictionaries, sets or lists. Frozen sets work, though.
- Use a tuple if you know that your data is not going to undergo any changes, especially if you are focusing on the performance of your programs. Owing to their immutability, python knows just how much memory to allocate for the data, and hence are great for performance.
- Use a dictionary if you want to store key-value pairs, which not only implement logical associations, but also are mutable and offer a fast lookup i.e. fast retrieval of values because of custom keys. Keep in mind that sets, lists and dictionaries(all mutable types) cannot assume the roles of a dictionary key. Frozen sets can work as a dicitonary key.
- Use a frozen set if want a collection of unique elements in an immutable way.
Unpacking Iterables into individual variables
In Python, iterable objects can be decomposed into individual components in a single statement. This is called Unpacking. If you specify an incorrect number of variables while unpacking, Python will throw a ValueError.
######################## ## UNPACKING A STRING ## ######################## >>> stringTen = "Hello" >>> a,b,c,d,e = stringTen >>> a 'H' >>> b 'e' #################### # UNPACKING A LIST # #################### sentence = ["You", "can", "change", "me."] word1, word2, word3, word4 = sentence print( word1 ) # 'You' print( word3 ) # 'change' ##################### # UNPACKING A TUPLE # ##################### coordinates = (20, 50, 100) x, y, z = coordinates print(x) # 20 print(y) # 50 print(z) # 100 ########################## # UNPACKING A DICTIONARY # ########################## >>> IndianCricketTeam = dict(batsman = "V. Kohli", bowler = "B. Kumar") >>> role1, role2 = IndianCricketTeam.keys() >>> role1 'batsman' >>> role2 'bowler' >>> player1, player2 = IndianCricketTeam.values() >>> player1 'V. Kohli' >>> player2 'B. Kumar' ################### # UNPACKING A SET # ################### >>> setOfAnimals = {"cat", "dog", "elephant", "snake"} >>> a, b, c, d = setOfAnimals >>> a 'snake' >>> b 'dog' >>> c 'cat' >>> d 'elephant
Docstrings
You can provide a helpful description of your functions, classes, modules in form of docstrings. Documentation strings need to be the first line of the body of the function, class or module. These helpful strings can be viewed by calling the dunder doc attribute i.e. __doc__ attribute of the object.
>>> def countdown(): '''This function counts from 3 to 0.''' print(3) print(2) print(1) >>> countdown.__doc__ 'This function counts from 3 to 0.' >>> >>> >>> class Man(): '''A simple Man class.''' def eat(): pass def grow(): pass def sleep(): pass >>> Man.__doc__ 'A simple Man class.'
Duck Typing
Python allows the user to skip the data type of variables. It doesn't even require you to declare the return type of functions. This is in contrast to statically typed languages such as C++ & Java. This behaviour of Python is based on the Duck Test, which states that "If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck." Python recognizes the variables on the basis of values they hold.
>>> a = 'hi' >>> type(a) <class 'str'> >>> a = 5 >>> type(a) <class 'int'>
Python's Delimiters
Most programming languages such as C++ & Java, use semicolons to delimit statements and curly braces to separate code blocks. Python is an indentation oriented programming language. It uses carriage returns to delimit statements and a colon and indentation to separate code blocks.
See also: 50+ Know-How(s) Every Pythonista Must Know