Python Data Types

In Python, it is very important to understand the data types because these are the objects that we will work on a day-to-day basis. To make the best of the Python or any programming language, we’ll need a strong understanding of the basic data types. Below are the data types available in Python. before going forward I would recommend you go through Introduction to Python.

Data Types in Python

  1. Number
  2. String
  3. List
  4. Tuple
  5. Dictionary
  6. Set

Number

In Python, the Number data type is used to hold numeric values. Below are the different Number types available in Python

  1. Integer: holds signed integers.
  2. Long: holds long integers
  3. Float: holds Floating point real numbers and it is accurate up to 15 decimal places.
  4. Complex: holds complex numbers.

In Python, we need not declare datatype while declaring a variable like C or C++. We can simply just assign values in a variable. We can check the type of numerical value by type(). We can check if a value belongs to a particular class byisinstance(). We can also use the conversion function(int(), long(), float(), complex())  to forcefully change the data type of a variable.

Below are the examples:

String

In Python, String is a sequence of characters. In Python, Unicode characters are supported. We use single quotes or double quotes to represent strings. We can use triple quotes, ''' or """ to assign a multi-line string.

Below are the examples:

Subsets of strings can be taken using the slice operator ([ ] and [:] ) In Python, Indexing starts from 0  i.e. the first letter of your string has index 0. In Python, there is a concept of negative indexing, the end of the string indexed by -1. We can calculate the length of the string by the function len().

Below is the example:

List

In Python, List is a versatile data type. We can say it is the same as arrays in C/C++. But the advantage of a list in Python is it can simultaneously hold different data types. A list can contain a series of values. List variables are declared by using square brackets [ ]. The elements of a list are separated by comma(,). We can update the elements of the list after they are declared. Lists aren’t limited to a single dimension. Indexing is the same as string. The first element will be indexed as 0, second will be 1 and so on. In case of negative indexing, the last element will be indexed as   -1, second last as -2 and so on.

Below are the examples:

Tuple

In Python, Tuple is an ordered sequence of items the same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified. As tuples are immutable they are faster to process than a list. Tuple variables are declared by using parentheses (). The elements of the tuple are separated by comma (,), Indexing is the same as lists.

Below are the examples:

Dictionary

In Python, Dictionary is an unordered collection in the form of key-value pairs. In order to retrieve the value, we must know the key. In Python, dictionaries are defined within curly braces {}. Each item in Dictionary are a pair in the form key:value. Key and value can be of any type. It is very useful to retrieve data in an optimized way when there is a large amount of data.

Below are the examples:

Sets

In Python, Sets is an unordered i.e. not sorted collection of unique items.  In Python, Sets are defined within braces {}. Items in a set are not ordered. Sets have unique value i.e. they eliminate the duplicate entries. Sets are unordered; hence we cannot perform slicing []. We can perform operations like union, intersection on two sets.

Below are the examples:

Let me know your views on this post.

To Stay in touch, follow us on Twitter and Facebook.

View previous post Introduction to Python.

About the author

Gaurav Tiwari

My Name is Gaurav Tiwari. I am working in the IT industry for over 3.5+ years. I completed my B.E. from Mumbai University in 2015, Since then I’m working with Accenture Solutions PVT. LTD. as data Analyst.
I’ve started writing blogs as hobby.

View all posts

1 Comment

Leave a Reply

Your e-mail address will not be published. Required fields are marked *