Friday, December 2, 2016

Getting Started with Python4CPAs: Step 2 - Write a Simple Program!

Hello again, fellow CPAs and accountants, it's time to write some code! If you haven't already done so, download and install the Anaconda Python 3 distribution package.





NOTE: If you are new to programming, definitely start the journey here. If you've done some programming before and know a little Python already, feel free to skip ahead to a later step.




Let's Get Coding!



We will start with a simple Python editor called IDLE that you should now have on your machine.

To start, open Windows run (windows key + r), type in 'idle', then hit enter.


Starting IDLE in Windows


🍎If you're a mac person, open terminal, type 'idle3', then enter.🍎



Here's what your shell should look like. Make sure it says "Python 3.X", and not "Python 2.X"
Python 3.x Shell


From the Python Shell, go to File > New File

This opens up the Editor window of Python IDLE. Now you have two windows open: one for the editor, and one for the shell, where the results of the code will be displayed.

Arrange your editor and shell as shown above so they're easy to work with.




From the editor window, save the file with the name 'program.py' or any name you like.


Hello, World!


Let's get started with the old tried and true "Hello world" program. The traditional version of this program prints the statement "Hello, world" to the screen. But because we are Python4CPAs, we get to have a little fun and I'll add an accounting twist. In writing these seven lines of code, you will learn about:
  • creating and saving a new python file in IDLE
  • accepting a user input() and storing it in a variable
  • performing a math calculation and storing the result in a new variable
  • the print() statement
  • string format() method

In the editor, type the following
(See if you can guess what it does as you type it. )

name = input('What is your name? ')
debit = 5000
credit = 3500
net_balance = debit - credit
print('Hello, {}!'.format(name))
print('Debits are {} and Credits are {}'.format(debit, credit))

print('Net balance is {}. Laterz!'.format(net_balance))

Once you've typed in your code as above (if you're lazy you might have copied/pasted), hit F5 to run the file. It will make you save first, and then it will run in the Shell window. When it prompts you, type in your name!




Did it work?

If you entered the code as shown above, the shell should display something like this:



CONGRATS! You just wrote a program!


Here's a little breakdown on how the program works.


name = input('What is your name? ')

The input() prompts the user to type something in, and by using a variable name and '=', the user's input is saved to the variable called "name"



debit = 5000
credit = 3500 


Here we are assigning the numbers 5000 and 3500 to variables debit and credit. These are called "int" type, or integers, and they do not have decimals. We'll get much heavier into numbers in the next few sessions.



net_balance = debit - credit

Python can do math, hooray! Here we are subtracting the credit variable from the debit variable, and putting the resulting calculation into a variable called "net_balance".



print('Hello, {}!'.format(name))
print('Debits are {} and Credits are {}'.format(debit, credit))
print('Net balance is {}. Laterz!'.format(net_balance))

Here are our three print() statements. Print statements must use parenthesis, and any strings printed must be within quotation marks (Python accepts single or double quotes).

We've gotten a bit fancy here and used a neat feature called the format() method. Format() allows you to insert open/closed brackets within a string as a placeholder, and then you can pass variables through to those placeholders by putting them in the parentheses of format(), separated by commas. If we just wanted to print a simple hello, it would just be print('Hello')

Note how the format method is attached to the string with a period. This is common in programming languages and python as well, and just means we are calling the format method on the string. But you don't worry about all that today. For now, just get it to work, print out the results, and put them up on your fridge.


I encourage you to play around with this program, change the numbers, change the print statements, and just have fun. That's the best way to learn something new.


Great job!


Well, that's it for this one. Next time we'll get more advanced, and before too long we'll get you doing something that actually might be useful in your job, hopefully!

Questions? Suggestions? Please leave me some feedback! 

Also please sign up for the RSS feed, and follow me on twitter - @python4cpas and @dtizzlenizzle

5 comments:

  1. Thanks for this! Really looking forward to future posts!

    ReplyDelete
    Replies
    1. Thanks Petr, hoping to get the next one out very soon!

      Delete
  2. I'm a CPA and I'm in the process of learning Python. This seems like it will be very useful. Thanks for running this blog!

    ReplyDelete
    Replies
    1. Awesome! I'm way behind but still planning on keeping this going. Keep watching it!

      Delete
  3. Hi Daniel,
    You're my angel! Thanks for this blog and I'll follow your progress as I'm learning Python myself as a new CPA.

    ReplyDelete