Python Lesson 1: Intro to Python

Advertisements

Hello everybody,

This is Michael, and I thought I’d introduce a new analytical tool-Python (after all, I did say in the welcome post that I would eventually include Python. Well 14 months later, here it is.). Don’t worry, I’ll continue my R and Java lessons, but I thought this would be a perfect time to introduce another tool.

So, what is Python? It’s a multipurpose programming language invented by Dutch programmer Guido van Rossum in 1991. I say multipurpose because while Python has the coding capabilities of Java and the analytical capabilities of R, Python also has several more uses, such as creating web applications or connecting to database systems.

What makes Python a good programming language? First of all, just like Java and R, it is an open-source language, meaning that it’s free to use for anybody. In other words, there’s no expensive annual license fee to use Python (and software licensing fees run at least $1000 a year). Secondly, Python has a simpler syntax than other languages (though I personally don’t find Java syntax to be that complicated), as you can write Python programs with fewer lines of code than you could with some other languages. Next, Python runs on an interpreter system, which means that code can be executed as soon as it written (which is not the case with Java), thus making programming quicker. Finally, Python can either be used in a procedural, object-oriented (like Java), or functional manner. I explained the concept of object-oriented programming in my first Java post, but what are procedural and functional programming languages? Procedural languages view the program as one long procedure, with each line of code being a step in the procedure; think of procedural programming like a recipe, where each step in the recipe is just like each line of code in the program. Two examples of procedural programming languages include C and BASIC (which became obsolete in the late 80s). Functional programming views the functions in the program (the functions being the code) as data. This means that you can use the functions as parameters, return them, create other functions from those functions and create custom functions. The functions in functional programming must be pure functions, must avoid shared state, and must be immutable. Pure functions are functions where the same type of input will always return the same type of output. Shared state is a state (I covered the concept of state in my Java posts) that is shared between two or more functions or data structures. Immutable means unable to be changed; in the context of functions, immutable refers to functions that can’t be changed once created.

Now, one thing I want to note is that for any Python lessons I will be posting, I will be using an IDE tool called Spyder. IDE stands for integrated development environment, which is essentially software for computer programmers to write their code to develop their programs. As a matter of fact, I’ve already used another IDE in my blog-NetBeans (for my Java posts). Both Spyder and NetBeans are free to use IDEs, though keep in mind that these two software tools aren’t the only IDEs for their respective languages (Python and Java). Some other Java IDEs include BlueJ and JCreator (which I’ve used before but I like NetBeans better) while some other Python IDEs include PyCharm and Cloud 9 IDE.

spyder

Now, how do you install Spyder? Take a look at this website-Spyder Website-as it will tell you how to install Spyder for your system (whether you’re using Mac, Windows, or Linux).

One thing I want to make clear is that during the installation process, you’re not technically downloading Spyder-rather you’re downloading something called Anaconda. Anaconda is a free open-source Python package and IDE manager, which means that Anaconda collects thousands of Python packages (such as numpy and pandas, which I’ll discuss in future Python posts)  as well as Python IDEs, such as Spyder. Here is the what the dashboard for Anaconda looks like:

As you can see, these are some of the tools available in Anaconda. Each tool has a description that mentions what that tool can be used to do. For instance, the Jupyter notebook serves as a “web-based, interactive computing notebook environment”. And if you’re wondering what the difference is between R-Studio and R, R is simply for statistical calculations, analytical model building, and data visualization, while R-Studio can do all of those things plus build programs using R-after all, R-Studio is an IDE.

So, let’s open up Spyder and start Python coding. Here’s what an empty Spyder console looks like:

The entire left side of the screen consists of white-space where you will write the code for your programs. The top right of the screen simply shows a message linking to a Spyder tutorial, which you will see when you open up Spyder for the first time (this was my first time opening Spyder). You can close this window down, since it isn’t relevant for your Python coding. The bottom right part of the screen is also very important, as it is the place where your code will run (or detect any errors, of which you will be informed).

Now, let’s start with something simple:

If you can’t see the picture, here’s the code:

print(“Hello World”)

And here’s the output:

Hello World

Does this example look familiar? If you read my first Java lesson, or just know Java programming, then it certainly would. I simply printed Hello World, which is usually the first thing beginning Java coders learn to do. But there are two notable differences between Python’s Hello World and Java’s Hello World. They are:

  • The function to print Hello World is simply print as opposed to System.out.println.
  • Unlike Java, there is no need for a semicolon after each line of code.

Thanks for reading, and don’t worry, we’ll get into more coding in Python Lesson 2.

Michael

 

 

 

Leave a ReplyCancel reply