Prashant Sharma — August 3, 2021
Beginner Programming Python

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

Introduction

In this article, you’ll learn about Python Data Types and their applications while we writing the python programs in an optimized manner. You will learn ranging from what they’re, their syntax, and also see the examples to use them in Programs. Python is a language that needs no introduction. It’s incredibly powerful, versatile, fast and it’s easy to find out.
Python is one of the languages that’s witnessing incredible growth and recognition year by year. Python is an interpreted programming language that is object-oriented used for general-purpose programming. In this article, we are going to learn about different data types in the python language.

What are data types?

Data types are the classification or categorization of knowledge items. It represents the type useful that tells what operations are often performed on specific data. Since everything is an object in Python programming, data types are classes and variables are instances (object) of those classes.
Data types are an important concept within the python programing language. Every value has its own python data type, in the python programming language. The classification of knowledge items or to place the info value into some kind of data category is named Data Types. It helps to know what quiet operations are often performed on a worth.

 

Python has six standard Data Types:-

  • Numeric
  • String
  • List
  • Tuple
  • Set
  • Dictionary
Python has six standard Data Types

Image Source: link

Let’s discuss the above data types one by one:-

Numeric Data Type:-

In Python, numeric data type represents the data that has a numeric value. The numeric value can be an integer, floating number, or even complex number. These values are defined as int, float, and complex classes in Python.

Integers – This data type is represented with the help of int class. It consists of positive or negative whole numbers (without fraction or decimal). In Python, there’s no limit to how long integer values are often.

Example:-

a = 2
print(a, "is of type", type(a))
Output: 2 is of type

Float – This type is represented by the float class. It is a true number with floating-point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer could even be appended to specify scientific notation.

Example:-

b = 1.5
print(b, "is of type", type(b))
Output: 1.5 is of type

Complex Numbers – Complex numbers are represented by complex classes. It is specified as (real part) + (imaginary part)j, For example – 4+5j.

Example:-

c = 8+3j
print(c, "is a type", type(c))
Output: (8+3j) is a type

String Data Type:-

The string is a sequence of Unicode characters. A string may be a collection of 1 or more characters put during a quotation mark, double-quote, or triple quote. It can be represented using an str class.

Example:-

string1=  “Hello World”
print(string1)
output: Hello World

We can perform several operations in strings like Concatenation, Slicing, and Repetition.

Concatenation: It includes the operation of joining two or more strings together.

Example:-

String1 = "Hello"
String2 ="World"
print(String1+String2)
Output: Hello World

Slicing: Slicing is a technique for extracting different parts of a string.

Example:-

String1 = "Hello"
print(String1[2:4])
Output: llo

Repetition:

It means repeating a sequence of instructions a certain number of times.

Example:-

Print(String1*5)
Output: HelloHelloHelloHelloHello

List Data Type:-

A list is formed(or created) by placing all the items (elements) inside square brackets [ ], separated by commas.
It can have any number of items and they may or may not be of different types (integer, float, string, etc.).
A list is mutable, which suggests we will modify the list

Example:

List1 = [3,8,7.2,"Hello"]
print("List1[2] = ", List[2])
Output:  List1[2] = 7.2
print("List1[1:3] = ", List[1:3])
Output: List1[1:3] = [8, 7.2]

Updating the list:- we can update the list.

List1[3] = "World"
#If we print the whole list, we can see the updated list.
print(List1)
Output: [3, 8, 7.2, ‘World’]

Tuple Data Type:-

A tuple is defined as an ordered collection of Python objects. The only difference between tuple and list is that tuples are immutable i.e. tuples can’t be modified after it’s created. It is represented by tuple class. we can represent tuples using parentheses ( ).

Example:

Tuple = (25,10,12.5,"Hello")
print("Tuple[1] = ", Tuple[1])
Output: Tuple[1] =  10
print("Tuple[0:3] =", Tuple[0:3])
Output: Tuple[0:3] =  (25,10,12.5)

Set Data Type:-

A set is an unordered collection of items. Every set element is exclusive (no duplicates) and must be immutable (cannot be changed).

Example:

Set = {4,3,6.6,"Hello"}
print(Set)
Output: {‘Hello’, 3, 4, 6.6}

As the set is an unordered collection, indexing will be meaningless. Hence the slicing operator [ ] doesn’t work.

Set[1] = 12
Output: TypeError

Dictionary Data Type:-

In Python, Dictionary is an unordered collection of data values, which is used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, a Dictionary consists of key-value pair. Key-value is provided within the dictionary to form it more optimized. In the representation of a dictionary data type, each key-value pair during a Dictionary is separated by a colon: whereas each key’s separated by a ‘comma’.

Syntax:
Key:value

Example:

Dict1 = {1:'Hello',2:5.5, 3:'World'}
print(Dict1)
Output: {1: ‘Hello’, 2: 5.5, 3: ‘World’}

We can retrieve the value by using the following method:

Example:

print(Dict[2])
Output: 5.5

We can update the dictionary by following methods as well:

Example:

Dict[3] = 'World'
print(Dict)
Output:
{1: ‘Hello’, 2: 5.5, 3: ‘World’}

Conclusion

If you’re reading this text, you’re probably learning Python or trying to become a Python developer. Learning Python or the other programing language begins by understanding the concepts that are a basic part of its foundation.
Hope you want to have understood the varied classifications of Python Data Types by now, from this text .

About The Author

Prashant Sharma

Currently, I Am pursuing my Bachelors of Technology( B.Tech) from Vellore Institute of Technology. I am very enthusiastic about programming and its real applications including software development, machine learning, and data science.

Hope you like the article. If you want to connect with me then you can connect on:

Linkedin

or for any other doubts, you can send a mail to me also

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

About the Author

Our Top Authors

  • Analytics Vidhya
  • Guest Blog
  • Tavish Srivastava
  • Aishwarya Singh
  • Aniruddha Bhandari
  • Abhishek Sharma
  • Aarshay Jain

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *