Datatypes in Python

In Python, different types of values can be assigned to variables. Python is an object-oriented language and it implements the concept of classes and objects. The datatypes in python are treated as Classes and values are treated as objects of the class.

Thus, you can say that different types of values belong to different classes(datatypes). Datatypes available in Python are as follows:

Python Numbers

Python numbers consist of four types of values. They are:

  1. Integer (int)

    Integers are just like integers in mathematics. Any whole number is considered as an integer in Python. Integers include positive as well as negative whole numbers.

    There is no limitation on the length of an integer. You can use as large as the value you want, depending upon the memory available.

    Integer value must not have a decimal point.

    Example:
    age = 50
    p = 12345678987
    marks = -10​
  2. Floating-point Numbers (float)

    Floating-point numbers are decimal values. Float values can be positive or negative.
    Python provides accuracy in floating-point numbers up to 15 decimal places only. After that, the value may become inaccurate.

    Example:
    depth = 456.3759
    a = 5.0​
  3. Complex Numbers (complex)

    Complex numbers are the imaginary numbers. Complex numbers are not used commonly. They are used for research purposes in engineering and science field.

    Complex numbers are written in the format 'a + bj', where a is the real part and b is the imaginary part. Letter 'j' is added as a suffix to the imaginary part.

    Example:

    3 + 6j
    10j #real part is 0.
  4. Boolean (bool)

    Boolean datatypes work on logical mathematics. A value of Boolean datatype can either be 'True' or 'False'.
    It is used in algorithm implementation. The values True and False will always start with capital letters 'T' and 'F' respectively because these words have special meanings to the interpreter.

    The value 'True' is assigned to the variable if the given condition evaluates to true. Otherwise, 'False' value is assigned.

    Example:
    >>> c = 10>5
    >>> c
    True
    
    >>> d = 5<2
    >>> d
    False​

Python Strings

A string is a sequence of characters which lies in a pair of double or single quotes. A string can contain one or more characters. The quotation marks show the beginning and end of the string.

Example:

"Learning Python"

or

'Learning Python'

The only difference between double and single quotes is that, with single quotes, you won't be able to use an apostrophe.

Python List

A list is a collection of values that may or may not be of the same type.

  • A list is a mutable i.e changeable and ordered data.
  • The values present in a list are called Items. Every item in the list has an index.
  • The Items of a list lie within a pair of square brackets [].

Example:

Subjects = [ 'Hindi', 'English', 'Maths', 'Science' ]

Python Tuples

Tuples are similar to lists but they are immutable i.e unchangeable and ordered.

  • All the items in tuples are indexed.
  • Unlike a list, the items lie within a pair of parenthesis ().

Example:

Days = ( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' )

Python Set

A Set is a collection of unordered and immutable i.e unchangeable values.

  • All the values in a set are unique and unindexed.
  • The values in a set are enclosed within a pair of curly braces {}.

Example:

Age = { 12, 45, 67, 40, 33, 70 }

Python Dictionary

Dictionary is a special datatype in Python which consists of Key-Value pairs.

  • Dictionary is a built-in datatype which binds the values to the respective keys.
  • It is an unordered and mutable i.e changeable datatype.
  • Every item in a dictionary has an index.
  • Curly braces {} are used in a dictionary to hold all the items.

Example:

Countries {
'India' : 'New Delhi'
'China' : 'Beijing'
'USA' : 'Washington DC'
}

 

Detecting the datatype of a value in Python.

Python provides a built-in function 'type()' to detect the datatype of a value. In other words, you can say that the type() function returns the class to which the value (object) belongs.

The value or name of the variable referring to that value whose type is to be detected is given as an argument to the function.

Example:

  • Passing the variable as an argument
    >>> a = 55
    >>> type(a)
    <class 'int'>
    
    >>> m = 45.1
    >>> type(m)
    <class 'float'>
    
    >>> test = 5>10
    >>> type(test)
    <class 'bool'>
    
    >>> day=['monday', 'tuesday'] #list
    >>> type(day)
    <class 'list'>
    
    >>> age = {10,99,22,13,14} #set
    >>> type(age)
    <class 'set'>
    
    Days = ( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' )
    >>> type(Days)
    <class 'tuple'>​
  • Passing the value as an argument

    >>> type(6.8) 
    <class 'float'>
    
    >>> type(True)
    <class 'bool'>
    
    >>> type(33333)
    <class 'int'>
    
    >>> type("hello")
    <class 'str'>​