Intro Python Data Analysis Part1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

A practical introduction to

Python for data analysis


Part 1: Installing Anaconda and Python variables

Lewys Brace
[email protected]
Aims of the course
• Provide you with a basic understanding of programming in Python
• Basic programming architecture and logic.
• Introduction to key packages used by Python programmers for data
analysis.
• How to conduct basic data analysis in Python.
• Focus on the more “common” aspects of Python programming.
• Learn that Google is your best friend.
An introduction to coding with Python

An introductory coding course with Python…


Interpretive vs compiled languages
• Python is an interpretive language.
• This means that your code is not directly run by the hardware. It is
instead passed to a virtual machine, which is just another programme
that reads and interprets your code. If your code used the ‘+’ operation,
this would be recognised by the interpreter at run time, which would
then call its own internal function ‘add(a,b)’, which would then execute
the machine code ‘ADD’.
• This is in contrast to compiled languages, where your code is translated
into native machine instructions, which are then directly executed by the
hardware. Here, the ‘+’ in your code would be translated directly in the
‘ADD’ machine code.
Advantages of Python?
Because Python is an interpretive
language, it has a number of
advantages:
• Automatic memory management.
• Expressivity and syntax that is ‘English’.
• Ease of programming.
• Minimises development time.
• Python also has a focus on importing
modules, a feature that makes it useful
for scientific computing.
Disadvantages
• Interpreted languages are slower than compiled languages.
• The modules that you import are developed in a decentralised
manner; this can cause issues based upon individual assumptions.
• Multi-threading is hard in Python
Which language is the best
• No one language is
better than all others.
• The ‘best’ language
depends on the task
you are using it for and
your personal
preference.
Versions of Python
• There are currently two versions of Python in use; Python 2 and
Python 3.
• Python 3 is not backward compatible with Python 2.
• A lot of the imported modules were only available in Python 2 for
quite some time, leading to a slow adoption of Python 3. However,
this not really an issue anymore.
• Support for Python 2 will end in 2020.
Anaconda - Spyder
• For this tutorial, we’ll use the Anaconda distribution of Python.
• This can be downloaded via: https://www.anaconda.com/distribution/
• Ensure you download the right version:
The Anaconda IDE…
• The Anaconda distribution is the most popular Python
distribution out there.
• Most importable packages are pre-installed.
• Offers a nice GUI in the form of Spyder.
• Before we go any further, let’s open Spyder:
Variables
• Variables in python can contain alphanumerical characters and some
special characters.
• By convention, it is common to have variable names that start with
lower case letters and have class names beginning with a capital
letter; but you can do whatever you want.
• Some keywords are reserved and cannot be used as variable names
due to them serving an in-built Python function; i.e. and, continue,
break. Your IDE will let you know if you try to use one of these.
• Python is dynamically typed; the type of the variable is derived from
the value it is assigned.
Variable types
• Integer (int) • A variable is assigned using the = operator;
i.e:
In:
• Float (float) :
Out

• String (str)
• Boolean (bool)
• Complex (complex)
• The print() function is used to print something
• […] to the screen.
• User defined (classes) • Create an integer, float, and string variable.
• Print these to the screen.
• Play around using different variable names,
etc.
• You can always check the type of a variable using the type() function.

In: Out:

• Check the type of one of your variables.


• Variables can be cast to a different type.

In: Out:
Arithmetic operators
The arithmetic operators: • Write a couple of operations
• Addition: + using the arithmetic operators,
• Subtract: - and print the results to the
screen.
• Multiplication: *
• Division: / In: Out:
• Power: **
A quick note on the increment operator
shorthand
• Python has a common idiom that is not necessary, but which is used frequently and is
therefore worth noting:
x += 1
Is the same as:
x=x+1

• This also works for other operators:


x += y # adds y to the value of x
x *= y # multiplies x by the value y
x -= y # subtracts y from x
x /= y # divides x by y
Boolean operators
• Boolean operators are useful when making conditional statements,
we will cover these in-depth later.
• and
• or
• not
Comparison operators
• Greater than: > • Write a couple of operations using
• Lesser than: < comparison operators; i.e.
• Greater than or equal to: >= In:
• Lesser than or equal to: <=
• Is equal to: ==

Out:
Working with strings
In: Out:

• Create a string variable.


• Work out the length of the string.
End of part 1

You might also like