Tuesday, December 27, 2016

A Taste of Things to Come

Happy holidays!

I wanted to give a little preview of the things to come, and explain where I plan to take this blog in 2017. My current plan is to focus the blog posts on three areas, with the majority in #1 or #2:
  1. Learning Python (aka Getting Started with Python4CPAs): Learning oriented posts focused on helping newcomers to Python get started. The examples will be geared toward accountants, however they will be applicable to anyone looking to learn Python. 

  2. Using Python in Accounting: These posts will mostly explore the meat of where Python really shines, and where it will be the most awesome for CPAs, which are the modules. The quick and dirty: you can "import" various modules into your code, and add the ability to implement some incredibly powerful capabilities with just a few lines of code.  Python at its core comes with standard modules in its "standard library", which has tons of awesome things in it. Then there are the 3rd party modules, which can be easily downloaded from pypi, which currently has over 95,000 Python packages! We'll explore which modules from both the standard library and pypi are most useful and how to use them, and then how to link them together.

  3. Everything Else: the other posts will be for posts that don't fit into the first two categories, like this post, or real life examples of Python in accounting, general observations, or other big picture topics.


What's comping up the pipeline?


Here's a sample of what we've got planned:

Learning python
  • Python basics: lists, dictionaries, tuples, and functions
  • Resources for Python learning outside the scope of this blog
  • Installing and managing 3rd party packages

Using Python in Accounting
  • Jupyter Notebook - Documenting code, analysis, and results, and how it can be used in an audit
  • Pandas (aka the Excel killer) - Importing, analyzing, and viewing tabular data 
  • Importing PDF files using regular expressions
  • Pyautogui - Automating repetitive tasks to save time and sanity

As always, suggestions and questions are welcome. 
Follow me @python4cpas and @dtizzlenizzle!


Cheers!
Daniel

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