pattern = re.compile(r'^\w+ (\w+) (\w+)', re.M)

We use the re.M flag which will search the entire paragraph for the match words.

Now let us try out the program above…

gad = pattern.findall("hello mr Islandt\nhello mr gadgets") print(gad)

…which will then display the following outcome

[('mr', 'Islandt'), ('mr', 'gadgets')]

Explanation :

The program above will look for two words in the first line and keeps them under a tuple and when the program meets the new line character it continues the search in the second line and return another tuple, both of the tuple will include inside a list. Using re.M flag the search will go on for multiple lines as long as there are more matches out there!

]]>import re # Extract the username without @ pattern = re.compile(r'(?<=\B@)[\w_]+') gad = pattern.findall("hello mr @gadgets") print(f'Hi, {gad}') # Hi, ['gadgets']

The Twitter username only allows words, numbers and underscore…

]]>Let’s say the phone number has been written in this manner.

123-456-7810.

The below python program will group the numbers by taking out both the ‘-‘ sign and ‘.’.

find = re.compile(r'\d+(?=\-|\.)') match = find.findall('123-456-7810.')

The match array now stored the following numbers:-

['123', '456', '7810']

Next what if I just want to extract all the numbers without the ‘-‘ sign? Then use the below program.

find = re.compile(r'\d+(?!\d)') match = find.findall('123-456-7810')]]>

How to use this application :

1) Enter the amount of the present or future sum of money into the Amount text box.

2) Enter the length of the period you want to calculate into the Period text box.

3) Select the interest rate per interest period from the combo box.

4) If you want to calculate the future sum of money then you can check the Present Sum check box or else leave it unchecked!

5) Click the calculate button to calculate the present or future sum of money.

Let us try out these examples:

1) You have put $500 worth of US dollars in the cryptocurrency bank with a 15% annual interest return, how much money will you get at the end of the third year?

Enter the data into the application as shown below and click the calculate button.

2) If you want to make $10,000 US dollars at the end of three years in the cryptocurrency bank with a 3% annual interest return, how much money do you need to put in that cryptocurrency bank at this moment?

Enter the data into the application as shown below and click the calculate button.

Latest feature:

1) Uniform Payment Series, A:- Use this to find the future sum of money you will get if you invest the uniform amount of money for the n period of time!

How to use it:-

1) Enter the uniform amount into the Amount text box

2) Enter the Period of time

3) Select the interest rate

4) Check the Uniform Series checkbox!

Example:

You have entered a uniform amount of money (300 USD), how much money will you get at the end of 3 years if the interest rate per interest period is 5%?

This page will keep on updating so stay tuned!

]]>In this article, I will start building the application with the first mission, which is to create an application that will take the user inputs and then output the present or future sums of money, the formula to find the sum of money will be based on the previous solution which you can read on my previous article.

I will use wxPython to create the entire financier project, if you want to know more about this python’s third-party module you can either visit its homepage or read my previous wxPython articles about it!

Below is the entire python program used to create an approximate user interface that will accept the user inputs and then calculate the present and the future sum of money. This is more or less like a draft program which I will modify later as the project progresses before uploading it to GitHub for you all to enjoy! This project is a big project and this calculator will include various functions to do various financial calculations!

As you can see from above after you have entered the amounts of money, specify whether you want to find the present or future sum of money, entered the total interest period, and select the interest rate per interest period, the program will then go to work to find the result and displayed it in the form of message box (this is only a draft program thus the outcome will certainly not in the form of message box but instead in a more appropriate form later).

Below is the entire program, hope you will enjoy it.

import wx class CalPro(wx.App): def OnInit(self): self.the_frame = wx.Frame(None, -1) # create the top frame self.the_frame.Show(True) return True def OnButton(self, event): self.amout = int(self.amt.GetValue()) # get the amount entered self.year = int(self.n.GetValue()) # get the years entered self.percentage = self.combo1.GetValue() # get the percentage if self.percentage == "3%": self.pct = 0.03 elif self.percentage == "6%": self.pct = 0.06 elif self.percentage == "12%": self.pct = 0.12 else: self.pct = 0.16 self.present_or_future = self.chkbox1.GetValue() self.total = 0 if(self.present_or_future == True) : self.total = round(self.amout * pow((1 + self.pct), self.year), 2) else: self.total = round(self.amout / pow((1 + self.pct), self.year), 2) self.CreateMessageBox(self.present_or_future, self.percentage, self.year, self.total, self.amout) def CreateMessageBox(self, present_or_future, percentage, year, total, amount): self.message = "" if(present_or_future == True): self.message += "With " + percentage + " interest rate per interest period " + "you will receive " + "$" + str(total) + " at the end of " + str(year) + " interest period" + " if you invest " + "$" + str(amount) + " at this moment!" else: self.message += "With " + percentage + " interest rate per interest period " + "you will receive " + "$" + str(amount) + " at the end of " + str(year) + " interest period" + " if you invest " + "$" + str(total) + " at this moment!" wx.MessageBox(self.message, 'Present Future Value', wx.OK | wx.ICON_INFORMATION) def CreateWidgets(self): # create various widgets self.CreateButton() self.CreateCheckBoxes() self.CreateComboBox() self.CreateTextBoxes() def CreateComboBox(self): # create combo box cif = ["3%", "6%", "12%", "16%"] # compound interest factors self.percentLabel = wx.StaticText(parent=self.the_frame, id=15, label="Interest Rate : ", pos=wx.Point(10, 150), size=wx.Size(width=80, height=100), style=0, name="rate") self.combo1 = wx.ComboBox(parent=self.the_frame, id=3, value="3%", pos=wx.Point(90, 150), size=wx.Size(width=50, height=100), choices=cif, style=0, name="week day") def CreateButton(self): self.the_button = wx.Button(self.the_frame, id=1, label="Calculate", pos=wx.Point(150, 350), name="Hello!") self.the_button.Bind(wx.EVT_BUTTON, self.OnButton) def CreateCheckBoxes(self): self.chkbox1 = wx.CheckBox(self.the_frame, id=2, label="Present Sum", pos=wx.Point(190, 150), size=wx.Size(width=100, height=20), style=0, name="check1") def CreateTextBoxes(self): self.amtLabel = wx.StaticText(parent=self.the_frame, id=13, label="Amount : ", pos=wx.Point(10, 50), size=wx.Size(width=100, height=30), style=0, name="label") self.amt = wx.TextCtrl(parent=self.the_frame, id=20, value="", pos=wx.Point(65, 50)) self.nLabel = wx.StaticText(parent=self.the_frame, id=13, label="Interest Period : ", pos=wx.Point(200, 50), size=wx.Size(width=120, height=30), style=0, name="label") self.n = wx.TextCtrl(self.the_frame, id=21, value="", pos=wx.Point(290, 50), size=wx.Size(width=60, height=23)) wxapp = CalPro() wxapp.CreateWidgets() # initialise all widgets wxapp.MainLoop()

Now let us find the present and the future sum of money using this application.

1) Now let us try to enter the below parameters into the application, the present sum of money p is $600, the interest rate per interest period is 3% and the interest period is 3 as well. You will need to tick the Present Sum checkbox if the present sum of money is a known value.

Click on the calculate button below to display the result.

2) Let’s find the present sum of money you need to put into your bank account if you expected $500 inside your bank at the end of the first year with 6 percent annum bank interest. Since what you want now is to find the present sum of money when the future sum of money is a known amount then you will need to uncheck the Present Sum checkbox.

Click on the calculate button to display the result.

As per expected, the program did what it supposes to do which was to calculate the present and the future sum of money and presented it to the user!

I will continue to develop this application and when I have uploaded it to Github I will let you all know through this website.

]]>

Before I start to write the program, let us understand how are the future sum and the present sum of money relates to each other first.

Let’s say you have deposited $100 into a bank with a 6 percent yearly interest rate, then after a year you will have $100 + (0.06) * 100 in your bank account, which is F = P(1+i) where F is the future sum of money, P is the present sum of money and i is the interest rate per annum. If you keep the money in the bank then at the end of the second year you will get F = P(1+i)(1+i)! There is actually a formula that is used to find the future sum of money and the formula is F = P(F/P, i,n) where i is the interest rate per interest period, and n is the number of years, months, etc.

Now let us write the python program which can be used to calculate the future sum of money as follows:

def calculateFutureSum(p, i, n): f = p * pow((1+i),n) print(f)

Now let us try to enter the below parameters into the function, present sum of money p is 600, the interest rate per interest period is 3% or 0.03 and the interest period is 3 as well.

calculateFutureSum(600, 0.03, 3)

Then the program will generate the below answer.

655.6362

This means if you deposited 600 dollars into a bank now with a 3% interest rate per annum you will get 655.64 dollars at the end of the third year which is not that interesting as compared to use those money to invest in something else such as lending to your friend with 3% quarterly interest rate.

How about if you know the future sum of money and you want to find the present sum of money, for example, how much money that you need to put inside a bank if you expected $500 in your bank at the end of a year with 6% interest rate per annum? Then it is simply using this formula P = 500/(1+0.06) or simply P = F(P/F, i, n). Instead of create another function to calculate the present sum of money, let us create a unified function to calculate both of them.

def calculateFutureSum(amount, i, n, present): if present: f = amount * pow((1+i),n) print(f) else: p = amount / pow((1+i),n) print(p)

With this updated version of the function, you can find both the present value and the future value by just specifying whether the present boolean is true or false, if the present sum of money is unknown then the present boolean value is false and the amount simply means the future sum of money and vice versa!

Let’s find the present sum of money you need to put into your bank account if you expected $500 inside your bank at the end of first year with 6 percent annum bank interest.

calculateFutureSum(500, 0.06, 1, False)

The answer is $471.698!

Actually, this is a small part of the latest project I am going to create with the help of wxPython or Tkinter thus stay tuned for this latest project of mine!

]]>The program is as follows:-

import matplotlib.pyplot as plt brand = ['Slipper', 'High Heel', 'Sports Shoe'] quantity = [20, 30, 60] plt.figure(figsize=(6, 3), facecolor="brown", edgecolor="red") plt.subplot(3,1,(1, 2)) plt.scatter(brand, quantity) plt.suptitle('Sale of Shoes') plt.show()

The ‘figsize’ parameter of the plt.figure(figsize=(6, 3)) function is used to specify the size of the chart.

Play around with the parameters within the subplot function to plot the best chart as you wish.

The suptitle function will display the title of the chart.

Run the above Matplotlib program to get the above outcome.

]]>import matplotlib.pyplot as plt fig, ax = plt.subplots() wear = ['Slipper', 'Sport Shoe', 'Snicker', 'High Heel'] counts = [100, 150, 35, 16] bar_labels = ['Slipper', 'Sport Shoe', 'Snicker', 'High Heel'] bar_colors = ['tab:green', 'tab:brown', 'tab:blue', 'tab:red'] ax.bar(wear, counts, label=bar_labels, color=bar_colors) ax.set_ylabel('Wear Sale Quantity') ax.set_title('Wear Sales') ax.legend(title='Wear Sale') plt.show()

Just like before, I am using the matplotlib.pyplot module to help me to plot the below chart. After calling the subplots method two objects have been returned and I am going to use the second object to set up all the elements within the bar chart which is displayed as follows:-

]]>First of all, just like other third-party python libraries, you will need to import Matplotlib into your python program.

import matplotlib.pyplot as plt

As you can see I am using the matplotlib.pyplot module to plot the below graph.

plt.ylabel('Total Daily Sales') plt.xlabel('Five Day Sale') plt.title("Shoe Sale Data") plt.show()

I have set the y and x labels as well as the title of the graph. The show method of that module will display the graph on the screen!

With only a few lines of code, I can actually plot the entire data points and display them on the graph!

]]>In this example tutorial I am going to create a message box that will return the answer from the user after the user has either clicked on the yes or the no button on the message box.

This example is not that different from the previous one except this time I have created the yes or no message box instead of the normal message box.

def showHello(): if chVarName.get() == 1: pass else: hello_label.configure(text=helo.get() + " " + slogan.get()) # change the text inside the label yesno = msgbox.askyesno("Hello World", "Hello Again World?") # message box will get answer from user print(yesno)

As you can see if the user has clicked on the button and the check box has been unchecked (refer to the previous program) then the above function will create the message box which will print the output depending on whether the user has clicked on the yes or the no button.

The outcome is as follows:-

If I pressed the Yes button then the message box will return True or else if I pressed the No button then it will return false and exit accordingly.

]]>