20 useful tricks & tips in Python.
A Thread 👇
We can specify an optional 2nd argument in Python's round function.
If we pass a negative value here, this will start rounding from the left of the decimal point; meaning we can round to the nearest 10, 100, and so on.
There's an extremely easy way to reverse strings in Python using slicing.
Below we use [::-1]
This means start at the end of our string, and end up at position 0.
The join string method joins elements of a string, list or tuple with a string separator of our choice.
This could be useful if we have a list of strings we wish to concatenate into one single string.
We can also use filter & lambda to extract items from an iterable before using join.
The filter function filters items through a function.
Below, we pass it a lambda function (function defined without a name) which filters our list.
We can open up URL's in Python easily with the webbrowser module 🙂
When opening files, we can use the with statement.
This will guarantee the file is closed at the end of the code block, even if an exception is raised.
The time module helps us keep track of how much time has passed from one point to another.
This is useful when assessing how long a chunk of code or function takes to run 🙂
The underscore ( _ ) can be used as a "throwaway" variable name.
When iterating, like in the example below, it indicates that the loop variable isn't used.
Docstrings are string literals that appear within functions, methods, classes, or modules.
These are a form of documentation, describing what a code block does.
We can access them using Python's help function or doc method.
Using Python's dictionary items method, we can easily return the key-value pairs of a dictionary.
Often in programming, when we want to swap 2 variables, we need to introduce a 3rd temp variable.
In Python, we can just do the following 🙂
We might want to create a new list, iterate through an old list, and append to our new list based on some condition.
With list comprehension, we can achieve this in one simple line:
Want to speed up your code?
With Python's multiprocessing module, we can split work across separate processes.
Running several processes at the same time is faster than running them one after the other.
Everyone should start using virtual environments.
We can easily set these up in Python.
These are useful when working on multiple projects, each of which requires different versions of third party libraries.
If we have two lists, and want to generate a dictionary from them, with one list as keys, and the other as values, we can achieve this simply with Python's zip function 🙂
We can easily merge dictionaries together in Python using the below syntax. Give it a try 🙂
This allows you to iterate over an object while keeping track of how many iterations have occurred.
Enumerate starts counting at 0 by default, but we can change this to whatever we want.
F-strings are a great way to format strings.
They make our code more concise and readable (as well as faster!)
Want to remove all duplicates from a List?
Just convert the List to a Set (these don't allow duplicate values) and the convert back to a List. Simple 🙂
If you're writing large numbers in Python, it can be difficult to read, and we can't use commas without just converting the number to a string.
Instead, use underscores 🙂
The behaviour of the two integers below is identical.
That's it 😀
If you liked this, or learned something new, consider liking, retweeting, and following me for more Python and SQL content.
And as @razacodes would say, go and code some Python!