Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

Python data types

Python data types
(upbeat music) When you program in Python, the information you provide to the computer is referred to as data. The first of our basic data types are numbers including integers, for example, four or minus 222. Real numbers or decimal numbers, which have a floating decimal point are called floats, such as 3.14.
Complex numbers are also catered for. Here j is assigned to be the square root of minus one. Besides numbers you also have strings which resemble words. Each string is composed of elements or letters, and they’re defined using quotation marks, such as name and surname, or even a space, which is an empty character. The next data type may be slightly less familiar to you. This is a Boolean data, which can either be true or false. For example, you can test whether the variable name equals Mark by calling the bool function, which evaluates the name equals Mark as true or false. In this case, it returns true. Let’s practice with these basic data types in Jypter notebook.
Follow the commands in the video, and practice them on your notebook. In the first cell you’ll practice with the basic numerical data types starting with integers. Here you have an integer data four. When you assign variables in Python, in this case the number four is stored in memory and the variable name n points towards that location in memory. Pause the video and add them to your Jypter notebook. Now you have created four objects in memory and the variable names point towards these locations in memory. As you define these variables in memory, the variable names enter the namespace. You can provide an overview of this namespace in Python with the command whos.
Run the command and see what variables are being defined in memory, what type, and in some cases how much memory these variables are using. Creating string data types is relatively similar. In this case, instead of numerical data you have characters for letters or symbols. And again, as with numbers our human readable data is converted to binary data forms and is stored in the computer’s memory. Pause the video and add them to your Jypter notebook. With strings you can join them or concatenate them together using the plus operator. Here you’ll create a variable called full, which contains the surname, first name and a space between them.
Again, calling the function whos should show that these new string variables have been added to memory. Next, let’s practice with a simple Boolean example. Here you’ll use the function bool to evaluate whether a statement is or false. You’ll assign a variable b to our very obvious statement. Is one equal to one? Yes, it is. Pause the video and add them to your Jypter notebook. In our next example, you can show that you can easily combine the print statement and Boolean function statement in one line. So you can test, for example is our variable n equal to two? No, it’s not. It’s false and was defined as equal to four.
If you experiment, you can also test if p is equal to 3.14. It is, so it’s true. It’s a very simple function and you’ll find a place for it in solving different problems when you want to check the value of a variable and act on whether that variable is true or false. Using all these built-in data types and functions will become much clearer as you start using Python for your data analysis.

Each and every value belongs to a specific data type in Python. Data type defines the type of data values a variable can contain and the operations that can be performed on that data.

The video introduced you to the data types built into Python and showed you examples of each data type and its syntax in the Jupyter Notebook.

Next, you will engage in an exercise to apply your knowledge about Python data types.

This article is from the free online

Introduction to Data Analytics with Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education