Tutorials

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:

ipython

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.

Concatenating

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"
dir(x)
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__getslice__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '_formatter_field_name_split',
 '_formatter_parser',
 'capitalize',
 'center',
 'count',
 'decode',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'index',
 'isalnum',
 'isalpha',
 'isdigit',
 'islower',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

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() 
hELLO wORLD

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)
11

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')
1

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. 
False

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 explore and manipulate a dataset from the fivethirtyeight package in R

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.