Python Data Types

Python 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 instance (object) of these classes.
Following are the standard or built-in data type of python:
  • Numbers
  • Sets
  • Mapping
  • Sequences
  • Boolean

Numbers Types in Python

In any OOP language, there are many different data types. In Python, number data types are used to store numeric values. There are four different numerical types in Python:
  1. int : this one is pretty standard -- plain integers are just positive or negative whole numbers.
  2. float (floating point real values): floats represent real numbers, but are written with decimal points (or scientific notation) to divide the whole number into fractional parts.
  3. complex (complex numbers): represented by the formula a + bJ, where a and b are floats, and J is the square root of -1 (the result of which is an imaginary number). Complex numbers are used sparingly in Python.  

Sets in Python

In Python, Set is an unordered collection of data type 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. 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.
Creating a set

Sets can be created by using the built-in set() function with a sequence by placing the sequence inside curly braces, separated by ‘comma’. A set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed. The order of elements in a set is undefined and is unchangeable. Type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set.

Mapping Types in Python

Dictionary In Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds 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’.
Creating a dictionary
In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds a pair of values, one being the Key and the other corresponding pair element being its Key:value. Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be immutable.
Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces{}.
Note – Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly.

Sequences Types in Python

  • Strings Python strings are the instances of class ‘str‘. The string is a sequence of Unicode characters. Python strings are immutable. We can define a string using single quotes (‘) or double quotes(“).String is the most popular data type in Python. There are various operations supported for string objects – length, format, split, join, slicing etc.
  • Tuple in Python is an ordered sequence of items. Tuple is immutable i.e. once defined we can’t change its values.We can define a tuple using parentheses where items are separated using comma. A tuple can contain any number of elements and the elements can be of any type.
  • List is almost same as Tuple, except that it’s mutable. The order of the elements is maintained.List is defined using brackets and the elements are separated using comma.
Boolean Types in Python 
BooleanData 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 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 Boolean otherwise python will throw an error.

0 Comments