How to get started with Python

Learning to program is both fun and frustrating. The key is to just play in the sandbox. Don’t be afraid to mess up.

To get started with the programming language Python, several tutorials suggest using Anaconda, a free distribution of Python that includes 195 of the most popular Python packages which contain helpful mathematical and statistical functions.

Anaconda includes iPython notebook, precisely the sandbox beginners should be playing in. This is what it looks like:


Hello World

Every programming tutorial starts with the simple exercise of printing “Hello World.” The first “Hello World” is thought to have been programmed into an internal memo by Brian Kernighan at Bell Labs in 1972.

Let’s try “Hello World” in Python. Everything from here on is being done in iPython notebook.

Type this into the first cell.

x="Hello World"

print x

Run the cell by clicking the play button in the menu. This will print Hello World when you run the cell. You should get this:

Hello World

Welcome to programming.

What’s going on here? The line x=”Hello World” is defining x as a string “Hello World”. It’s helpful to unpack in your head everything that the string “Hello World” includes: 11 characters (including the space), two capitalized letters, an “e” in position two, etc.


Let’s append some more words to “Hello World” for fun. This will likely remind you of basic algebra, but the “+” sign isn’t adding the strings but rather joining one to the other, something called concatenating. Excel users should be familiar with that function.

x="Hello World"
y=x+", I hope you are having a good time."

print y
Hello World, I hope you are having a good time.

Interrogating your string

Python can perform a number of functions on your string. Writing dir(x) will print a list of objects that you can use to interrogate x. This isn’t always a complete listing, but rather some of what Python deems the most relevant.

x="Hello World"

Let’s play with a few of these objects. x.swapcase(), for example, will swap all uppercase characters with lowercase characters, and vice versa.

x="Hello World"

print x.swapcase() 

What if we want to find out how long the string is? The object len(x) will find out how many characters “Hello World” has, including spaces.

x="Hello World"

print len(x)

x.find(‘e’) will look for the character “e” and return the position in which it sits. If we print x.find(‘e’), we will return 1, which actually means that the character “e” sits in the second position. That’s because in Python, 0 is actually the first position and 1 is the second. Remember that.

x="Hello World"

print x.find('e')

True or False

Objects in Python can be tested for their truth value, where the statements True or False are returned. This is known in math as Boolean logic. Notice below how I’ve “commented” inside the code. Whatever I write on the same line after “#” will be a comment and not executed in the script.

x="Hello World"
"z" in x 
# Here we are looking to see if the character z exists within the string "Hello World." 
# If we hit run, the world "False" will be returned. 

Boolean functions come in handy when writing more complicated code that involves if, else, while, and, or, and not. True and False won’t usually be printed in these more involved functions, but rather be part of the internal machinery and help propel the code down preprogrammed paths.

DON’T MISS  How to install Babel (or other packages) in Sublime Text 3

That wraps up our first tutorial. We don’t want you to bite off more than you can chew. Please leave comments/suggestions/complaints below!

Aleszu Bajak

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest from Storybench

Keep up with tutorials, behind-the-scenes interviews and more.