What is Python Functions, Data Types & Type Sequences in Data Science? Day 2
This blog series has been integrated with jupyter notebooks. These notebooks allow you to divide your code up into what they called cells and execute these cells on demand. You can download and install, python, jupyter, all of the associated libraries you need for this course on your own computer.
The nice thing about the jupyter notebook is that they offer semi-permanent storage for your learning and you can experiment and play with programming without having to install anything on your computer.
Let’s dig into some basic python. First, Python is a high-level language, which means it is optimised for reading by people instead of machines. It is also an interpreted language which means it is not compiled directly to machine code and importantly is commonly used in an interactive fashion.
This might be quite different if you are used to programming in something like C or Java, where you can write code, compile it, run it and watch the output.
In python, you can start the interactive interpreter and begin writing code, line by line, with the interpreter evaluating each statement as you write it. This turns out to be very useful for tasks that require a lot of investigations, versus those who require a lot of design.
The first two set of variables x and y, each to be some integer value. Then we will do some addition. We can run this cell by hitting shift + enter or by clicking on the play head icon in the toolbar. The output of the statement is immediately printed.
We can see that after executing a cell, a new empty cell is created for us below. If we just put in x and execute, we get the value of 1. So it is important to know that the python interpreter is stateful. That is, that you variables exist between cells.
Beyond that, if we go back and change something in a previous cell, we have to re-execute the script to make those changes take place in jupyter.
You will notice that python does not require the use of keywords like var to declare a variable name or semicolons at the end of the lines which are commonly used in other languages.
You can see that def statement indicates that we are writing a function. Then each line that is part of function needs to be indented with a tab character or a couple of spaces.
Because we are in an interactive environment, when the statement is evaluated on a shift + enter, the results are printed out immediately below.
In the above image, we wrote the add numbers function to take three parameters, but we could set the last parameter to be none by default.
This means that you can call add numbers with just two numbers or with three and you do not have to rewrite the function signature to overload it. This is an important implication.
A final word on the basics of functions in python. In python, you can assign a variable to a function. This might seem either completely normal to you or completely odd depending upon on your programming background.
Types and Sequences:
The python language has a built-in function called type which will show you what type a given reference is. Some of the common types include strings, the none types as we have described. Integers and floating-point variables.
A lot of python’s built around different kinds of sequences or collective types. There are three native kinds of collections that are tuples, lists and dictionaries.
A tuple is a sequence of variables which itself is immutable. That means that a tuple has items in an ordering but that it cannot be changed once created. We write tuples using parenthesis, and we can mix types for the contents of the tuple. Here is a tuple which has four items. Two are numbers and two are strings.
Lists are very similar but they can be mutable, so you can change their length, number of elements and the element values. A list is declared using the square brackets.
There are a couple of different ways to change the contents of the list. One is through the append function which allows you to append new items to the end of the list. Both lists and tuples are iterable types, so you can write loops to go through every value they hold.
Lists and tuples can also be accessed as arrays might in other languages, by using the square bracket operator, which is called the indexing operator. The first item of the list starts at position zero and to get the length of the list, we use the built-in len function.
There are some other common functions that you might expect like min and max which will find the minimum or maximum values in a given list or tuple. Python lists and tuples also have some basic mathematical operations that can be allowed on them.
The asterisks repeat the value of a list. A very common operator is the operator. This looks at set membership and returns a Boolean value of true or false depending on whether one item is in a given list.
The most interesting operations you can do with lists are called slicing. Where the square bracket array syntax for accessing an element might look fairly similar to that which you have seen in other languages. In python, the indexing operator allows you to submit multiple values
The first parameter is the starting location, if this is the only element the one item is return from the list. The second parameter is the end of the slice. It is an exclusive end so if you slice with the first parameter being zero the next parameter being one, then you only get back one item.
One handy aspect of python is that all strings are actually just lists of characters so slicing works wonderfully on them. Here is an example. When we run x or x[0:1] we get just the first character of the string.
But when we run x[0:2], we get the first two characters of the string. Our indexing values can also be negative which is really cool. And this means to index from the back of the strings.
Dictionaries are similar to lists and tuples in that they hold a collection of items as shown in the above figure. But they have labelled collections which do not have an ordering. This means that for each value you insert into the dictionary, you must also give a key to get that value out.
In other languages, the structure is often called a map. And in python, we use curly braces to denote a dictionary. Here is an example where we might link names to email addresses. You can see that we indicate each item of the dictionary when creating it using a pair of values separated by colons.
Finally, you can iterate over both the values and you can unpack those items into different variables through assignments in one statement. Here is another example that, where we have a tuple, that has my first name, last name and email address. I declare three variables and assign them to the tuple.
In the next blog we are going to revisit strings briefly, then start working with some data files.
Thank You and Keep Coding.
VISIT OUR WEBSITE BRIGHTERBEES FOR MORE ABOUT DATA SCIENCE.
CONNECT WITH THE AUTHOR ON LINKEDIN.
IF YOU WANT TO KNOW ABOUT MACHINE LEARNING click here.