DataTypes and Containers in Python : A complete Guide

Gunjan Last Updated : 14 Jun, 2021
5 min read

This article was published as a part of the Data Science Blogathon

Introduction

Python is one every of the foremost popular programming languages in use today. It had been originally created by Guido van Rossum and first released in 1991. Python may be used for a variety of various purposes, including creating Web Applications, Read and Modify Files,  furthermore as handle Big Data and Data Science normally.

So, In this article, we will be discussing the Data Types and Containers within Python, and hopefully, by the end of this article, you’ll have a stronger understanding of what Python is, what it can do, and just how useful it’s to Data Scientists and Programmers everywhere.

Datatypes and containers python

                  Image Source: Google Images

 

Table of Contents

1. What are Variables?

2. What is Data Types?

3. Different Data Types

  • Numbers
  • Strings
  • Boolean

4. What is Container?

5. Different Types of Containers

  • List
  • Tuple
  • Set
  • Dictionary

 

What are Variables?

Variables are named locations used for storing data within the memory. you’ll view variables as containers holding data in computer programs. With python, variable declaration and assignment are easy, allowing you to call a variable anything you wish and changing its value willy-nilly:

Below are the initialization of the different variables;

v1 = 105
v2 = 3987.0
v3 = “My name is Gunjan Goyal”
v4 = ‘Hello Analytics Vidhya’

What are Data Types?

Data types represent the classification of knowledge items. It determines what style of operations is performed on data items. It reflects the language’s philosophy. Our focus is on Numbers, Strings, Lists, Dictionaries, Boolean, Tuples, and Set Types.

Different Data Types

Numbers

This includes representations of knowledge with numeric values.

Mainly, Python supports three numeric types: int, float, and complex.

  • Integers: includes both positive and negative whole numbers. The floating points number features a decimal place.
  • Python complex data type takes two parameters as an input- one real value and one imaginary value.

Example for Integer Data Type

print(type(267)) 
#output:
<class 'int'>

Example for Float Data Type

print(type(85.0)) 
#output:
 <class 'float'>

Example for Complex Data Type

a = (39+ 8j)
print(type(a)) 
#output: 
<class 'complex'>

 

Strings

  • They’re a sequence of characters in quotes.

With python there are several ways of defining a string:

By using Single quotes

greetings = ‘Hello’ 
print(greetings) 
#output: 
Hello

By using Double quotes

greetings = “Analytics”
print(greetings) 
#output: 
Analytics

 By using Triple quotes

greetings = '''Vidhya’’’
print(greetings) 
#output: 
Vidhya

 

Boolean 

  • With Boolean, there two possible values True or False which are meant to represent the real values of logic.
  • Both T and F must be capitalized for correct identification by the python interpreter.
print(type(True)) 
#output:
<class 'bool'>

 

What are Containers or Collection Types?

In many cases, we will want to store many values into a single variable, known as a container or a collection. These containers can hold an arbitrary number of other objects. There are a number of common containers present in Python so let’s dive right in.

List

Python Lists - javatpoint | Datatypes and containers python

                                                     Image Source: Google Images

It is the primary, and certainly the foremost common container.

  • A list is defined as an ordered, mutable, and heterogeneous collection of objects.
  • To clarify: order implies that the gathering of objects follow a particular order
  • Mutable means the list can be mutated or changed, and heterogeneous implies that you’ll be able to mix and match any kind of object, or data type, within a list (int, float, or string).
  • Lists are contained within a collection of square brackets [ ].
  • Instantiating a brand new list can be done in the following ways:

Initialize an empty list

new_list = [] 
#output
[ ]

Also, initialize an empty list

new_list = list() 
#output
[ ]

Initialize a list of strings

new_list = ['Hi', 'Analytics', 'Vidhya'] 
#output
['Hi', 'Analytics', 'Vidhya']

Tuple

Python Tuples - javatpoint | Datatypes and containers python

                                                 Image Source: Google Images

This container named Tuple is a smaller amount common than lists.

  • Tuples are similar therein they’re ordered and heterogeneous.
  • However, they differ because they’re immutable — meaning that after created they can not be mutated or changed.
  • Tuples are contained within a group of parentheses ( ).
  • Tuples are slightly faster and smaller than an inventory so, while their existence is partly legacy from a time after they were more useful, they still have their uses.
  • Instantiating a new tuple may be worn out in some alternative ways.

Initialize an empty tuple

new_tuple = () 
#output
()

Also, Initialize an empty tuple

new_tuple = tuple()
#output 
()

Initialize  a tuple of strings

new_tuple = ('Hi', 'Analytics', 'Vidhya') 
#output
('Hi', 'Analytics', 'Vidhya')

When the Tuples contain only a single object, we need to specify it with a trailing comma, so Python knows it is a tuple rather than a grouping operation.

new_tuple = ('Hello',) 
#output
('Hello',)

 

Sets

sets

                                                   Image Source: Google Images 

We also have a set.

  • Sets are an unordered, mutable, unique collection of objects (similar to traditional sets in mathematics).
  • A set is often created by calling the set() function
  • set( ) Function can create an empty set, bypassing our information through a collection of curly brackets, or bypassing one argument of an iterable, within which case Python will return a replacement set containing one element for every unique element within our iterable.

Initialize an empty set

new_set = set() 
#output
set()

Initialize a new set

new_set = {'A', 'A', 'B', 'C', 'C', 'D'}
#output
{'A', 'B', 'C', 'D'}

Initialize a new set

new_set = set(['A', 'A', 'B', 'C', 'C', 'D'])
#output
{'A', 'B', 'C', 'D'}

Dictionary

dictionary

                                                Image Source: Google Images

Finally, the last container we’ll discuss maybe a dictionary.

  • Dictionaries are one of the foremost common container types that you’ll encounter in your Pythonic journey.
  • Dictionaries are unordered, mutable key-value pairs. They’ll be thought of very similar to an actual dictionary, where the secret is the “word” and therefore the value is that the “definition” of that specific word.
  • Dictionaries, very similar to the other container types, will be instantiated in a very number of various ways.

Initialize an empty dictionary

new_dict = {} 
#output
{}

Also, Initialize an empty dictionary

new_dict = dict() 
#output
{}

Initialize a new dictionary

new_dict = {'a': 1, 'b': 2, 'c': 3}
#output
{'a': 1, 'b': 2, 'c': 3}

Initialize a new dictionary

new_dict = dict(a = 1, b = 2, c = 3)
#output
{'a': 1, 'b': 2, 'c': 3}

This ends our discussion!

End Notes

I hope you enjoyed the article.

If you want to connect with me, please feel free to contact me on Email

Your suggestions and doubts are welcomed here in the comment section. Thank you for reading my article!

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details