This article was published as a part of the Data Science Blogathon
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.
Image Source: Google Images
1. What are Variables?
2. What is Data Types?
3. Different Data Types
4. What is Container?
5. Different Types of Containers
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’
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.
This includes representations of knowledge with numeric values.
Mainly, Python supports three numeric types: int, float, and complex.
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'>
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
print(type(True)) #output: <class 'bool'>
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.
Image Source: Google Images
It is the primary, and certainly the foremost common container.
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']
Image Source: Google Images
This container named Tuple is a smaller amount common than lists.
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',)
Image Source: Google Images
We also have a set.
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'}
Image Source: Google Images
Finally, the last container we’ll discuss maybe a dictionary.
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!
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.