Data types

Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instances (object) of these classes. The following are the standard or built-in data types in Python:

  • Numeric
  • Sequence Type
  • Boolean
  • Set
  • Dictionary
  • Binary Types ( memoryview, bytearray, bytes)

Numeric Data Type in Python

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

  • Integers – This value is represented by int class. It contains positive or negative whole numbers (without fractions or decimals). In Python, there is no limit to how long an integer value can be.
  • Float – This value is represented by the float class. It is a real number with a floating-point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.
  • Complex Numbers – Complex number is represented by a complex class. It is specified as (real part) + (imaginary part)j. For example – 2+3j

Sequence Data Type in Python

The sequence Data Type in Python is the ordered collection of similar or different data types. Sequences allow storing of multiple values in an organized and efficient fashion. There are several sequence types in Python –

  • Python String
  • Python List
  • Python Tuple

String Data Type

Strings in Python are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double-quote, or triple-quote. In python there is no character data type, a character is a string of length one. It is represented by str class.  

Creating String

Strings in Python can be created using single quotes or double quotes or even triple quotes. 

Accessing elements of String

In Python, individual characters of a String can be accessed by using the method of Indexing. Negative Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character, and so on. 

List Data Type

Lists are just like arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.  

Creating List 

Lists in Python can be created by just placing the sequence inside the square brackets []. 

Python Access List Items

In order to access the list items refer to the index number. Use the index operator [ ] to access an item in a list. In Python, negative sequence indexes represent positions from the end of the array. Instead of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3]. Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc. 

Tuple Data Type

Just like a list, a tupple is also an ordered collection of Python objects. The only difference between a tuple and a list is that tuples are immutable i.e. tuples cannot be modified after it is created. It is represented by a tuple class.  

Creating a Tuple

In Python, tupples are created by placing a sequence of values separated by a ‘comma’ with or without the use of parentheses for grouping the data sequence. Tuples can contain any number of elements and of any datatype (like strings, integers, lists, etc.). 

Note: Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple.

Note – The creation of a Python tuple without the use of parentheses is known as Tuple Packing.

Access Tuple Items

In order to access the tuple items refer to the index number. Use the index operator [ ] to access an item in a tuple. The index must be an integer. Nested tuples are accessed using nested indexing. 

Boolean Data Type in Python

Data type with one of the two built-in values, True or False. Boolean objects that are equal to True are truthy (true), and those equal to False are falsy (false). But non-Boolean objects can be evaluated in a Boolean context as well and determined to be true or false. It is denoted by the class bool. 

Note – True and False with capital ‘T’ and ‘F’ are valid booleans otherwise python will throw an error. 

Set Data Type in Python

In Python, a Set is an unordered collection of data types that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.

Create a Set in Python

Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by a ‘comma’. The type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set. 

Access Set Items

Set items cannot be accessed by referring to an index, since sets are unordered the items has no index. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in the keyword. 

Dictionary Data Type in Python

A dictionary in Python is an unordered collection of data values, used to store data values like a map, unlike other Data Types that hold only a single value as an element, a Dictionary holds a key: value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon : , whereas each key is separated by a ‘comma’.

Create a Dictionary

In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be immutable. The dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing it in curly braces{}. Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated distinctly. 

Accessing Key-value in Dictionary

In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets. There is also a method called get() that will also help in accessing the element from a dictionary. 

Types of Data based on different parameters

Based on Structure

  • Structured Data: Tabular data, such as rows and columns, is used to organize and store structured data. Spreadsheets and databases frequently contain this type of data.

Examples: Sales records, customer details, financial transactions.

Usage: Useful in supervised learning tasks like regression and classification.

  • Unstructured Data: Processing unstructured data is more challenging because it lacks a preset structure.

Examples: Text files, pictures, videos, and audio files are a few examples.

Usage: Found in speech-to-text systems, image recognition, and natural language processing (NLP) applications.

  • Semi-Structured Data: This type of data falls somewhere between unstructured and structured data. It has organizational elements but does not fit nicely into a tabular format.

Examples: JSON files, XML files, and NoSQL databases.

Usage: Often used in web scraping, API responses, and social media analysis

Based on Representation

  • Numerical Data: Features measured in numbers (e.g., age, income).
  • Categorical Data: Represents Categories or labels (e.g., gender, fruit type).
  • Ordinal Data: Categorical data with an essential order (e.g., clothing sizes: Small, Medium, Large).

Based on Labeling

  • Labeled Data: Includes input variables and corresponding target outputs. Example: Features like “age” and “income” with a label like “loan approval status.”
  • Unlabeled Data: Contains only input variables without any target labels. Example: Images without annotations.