General Python Syntax
Introduction to Python
Python is an interpreted computer language, which has become increasingly popular among data scientists. It was designed to emphasise code readability, by using whitespace indentation to define code blocks rather than curly brackets or keywords. In particular, it is widely used both at the prototyping level and professional development.
An example of the most basic operations include:
 Addition: (10+4)
 Subtraction: (104)
 Multiplication: (10*4)
 Exponentiation: (10**4 (=10000))
 Division: (10 / 4)
Note that the division (10/4) will give you (2) as both numbers are integers. To include the full digits of this operation, it is necessary to perform 10 / float(4)’
Conditional statements are widely used to select specific commands if some conditions are satisfied. The following is an example of conditional statements.
x=3
# if statement
if x > 0: # note the ":" after the conditional.
# print "positive" if x is greater than zero. The block of coding, which has to be performed if the conditional is satisfied, is indented
print('positive')
else:
print('zero or negative')
# if/elif/else statement
if x > 0:
print('positive')
elif x == 0:
print('zero')
else:
print('negative')
# singleline if statement (sometimes discouraged)
if x > 0:
print('positive')
Try to understand the above code. What do you notice?
Try to remove the indentation of the command print(‘positive’) after the if x>0: statement. What happens?
Experiment with the code and try different conditions. Also, explore different types of conditionals, such as while , etc.
Lists and Dictionaries
Lists and dictionaries are also widely used in Python, which has various methods and libraries to allow their creation, manipulation and visualisation. The following code illustrates an example of a list.
## properties: ordered, iterable, mutable, can contain multiple data types
# create an empty list (two ways)
empty_list = []
empty_list = list()
# create a list
simpsons = ['homer', 'marge', 'bart']
# examine a list
simpsons[0] # print element 0 ('homer')
len(simpsons) # returns the length (3)
print(simpsons) # print the initial list
# modify a list (does not return the list)
simpsons.append('lisa') # append element to end
simpsons.extend(['itchy', 'scratchy']) # append multiple elements to end
print(simpsons) # print the list
simpsons.remove('bart') # searches for first instance and removes it
simpsons.pop(0) # removes element 0 and returns it
print(simpsons) # print the list
del simpsons[0] # removes element 0 (does not return it)
simpsons[0] = 'krusty' # replace element 0
simpsons.insert(0, 'maggie') # insert element at index 0 (shifts everything right)
print(simpsons) # print the list
# concatenate lists (slower than 'extend' method)
neighbors = simpsons + ['ned','rod','todd']
# find elements in a list
simpsons.count('lisa') # counts the number of instances
simpsons.index('itchy') # returns index of first instance
print(simpsons) # print the list
Try to find other examples of list operations.
The following code provides an example of a dictionary.
# create an empty dictionary (two ways)
empty_dict = {}
empty_dict = dict()
# create a dictionary (two ways)
family = {'dad':'homer', 'mom':'marge', 'size':6}
family = dict(dad='homer', mom='marge', size=6)
# convert a list of tuples into a dictionary
list_of_tuples = [('dad','homer'), ('mom','marge'), ('size', 6)]
family = dict(list_of_tuples)
# examine a dictionary
family['dad'] # returns 'homer'
len(family) # returns 3
family.keys() # returns list: ['dad', 'mom', 'size']
family.values() # returns list: ['homer', 'marge', 6]
family.items() # returns list of tuples: [('dad', 'homer'), ('mom', 'marge'), ('size', 6)]
print('marge' in family) # returns False (only checks keys)
print(family) # print family
# modify a dictionary (does not return the dictionary)
family['cat'] = 'snowball' # add a new entry
family['cat'] = 'snowball ii' # edit an existing entry
del family['cat'] # delete an entry
family['kids'] = ['bart', 'lisa'] # value can be a list
family.pop('dad') # removes an entry and returns the value ('homer')
print(family) # print family
Find examples of other dictionary operations. What are the main differences between lists and dictionaries? Which do you prefer?
Functions and Loops
Functions in Python can be used to define a code fragment, which can be called at any time.
# define a function with one argument and one return value
def add_number_to_two(x): # note the ":"
return x + 2 # the return statements specifies the output of the method. Note the indentation.
print(add_number_to_two(4)) # This returns 6
# return two values from a single function
def min_max(nums):
return min(nums), max(nums)
# return values can be assigned to a single variable as a tuple
nums = [1, 2, 3]
min_max_num = min_max(nums) # min_max_num = (1, 3)
# Simple loop
for i in range(3): # the range(n) command specifies a list of values from 0 to n1
print(i)
for j in range(5):
print(add_number_to_two(j))
Find examples of other loops and functions.
Basic Statistics with Python
Let (X = {x_{1}, ldots, x_{n}})
be a set of observation. The mean is defined as
[bar{X} = frac{1}{n} sum_{i=1}^{n} x_{i}.]
For example, for (X = {1,2,3,4}),
[bar{X} = frac{1+2+3+4}{4} = frac{10}{4} = 2.5]
Using Python we have
from __future__ import division
X = [1,2,3,4]
temp = 0
for i in range(len(X)):
temp = temp + X[i]
mean = temp/len(X)
print(mean)
# Creating a function 'mean'
def mean(input_list):
temp = 0
for i in range(len(input_list)):
temp = temp + input_list[i]
mean = temp/len(input_list)
return mean
print (mean(X))
We can also use specific Python libraries, which provide offtheshelf functions and modules, such as Numpy
import numpy
X = [1,2,3,4]
mean = numpy.mean(X) # Find the mean
std = numpy.std(X) # Find the standard deviation
print(mean)
print(std)
Explore what Numpy can do.
Hard exercise! Can you implement the standard deviation for (X=[1,2,3,4]) using first principles, rather than the NumPy library?
Methods in Python
In Python, we can define fragments of code that can be invoked at any time. These are called methods and defined by the keyword def.
Consider, for example, the area of a circle (A = pi r^2),
where (pi = 3.1415926cdots) and (r) is its radius. For any circle, the only parameter that changes (A) is its radius (r). So we can write
[A(r) = pi r^2.]
In other words, by using (A(r)) rather than (A), we specify that the area (A) is a function of (r).
So, if we want to calculate the area of a circle with radius (r=2), the above Equation gives us that (A(r=2) = pi 2^2 = 4 pi = 12.57). Therefore, our input is (r=2) and the output will be the number evaluated by (A(r=2)).
This is how a method (or function) is defined in Python. We specify an input and output based on a sequence of steps. So the area equation can be written as
def A(r): # note the ":" and the indentation of the block of commands in the method
area = r*r*3.141592
return area
print A(2)
12.566368
Note the return statement. This specifies the output of the method.
The area of a square with side (l) is (A(l) = l^2). Define a method to find the area of any square.
Vectors and Matrices in Python
Python has various libraries, which allow easy manipulation of vectors and matrices. However, as an example, let us create a Python code to evaluate the sum of two vectors using first principles
def vector_addition(v_1,v_2):
sum = [] #need to initialise sum as an empty list
if len(v_1) == len(v_2): #vector addition makes sense if they are both of the same length
for i in range(len(v_1)): #consider each component
temp = v_1[i]+v_2[i] #add the ith component
sum.append(temp) #add the sum of the two components to the vector sum
else:
print("The two vectors must have the same dimension!")
return sum
a = [1,2,3]
b = [1,2,3]
print(vector_addition(a,b))
Modify the above code to evaluate vector subtraction and scalar multiplication.
Introduction to Python for Big Data Analytics
Introduction to Python for Big Data Analytics
Reach your personal and professional goals
Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CVbuilding certificates.
Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.
Register to receive updates

Create an account to receive our newsletter, course recommendations and promotions.
Register for free