Subtracting years from a date in Python
Renan Moura
by Renan Moura
2y ago
The easiest way to subtract years from a date in Python is to use the dateutil extension. Install it with pip: pip install python-dateutil The relativedelta object from the dateutil.relativedelta module allows you to subtract any number of years from a date object. In this example I always take the current date using the date.today() method. Then I set a relativedelta of 2 years and subtract it from current_date. from datetime import date from dateutil.relativedelta import relativedelta current_date = date.today() print(current_date) future_date = current_date - relativedelta(years=2) p ..read more
Visit website
Adding years to a date in Python
Renan Moura
by Renan Moura
2y ago
The easiest way to add years to a date in Python is to use the dateutil extension. Install it with pip: pip install python-dateutil The relativedelta object from the dateutil.relativedelta module allows you to add any number of years to a date object. In this example I always take the current date using the date.today() method. Then I set a relativedelta of 2 years and add it to the current_date. from datetime import date from dateutil.relativedelta import relativedelta current_date = date.today() print(current_date) future_date = current_date + relativedelta(years=2) print(future_date ..read more
Visit website
How to subtract months from a date in Python
Renan Moura
by Renan Moura
2y ago
The easiest way to subtract months from a date in Python is to use the dateutil extension. Install it with pip: pip install python-dateutil The relativedelta object from the dateutil.relativedelta module allows you to subtract any number of months from a date object. In this example I always take the current date using the date.today() method. Then I set a relativedelta of 2 months and subtract it from current_date. from datetime import date from dateutil.relativedelta import relativedelta current_date = date.today() print(current_date) future_date = current_date - relativedelta(months=2 ..read more
Visit website
Adding months to a date in Python
Renan Moura
by Renan Moura
2y ago
The easiest way to add months to a date in Python is to use the dateutil extension. Install it with pip: pip install python-dateutil The relativedelta object from the dateutil.relativedelta module allows you to add any number of months to a date object. In this example I always take the current date using the date.today() method. Then I set a relativedelta of 2 months and add it to the current_date. from datetime import date from dateutil.relativedelta import relativedelta current_date = date.today() print(current_date) future_date = current_date + relativedelta(months=2) print(future_d ..read more
Visit website
Beware of excess of “best practices”
Renan Moura
by Renan Moura
2y ago
Unlike other disciplines that are more rigid and regulated like Civil Engineering, Software Engineering doesn’t have a set of rules to follow by law enforcement. You won’t go to jail if you don’t do TDD (Test-Driven Design), or even write tests for your code. Your system can work just fine if you don’t follow any agile method. You could deploy your next app in a bare-metal Linux machine instead of setting up Kubernetes on a cloud provider to autoscale your pods, and no one will complain about performance and such. We have all read Clean Code, Clean Architecture, and some other "bibles" of the ..read more
Visit website
The hard part is to continue
Renan Moura
by Renan Moura
2y ago
Starting out is easy, the hard part is to continue. Anything for a while is easy: Work out for a while Study for a while Diet for a while Work right for a while Take good care of the family for a while Save for a while Programming for a while For a while, everything is a piece of cake. But what will get you there is not being brilliant for a while. What takes you there is being consistent for a LONG time. James Clear has an interesintg take on this. On the image below you can see how a tiny gain makes such a huge difference when compounded for long enough. And also the opposite, how a tiny l ..read more
Visit website
Python list drop duplicates
Renan Moura
by Renan Moura
2y ago
Removing duplicates from a list is a task that might happen more often than you think. Maybe you are importing a bunch of rows from a CSV file and want to make sure you only have unique values. Or you are making sure to avoid repeated values for the sake of keeping your data sanitized. Fortunately, you can drop duplicates from a list in Python with a single line. This is one of those simple, but powerful features that Python gives us for free and can save you a lot of trouble by applying the Pythonic way of doing things. Removing duplicates with set In the code snippet below we are creating a ..read more
Visit website
Learning Programming is Non-Linear
Renan Moura
by Renan Moura
2y ago
Learning is by no means a liner process, even in hard sciences like Math. It is very common to see people asking in groups, reddit, and other forum-like places "What path should I take to become a Software Developer". Unfortunately, learning programming is not linear. You will find many lists and articles giving you a path like: learn HTML, CSS, JavaScript… They are not wrong in the sense that you should learn JavaScript before React, but they don’t warn you that you should not worry about mastering the previous topic before jumping to the next. Those topics are all interconnected, which means ..read more
Visit website
Python for loop increment by 2
Renan Moura
by Renan Moura
2y ago
A regular for loop will increase its iteration counter by one in each iteration. But there are situations where you want to increment the iteration counter by 2. For some reason you might want to work with only even values. Let’s see a few solutions for this. range function The solution for to increment a for loop by 2 in Python is to use the range() function. This function allows you to specify three parameters: start, stop, and step. start is the counter’s initial value, step is by how much you want to increment until you reach the value of stop - 1, because the value of stop is included. Th ..read more
Visit website
Python 1 line for loop
Renan Moura
by Renan Moura
2y ago
Instead of using the same old way of iterating through lists, we can make our code simpler by using list comprehensions, which allow us to make a 1 line for loop in Python. Basic syntax of a 1 line for loop To use a one line for loop to replace a regular for loop, we can make: [expression for item in list] Which is the same as doing: for item in list: expression If we want some conditional to apply the expression, we have: [expression for item in list if conditional ] Which is the same as doing: for item in list: if conditional: expression Example 1: calculating the cub ..read more
Visit website

Follow Renan Moura on FeedSpot

Continue with Google
Continue with Apple
OR