Python
Similar to a lot of programmers, in 1999, I started my software development journey by building simple websites using HTML and eventually CSS. Over time, I became more ambitious, looking to implement dynamic capabilities and simple content management. This led me to learn PHP, as well as the basics of JavaScript (mostly for hacking front-end libraries).
In 2003, at university, I was forced to study Java, which I found to be a repetitive and heavy language (compared to other high-level languages). As a result, when it came time to complete my dissertation in 2007, I decided to switch back to PHP (my dissertation is available on GitHub).
Post University, I explored the world of Ruby, mainly driven by the popularity of Rails and static site generators such as Jekyll (LifeinTECH is built using Jekyll). I also went back to JavaScript, spending the time to learn the language more thoroughly, so that I could develop modern web applications using Node.js. I documented this journey in 2014 across a number of blog posts:
- JavaScript - Part One
- JavaScript - Part Two
- JavaScript - Part Three
- ES6 - Part One
- ES6 - Part Two
- Node Architecture
In recent years, I have spent most of my time developing in JavaScript and (due to work) on the Force.com platform (a combination of Apex and JavaScript). A few example applications include “Conf Buddy” and “Site Builder”. I also build some simple 2D games using GML, which is a proprietary programming language for GameMaker: Studio.
In summary, over nearly two decades I have used the following languages:
- HTML/CSS
- PHP
- JavaScript (Client-Side)
- Java
- JavaScript (Node.js)
- Apex
- GML
One language that I have never explored is Python. Therefore, I thought it was time to understand why it remains one of the worlds most popular and high-growth programming languages (based on the Stack Overflow Developer Survey 2016).
Introduction
Python is an interpreted, high-level, general-purpose programming language, released in 1991.
Python is considered fast, reliable, efficient, and easy to learn (thanks to its emphasises on code readability). One notable characteristic is its use of whitespace, as Python requires blocks to be delimited by indentation, instead of curly braces or “begin/end” keywords. This strict approach to whitespace (in theory) promotes readability, but can also be a point of contention amongst developers.
Python has a large, mature community, with strong corporate backing (e.g. Google, etc.) It has also become a “de facto standard” for Data Analytics, Machine Learning, Artificial Intelligence, and Data Science, as it well suited for data manipulation and repeatable tasks, as well as its relatively simple learning curve (when compared to R).
The language’s core philosophy is summarised in the document “The Zen of Python (PEP 20)”, which includes a collection of nineteen software principles.
There are two major versions of Python:
- Python 2 was first released in 2000 (The latest version 2.7, was released in 2010).
- Python 3 was released in 2008 but is not completely backward-compatible (slowing adoption).
The decision to develop using Python 2 or Python 3 can be tough for an established development team. However, with the official end-of-life date for Python 2 set for 2020, I would recommend anyone looking to learn Python today should start with Python 3.
Cheat Sheet
The official Python documentation is incredibly comprehensive, including a detailed Language Reference (core semantics) and Standard Library (built-in modules, etc.)
However, as part of my learning process, I always create a personal “cheat sheet”, which provides quick access to key features (variables, functions, etc.) Outlined below is my cheat sheet for Python 3.
Variables
Python has no command for declaring a variable, instead, a variable is created the moment you first assign a value to it. Also, variables do not need to be declared with a particular type.
a = 1 # Integer
b = 1.1 # Float
c = “a” # String
d = True # Boolean
Conditional Statements
Unlike other programming languages, Python does not require the expression to be enclosed in parentheses. Instead, a colon :
is required after the expression.
if x == 1:
print(“a”)
elif x == 2:
print(“b”)
else:
print(“c”)
Loops
A for
loop in Python works like an iterator method, therefore does not require an indexing variable to set.
for n in range(1, 10):
print(n)
while n < 10:
print(n)
n += 1
Functions
A function is defined using the def
keyword.
def increment(number, by=1):
return number + by
Lists
A list is a collection which is ordered, changeable and allows duplicates. Lists are written with square brackets.
# Creating a list
letters = ["a", "b", "c", "d"]
# Looping over lists
for letter in letters:
# Adding items
letters.append("e")
# Removing items
letters.pop()
letters.remove("d")
Sets
Similar to lists, a set is a collection which is unordered and unindexed. Lists are written with square brackets.
first = {1, 2, 3, 4}
second = {1, 5}
Dictionaries
A dictionary is another collection type, which is unordered, changeable, and indexed. Dictionaries are written with curly brackets and include keys and values.
point = {"x": 1, "y": 2}
point = dict(x=1, y=2)
Arrays
Similar to other languages, arrays are used to store multiple values in a single variable.
from array import array
numbers = array("i", [1, 2, 3])
Classes
As Python is an object-oriented language, a class is like a blueprint for creating objects.
# Creating a classes
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
...
Exceptions
When a Python script raises an exception, it must handle the exception immediately or the program will be terminated. Exception handling can be achieved using the try:
block and except
statement.
# Handling Exceptions
try:
...
except (ValueError, ZeroDivisionError):
...
else:
# no exceptions raised
finally:
# cleanup code
# Raising exceptions
if x < 1:
raise ValueError(“…”)
# The with statement
with open(“file.txt”) as file:
...