Sunday, January 26, 2020

Data types in Python

Data Types in Python


Data Types


Data types in a programming language define a way how the variables are stored and manipulated in the programs. There are many native data-types in Python (Boolean, Numbers, Strings, Lists, Sets, Tuples, and Dictionaries). However, Python is a dynamically typed programming language, and we don need to explicitly provide the type of variable.

Variables in Python can be broadly categorized as given in the picture below.


Python data types
Python data types



  • Boolean, can have either True or False value.
  • Numbers can be integers for example 12, 1889, and 755552 or floats values like 17.5 and 0.36 fractions, fraction values like 1/7 or 2/5.
  • Python even supports complex numbers.
  • Strings are sequences of Unicode characters like ‘I am a string’.
  • We can also use bytes and byte arrays, for example, JPEG image file.
  • Lists are built-in ordered sequences of values in Python. Lists have a number of extra functions and properties. Lists can be appended to a list without recreating a new one.
  • Tuples are ordered, immutable sequences of values. Like strings, tuples are a collection of data, but strings are only a collection of characters. Tuples are a way of storing many pieces of data under one a single variable name.
  • Sets are unordered collections of immutable values. Multiple occurrences of elements are not allowed in Set.
  • We can store and retrieve values with the help of keys in a dictionary. Dictionaries are mutable and indexed. 


We can summerise these data types in the following table,


Name Type Description
Integers

int

Whole numbers: 1,4,51

Floating Point

float

Decimal point numbers: 12.354, 0.89

Strings

str

An ordered sequence of characters: "hello", "Python"

Lists

list

An ordered sequence of objects :[10,23.47,"Python",True]

Dictionaries

dict

Unordered key-value pairs {"a":10,"b":"Python","c":True}

Tuples

tup

An ordered sequence of objects (25, "Python",17.58)

Sets

set

An unordered collection of unique objects {"a","b","c"}

Booleans

bool

Logical values indicating true or false


Mutable and Immutable Data types


We can divide all the data types into two broad categories on the basis of their mutability in Python. They can be mutable or immutable.

  • Mutable - If the value of the variable can be changed once it is assigned a value.
  • Immutable - If the value of the variable can not be changed once it is assigned a value.


Python mutable and immutable datatypes
Data types



Python Data Types Example: