PYTHON DATA TYPES

PYTHON DATA TYPES

Whenever we are writing python programs, we are usually dealing with some kind of data. For example, if we are writing a program to add two numbers, then the two numbers, which are to be added, are the data, and the result that we have after addition is also some data. In short, we are doing some different things with the data that we have. So, we need to be familiar with the data that we have.

First of all, let’s understand what data is.

Well, data is some piece of information, and it can be anything, like your name, email, address, mobile number, etc… So, if it is a mobile number, it is going to be a number. On the other hand, if it is your name, it is going to be a string (set of characters), and so on.

So, the data can be of different types like integers, floating point numbers, complex numbers, string, boolean data, etc.. So, to handle, or to work with different types of data, we have got some different data types, and we are going to have a look at those different data types now –

If you are familiar with other some other programming languages, like C/C++, or Java, we needed to declare the type of data that the variable was going to hold. But it is not the same thing here in python. Python is a dynamically typed language, which means that we do not specify what type of object is going to be referred by the variable. Instead, we just assign some value of some type to the variable, and then we can directly use that in the program.

So, as we know that we will be using variables to store the values, and the variable has to have some name to access it. Let’s take an example to store some kind of data into some variable.

  • name = “GyaniPandit”
  • num1 = 4
  • num2 = 4.8
  • isHuman = True
  • complexnum = 3j

Above are some variables which are assigned some values, and we did not specify the type of data which will be assigned to the variables. We just directly assigned it.

So, when we do –

name = “GyaniPandit”

Let’s have a look at some of the datatypes in Python

  • Numbers (integer, floating point number, complex number)
  • Boolean (bool) (this is for True or False things)
  • Strings. (Character is just a single-length string, as we will see later in detail, but string can be defined as a set of characters)
  • List – A list is used to store the collection of data. This will be seen later in the course.
  • Tuples – Tuples are also used to store collection of data, but the thing is that tuples are immutable.
  • Set – set is an unordered and un–indexed collection of data, in which, only unique values are allowed.
  • Dictionary – This is used to store key – value pairs (just like a dictionary, which has a word and it’s meaning). You can change or add the contents in the dictionary, but duplicate keys are not allowed in the dictionary. We will see this concept later in the course
  • frozenset – frozenset is an immutable version of a python set.
  • bytes – This is one of the inbuilt types for manipulating binary data. The bytes objects are immutable sequences of single bytes. Here, the allowed values are in the range of 0 to 255(both included)
  • bytearray – The bytearray object is a mutable counterpart for the bytes object. So, simply we can say that the bytes object is immutable, while the bytearray is mutable.
  • memoryview – The memoryview Let’s us access the object’s internal data(the object should support buffer protocol).

We are soon going to play around with many of these concepts so don’t worry, this is not even an introduction to these concepts (just an acquaintance though, to the names of the concepts so that when we will see them later, they won’t feel alien to you).

You just need to understand for now, that there are some different data types to deal with some different types of data. Soon, when we would start using this data, and gradually, we would become familiar with these data types. The thing is that in python, everything is an object, so the datatypes here, are actually classes.