Python Data Types


What are Data Types?

A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.

Now let’s take an example where we want to record student information in a notebook. Here we would like to record the following information −

Name:
Class:
Section:
Age:
Gender:

Now, let’s put one student record as per the given requirement −

Name: Mohit
Class: 6th
Section: B
Age: 13
Gender: F

The first example dealt with whole numbers, the second example added two decimal numbers, whereas the third example is dealing with a mix of different data. Let’s put it as follows −

  • Student name “Mohit” is a sequence of characters which is also called a string.
  • Student class “6th” has been represented by a mix of whole number and a string of two characters. Such a mix is called alphanumeric.
  • Student section has been represented by a single character which is ‘B’.
  • Student age has been represented by a whole number which is 13.
  • Student Gender has been represented by a single character which is ‘F’.

This way, we realized that in our day-to-day life, we deal with different types of data such as strings, characters, whole numbers (integers), and decimal numbers (floating point numbers).

Similarly, when we write a computer program to process different types of data, we need to specify its type clearly; otherwise the computer does not understand how different operations can be performed on that given data. Different programming languages use different keywords to specify different data types.

Built-in Data Types

In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

Text Type:str
Numeric Types:intfloatcomplex
Sequence Types:listtuplerange
Mapping Type:dict
Set Types:setfrozenset
Boolean Type:bool
Binary Types:bytesbytearraymemoryview

Text

  • String: A string value is a collection of one or more characters put in single, double or triple quotes.
x = “Hello World”str

Numeric

A numeric value is any representation of data which has a numeric value. Python identifies three types of numbers:

  • Integer: Positive or negative whole numbers (without a fractional part)
  • Float: Any real number with a floating point representation in which a fractional component is denoted by a decimal symbol or scientific notation
  • Complex number: A number with a real and imaginary component represented as x+yj. x and y are floats and j is -1(square root of -1 called an imaginary number)
x = 20int
x = 20.5float
x = 1jcomplex

Boolean

Data with one of two built-in values True or False. Notice that ‘T’ and ‘F’ are capital. true and false are not valid booleans and Python will throw an error for them.

x = Truebool

Sequence Type

A sequence is an ordered collection of similar or different data types. Python has the following built-in sequence data types:

  • String: A string value is a collection of one or more characters put in single, double or triple quotes.
>>> s = 'foobar'
String index 1
  • List : A list object is an ordered collection of one or more data items, not necessarily of the same type, put in square brackets.
  • Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, put in parentheses.
x = “Hello World”str
x = [“apple”, “banana”, “cherry”]list
x = (“apple”, “banana”, “cherry”)tuple

List: Listsare just like dynamic sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. The main characteristics of lists are – 

  • The list is a datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
  • List are mutable .i.e it can be converted into another data type and can store any data element in it.
  • List can store any type of element.

Tuple: Tupleis a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. The main characteristics of tuples are – 

  • Tuple is an immutable sequence in python.
  • It cannot be changed or replaced since it is immutable.
  • It is defined under parenthesis().
  • Tuples can store any type of element.

Set: In Python, Setis an unordered collection of data type that is iterable, mutable, and has no duplicate elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. The main characteristics of set are –

  • Sets are an unordered collection of elements or unintended collection of items In python.
  • Here the order in which the elements are added into the set is not fixed, it can change frequently.
  • It is defined under curly braces{}
  • Sets are mutable, however, only immutable objects can be stored in it.

Table of Difference between List, Set, and Tuple

LISTSETTUPLE
Lists is MutableSet is MutableTuple is Immutable
It is Ordered collection of itemsIt is Unordered collection of itemsIt is Ordered collection of items
Items in list can be replaced or changedItems in set cannot be changed or replacedItems in tuple cannot be changed or replaced

Ordered Vs Unordered In Python

In Python, you have heard that lists, strings and tuples are ordered collection of objects and sets and dictionaries are unordered collection of objects.

So, do you understand what are ordered and unordered collection of objects in Python? If you don’t then following example helps you to understand concept ordered vs unordered:

Let’s analyze this concept using ASCII letters as:

letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

string_letters = str(letters)
lists_letters = list(letters)
tuples_letters = tuple(letters)
sets_letters = set(letters)


print("String: ", string_letters)
print() # for new line
print("Lists: ", lists_letters)
print() # for new line
print("Tuples: ", tuples_letters)
print() # for new line
print("Sets: ", sets_letters)

Output

String:  abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Lists:  [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 
'h', 'i', 'j', 'k', 'l', 'm', 'n', 
'o', 'p', 'q', 'r', 's', 't', 'u', 
'v', 'w', 'x', 'y', 'z', 'A', 'B', 
'C', 'D', 'E', 'F', 'G', 'H', 'I', 
'J', 'K', 'L', 'M', 'N', 'O', 'P', 
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 
'X', 'Y', 'Z'
]

Tuples:  (
'a', 'b', 'c', 'd', 'e', 'f', 'g', 
'h', 'i', 'j', 'k', 'l', 'm', 'n', 
'o', 'p', 'q', 'r', 's', 't', 'u', 
'v', 'w', 'x', 'y', 'z', 'A', 'B', 
'C', 'D', 'E', 'F', 'G', 'H', 'I', 
'J', 'K', 'L', 'M', 'N', 'O', 'P', 
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 
'X', 'Y', 'Z'
)

Sets:  {
'G', 'U', 'P', 'K', 'Q', 'w', 'I', 
'Z', 'N', 'l', 'm', 'h', 'J', 'D', 
'k', 'C', 'r', 'B', 'A', 'F', 'y', 
'c', 'V', 'i', 'E', 'a', 'o', 'R', 
'T', 'e', 'g', 'b', 'L', 'f', 'X', 
'x', 'O', 'S', 'j', 'v', 'p', 'Y', 
'H', 'u', 'n', 'z', 't', 'M', 'd', 
'W', 's', 'q'
}

If we look at the output for strings, lists and tuples, they are in same order as they are specified intially. And these data structures guarantee this order. So strings, lists and tuples are ordered collections of objects.

If we look at the result of sets and dictionary, initial order, the order in which we specified the elements, is not maintained. So sets and dictionaries are unordered collections of objects.

Dictionary

A dictionary object is an unordered collection of data in a key:value pair form. A collection of such pairs is enclosed in curly brackets. For example: {1:"Steve", 2:"Bill", 3:"Ram", 4: "Farha"}

x = {“name” : “John”, “age” : 36}dict

Like it? Share with your friends!

What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win
Choose A Format
Personality quiz
Series of questions that intends to reveal something about the personality
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Countdown
The Classic Internet Countdowns
Open List
Submit your own item and vote up for the best submission
Ranked List
Upvote or downvote to decide the best list item
Meme
Upload your own images to make custom memes
Video
Youtube and Vimeo Embeds
Audio
Soundcloud or Mixcloud Embeds
Image
Photo or GIF
Gif
GIF format