Sunday, February 2, 2020

Data types in Python


A data type represents the type of data stored into a variable or memory. The data types which are already available in Python language are called Built-in data types. The data types which can be created by the programmers are called User-defined data types. The built-in data types are of five types:
1.  None Type
2.  Numeric types
3.  Sequences
4.  Sets
5.  Mappings

      
     Python has five standard data types, named Numbers,  None, Sequences, Sets and Mappings.Python sets the  type of variable based on the type of value assigned to it  and it will automatically change the variable type if the  variable is set to some other value.

Numeric Types
 The numeric types represent numbers. There are three sub types:
·    int
·    float
·    complex 


Bool Data type
The bool data type in Python represents boolean values. There are only two boolean values True or False that can be represented by this data type. Python internally represents True as 1 and False as 0. A blank string like "" is also represented as False. Conditions will be evaluated internally to either True or False.

Sequences in Python
A sequence represents a group of elements or items. For example, a group of integer numbers will form a sequence. 

Mapping Types
 A map represents a group of elements in the form of key value pairs so that when the key is given, we can retrieve the value associated with it. The dict datatype is an example for a map. The ‘dict’ represents a ‘dictionary’ that contains pairs of elements such that the first element represents the key and the next one becomes its value. The key and its value should be separated by a colon (:) and every pair should be separated by a comma. All the elements should be enclosed inside curly brackets {}. We can create a dictionary by typing the roll numbers and names of students. Here, roll numbers are keys and names will become values. We write these key value pairs inside curly braces as:

d = {10: 'aaa', 11: 'bbb', 12: 'ccc', 13: 'ddd', 14: 'eee'}
Here, d is the name of the dictionary. 10 is the key and its associated value is ‘aaa’.

None Type
In Python, the ‘None’ data type represents an object that does not contain any value. In languages like Java, it is called ‘null’ object. But in Python, it is called ‘None’ object. In a Python program, maximum of only one ‘None’ object is provided. One of the uses of ‘None’ is that it is used inside a function as a default value of the arguments. When calling the function, if no value is passed, then the default value will be taken as ‘None’. If some value is passed to the function, then that value is used by the function. In Boolean expressions, ‘None’ data type represents ‘False’.

No comments:

Post a Comment