Signup/Sign In

Data Types in Python

Let's do a quick overview of the data types that are available in Python. Of course, this will become more and more clear as you use practice python more.

First, let's understand what Data Types are? As the name itself suggests, these are some different types of data that the python compiler understands. Just like the Computer can understand only binary numbers (i.e., 1 and 0), so that is the one and only data type that a Computer can understand but python has more. As far as Python, programmers are provided with various ways of representing data - like your name, roll number, marks, to the height of Mt. Everest, world population etc. Basically, anything that holds some raw information.


Types of data in Python

Data in Python can be one of the following 5 types:

  1. Numbers
  2. None
  3. Sequences
  4. Sets
  5. Mappings

You can also jump directly to the code example to see all the different datatypes live in action,

Live Example →


Numbers

Number is a data type that we have already dealt with in the previous lessons. We used them with mathematical operators, in functions, with variables, and in input-output tutorial as well, we guess everywhere till now, and that's what make them crucial. As you might have noticed at a few places in the previous tutorials, we have mentioned about Integers and Floating point numbers, well this is where we'll learn the difference between them. Numbers are further divided into three more types:


Integers and Boolean

This set comprises of all the Integers like 1, 2, 3,..., 0, -1, -2, -3,... and Boolean values (the one that computer understands) which are true or false (1 or 0 respectively).


Floating Point Numbers

This set deals with the numbers having decimal point values (or any real number). For example, 1.2, 4.76832, 8.0 are all floating point numbers.


Complex Numbers

Yes, unlike any other programming language, there is a data type for complex numbers in Python. Type either 1+2j or 1+2J in IDLE and it won't return any error because it's a perfectly valid complex number in python language. We know, traditionally we have been using iota (or i) for representing complex numbers but python opted for j instead i. Which is totally fine. Who are we to judge?


None

Think of a scenario where you don't want the variable to have any value. What will do? You can't use 0 because it's a number after all. That is where None comes into picture. Just put:

>>> x = None

And x will have no value.


Sequence

A Sequence is an ordered collection of items, indexed by positive integers. It is a combination of mutable (a mutable variable is one, whose value can be changed) and immutable (an immutable variable is one, whose value can not be changed) data types. There are three types of sequence data type available in Python, they are:

  • Strings
  • Lists
  • Tuples

String

It is an ordered sequence of letters/characters. They are enclosed in single quotes (' ') or double quotes (" "). The quotes surrounding the string are not part of the string. They only inform the compiler about where the string begins and ends. They can have any character or sign, including space in them. These are immutable. A string with length 1 represents a character in python. You know we have used strings on several occasions in earlier tutorials. Basically, they help us to store and implement literature words in Python. For example, "Saharsh", 'Saharsh', "123", '123' are all strings.


Lists

It is also a sequence of values of any type. Values in the list are called elements/items. Lists are mutable and indexed/ordered. The list is enclosed in square brackets [ ]. For example, [2, 6, 8, 3, 1] or ["Python", "Java", "C++"] are both lists. You can notice that it's no different from the list we use in our daily life. A List is just a bunch of numbers or strings that are kept together inside the square bracket, sequentially. Each element of the list can be accessed by using it's index number.


Tuples

They are a sequence of values of any type and are indexed by integers. Tuples are enclosed in curved brackets i.e., (). Tuples are also pretty much like Lists, except that they are immutable, hence once we assign it some value, we cannot change it later. At the same time, it is faster in implementation than List.


Sets

Set is an unordered collection of values of any type with no duplicate entry. It is immutable (their values can't be changed later; try to keep that definition in mind).


Mapping

This data type is unordered and mutable. Dictionaries fall under Mappings.


Dictionaries

Dictionaries pretty much like our dictionary can store any number of python objects. What they store is a key-value pair, which is accessed using the key. Dictionary is enclosed in curly brackets { }.