
If you mastered the basics, you might (like me) have assumed you are a Python god who really doesn't need any more Python knowledge to complete most tasks. You know all about OOP polymorphism... that's it isn't there? Well no. Here are 7 tricks to prove there's so much more out there.
(note: random order... as each "trick" came to mind)
Feel free to ask questions below! There are no stupid questions.
1. String formatting is incredible and very useful. The basic formatting allows you to create more readable code. You can also reorder variables, use them more than once, align items, prefix with a sign and more! Check out this website for all the information.
2. Decorators. You may have come across the @ symbol if you've built web applications in Django or Flask. Effectively a decorator allows a function to be wrapped inside another function declared elsewhere. The other function runs additional, necessary code e.g. in creating a database model class in Django you can use @python_2_unicode_compatible - if you need to support Python 2. These decorators can help keep code concise.
3. Variable arguments. In Java, R and other languages the convention is to use three dots ... to denote a method accepts a any number of arguments. In Python, the equivalent is the *. You can always accept a list as a parameter but this makes it clear that the function call is accepting multiple values for the argument. You can use ** to accept a dictionary of arguments e.g. **kwargs.
4. List comprehension. Sometimes you have a list of data and need to alter it in some way e.g. converting everything to upper or lower case. You could loop through but a list comprehension is more concise and slightly more efficient. The basic format is [x for x in some_list if ..], with the test condition being optional. You can also do dictionary comprehension!!
5. Help! If you're like me, you've been coding without using the helpfunction for too long. I'm sure you learn this as part of any introductory course but it was only when using the R equivalent ?functionName (which is admittedly much better) that I remembered the same thing exists in Python. Simply use help().
6. With. The with keyword helps manage object resources by destroying them after they
have been used. You could do file_variable = open("filename", "w") and then file_variable.close() but you can easily forget to close the file. The with keyword creates concise code. There are other uses for with other than opening and closing a file. Any time you keep a resource open (loaded in memory) and then close it is a case for a with statement. See here: http://stackoverflow.com/questions/1984 ... r-and-exit which has a nice example of using the necessary special methods to create a database connection class which can be used with with.
7. Dictionary Key Lookup with .get(). When you lookup a key that does not exist, you receive a KeyError and your program terminates. You could place the lookup inside an IF statement (If key in dictionary: ) but we can also use the .get() method to nicely handle this.





















