Getting started with Python and Spyder

What is Python?

Python is an object oriented, interpreted programming language that is used for tasks including scientific computing, data analysis and web development.

Object-oriented programming languages organise software design around data, or objects, rather than functions and logic.

Python was created by Guido van Rossum in the 1980s who named the language after Monty Python, a group of comedians from the UK.

Monty Python and the Holy Grail gif taken from here

Nowadays it is one of the most widespread programming languages and is popular among bioinformaticians and data scientists.

How does Python manage data?

Python has built-in data structures that are used to store and manipulate data in particular ways, these are lists, tuples, dictionaries, sets and strings.

Lists

Lists, denoted by square brackets [ ] , are used to store ordered data.

You can add, remove and change items in a list (they are mutable).

Here is an example of what a list looks like in Python code:

im_a_list = ["a", "b", "c"]

Tuples

Tuples, denoted by parentheses ( ), are used to store ordered data but they are immutable, meaning that once you create a tuple you cannot change its contents.

Here is an example of tuple:

im_a_tuple = ("a", "b", "c")

Dictionaries

Dictionaries, denoted by curly brackets { } , are used to store key-value pairs.

You can add, remove, and change key-value pairs in the dictionary (they are mutable) and the ordering of the pairs is not maintained. Here is an example of a dictionary:

dictionary = {'a': 'is for aardvark', 'b': 'is for beetle', 'c': 'is for camel'}

Sets

Sets, denoted by curly brackets { }, are used to store unique items. Y

ou can add and remove items from the set (they are mutable) but the elements in a set do not maintain their order. An example of a set is shown below:

im_a_set = set("abbccc")

*note the set itself will include only the elements {‘c’, ‘a’, ‘b’}

Strings

Strings, denoted by single ‘ ‘ or double ” ” quotes, are used to store a sequence of characters.

Once you create a string you cannot change its contents (they are immutable). Below are two examples of a string with the different quotation marks:

im_a_string = "abc"
im_also_a_string = 'abc'

What about other data types?

A commonly used data structure in Python is the array. They are used to store the same data type and are mutable.

Arrays are not built-in data structures in Python but they are available through the NumPy package. Below are two examples, an array of strings and an array of numbers:

array_of_strings = np.array(['a', 'b', 'c'])

array_of_numbers = np.array([1, 2, 3])

Python modules and packages

In Python a module is a file containing Python code, which can include functions, classes, and variables that can be used in other Python programs.

Ultimately they are used to organise code, allowing you to break down your programming tasks into smaller pieces.

For example, by using the ‘import’ statement to load a module into memory you can use the functions, classes and variables defined in the module without needing to copy the code directly into your own Python script.

Python has many built-in modules, such as ‘os’ for operating system functions (user and operating system interactions) and ‘re’ for regular expression functions (pattern searches in strings) but you can create your own modules in a separate file and import them too.

A Python package is a collection of different Python modules that comes with an __init__.py  file, where the presence of this file is used to mark directories as Python package directories.