Pages

Monday, August 22, 2016

Python for Salesforce Administrators - Before You Begin Programming

Python is a programming language.

It's known for being extremely straightforward to read and write.

The "plain-old" version of the Python language comes with a good number of useful commands. On top of that, though, "modules" exist that, once downloaded into the proper folder on your computer, add even more commands to Python.

Python has gained a reputation as one of the best programming languages available for ordinary people who need to quickly process data. In my opinion, here's why:

  1. It's so easy to program
  2. So many useful data-processing "modules" are available
  3. Software is available that lets you run Python programs on your computer without "installing" anything (which you might not be allowed to do at work)

That said, there are a few confusing things I should warn you about.

Python 2 vs. Python 3

The first one is that there are TWO SLIGHTLY DIFFERENT versions of the language that are in use, side-by-side, right now. One is "Python 2." The other is "Python 3."

Between version 2 & 3, the inventors of Python and its "modules" changed the way you type certain commands to make them run.

This means that a program you write for "Python 2" may not run in software that can run "Python 3" programs, and vice-versa.

But more importantly, it means that some super-cool "module" you found online may only run in software than can run one version of Python or the other. (Because all those new commands that a "module" gives you are just mini-Python-programs behind the scenes, and they have the same compatibility issues as the software you write.)

Therefore, when you put software that can run Python on your computer, I recommend having 2 copies - one that can run "programs written for Python 2," and one that can run "programs written for Python 3."

If you have trouble running a program in one piece of software, try running the same program in the other piece of software. Or Googling how to change your code to make it more appropriate for the piece of software you're running it in.

Actually ... What I really recommend is starting with software to run "Python 3" and only adding something to run "Python 2" if you need to write a program that relies on a module that's incompatible with "Python 3."

The Language

As I've mentioned, Python is a programming language that you can use to write software.

Modules

"Modules" are downloadable Python mini-programs that can save you the trouble of writing hundreds of lines of code, letting you write just a few words of code instead. As far as you're concerned, they just make the set of available commands in Python bigger.

To refer to a "module's" command in a Python program you're writing, you have to write "include ModuleNameHere" somewhere above the line where you type that command.

In addition to that, the "module" itself (the mini-Python-program) needs to be downloaded to your computer somewhere that your "software that runs Python programs" can find it.

Typically, when you download "software that runs Python programs," it comes bundled with a few dozen of the most common modules pre-downloaded where they need to be.

For example, we may make use of "csv" and "pandas" - those often come in such bundles.

If for some reason a module you need doesn't, you'll need to download it into the proper folder on your computer yourself.

To make that easier, certain "pieces of software for running Python programs" come bundled with another program called "pip" and a command-line interface for interacting with it.

If your software has this, it's by far the easiest way to download modules to your computer.

Typically, you just type something like "pip install pandas" into that special command line, and it finds it on the internet and downloads it to exactly where it needs to go.

In my instructions, I'll presume you figured out how to "pip" or download-and-correctly-unzip any modules I refer to. (Always just try "include ModuleNameHere" in your code and see if your software complains about not being able to find it before going out of your way to download a module.) Feel free to write me in the comments area if you need more help.

"Interpreters" & "IDEs"

Finally, you'll want an "IDE." Again, it often comes bundled with "pieces of software for running Python programs." IDE stands for "Integrated Development Environment."

You'll typically save your software on your hard drive in plain-text files with the extension ".py".

You can edit these files in a simple text editor like "Notepad" and then type commands to "run" these files into the command-line interface of your "software that can run Python programs" (this is an interface to its "Python interpreter").

But even better is having a text-editor with a "play button" that runs whatever you're editing and displays any textual output in another corner of the screen. And that color-codes the code you're writing so you can tell what you're doing, and that tells you when you are missing something you should have written, etc. That's an IDE!

Click here to learn more about setting yourself up in Windows or, for this blog's exercises only, following along in your web browser.

"Print" statements

Writing text to "standard output" (which means "whatever part of your screen the software running your code thinks you might see some text in if it put it there") requires using a "print" command in the Python language.

I'm not sure why, but some "software that can run Python" seems to require that if you want the phrase, "Hello World" to show up in this "standard output" area, you write the command, "print 'hello world'", whereas other software seems to want you to write "print('hello world')".

Figure out early on what your software for running Python programs prefers, and write your code accordingly. Note that Stack Overflow (a Q&A site about programming) code examples or this blog's code examples may have "print" statements written differently than you need to write them.

My Windows-Centrism

I don't have access to a Mac, or a Linux machine, so I'll be writing this blog based on using Windows.

I have both "2.x.x.x" & "3.x.x.x" versions of WinPython on my machine, unzipped to folders I created under "My Documents" (rather than "installed" on my computer). I really like all the modules, "pip," and "IDE" features bundled with it, and the fact that I don't have to "install" it on my computer (i.e. have administrator rights).

Click here for an installation step-by-step guide for Windows & "Python 3."

Here is a list to a few other such bundles.


If you're on an Apple / Linux, I apologize, as I've completely glossed over actually setting up your computer so you can run Python. Give it a try yourself, but I am here if you need me - just leave a message in the comments.


Table of Contents

No comments:

Post a Comment