Data Types of Python
Data Types are very important for each data in the programming language. Because the types represent the kind of values of data and determine how the data values can be used. In Python, all data values are considered as an object and those objects are encapsulated in relevant object classes. Each object has an identity, a type, and a value like object-oriented languages such as C++, java. Python supports five different Standard/Built-in data types, each may have predefined memory requirement and memory representation. type() function is used to find the data type of given data.
Python's five Standard/Built-in Data Types
- None Type
- Numeric Types
- Sequences
- Sets
- Mapping
None Type
None is a special data type of python. In other languages like java, it is called null. But in Python, it is called 'None'. It is not equal to zero and not equal to False. None is equal to None. But you can not create other None Type objects. All variables whose value is None are equal to each other.
From this example, we can understand the None data type. The NoneType is equal to NoneType.
Numeric Types
Python supports four different numerical types
1)Integer 2) Float 3) Complex
Integers
Integers are whole numbers without any decimal point. They can be positive or negative. Python allocates a fixed number of bytes of space in memory for each variable of a normal integer type. Typically an Integer occupies four bytes(32 bit).
The following numbers and literals are recognized by Python
Regular integers
Normal Integer numbers. Which has the base 10.
Octal literals
The Octal literal base is 8. If you want to indicate an Octal number in a program, you will use the prefix 0o or 0O(Zero followed by uppercase or lowercase 'o'). It uses the digits 0-7 only.
Hexadecimal literals
The hexadecimal base is 16. If you want to indicate a hexadecimal number, you will use the prefix 0x or 0X (Zero followed by uppercase or lowercase 'x')
Binary literals are only known by computer. The base of binary literal is 2. To represent the binary value you'll use the prefix 0b or 0B (Zero followed by uppercase or lowercase 'b').
Converting Integer to their literal Representation
Python has some built-in functions to convert normal Integer numbers into their lliteral representation. They are hex(),oct(),bin().
The real numbers with floating points are called floating-point. They may also be written with scientific notations. For example, 3.3e3 or 3.3E3 here the uppercase or lowercase letter 'e' signifies the 10th power.
Complex Numbers
Complex Numbers are pairs of real parts and imaginary parts. They take the form a+bj where 'a' is the real part and 'bj' is the imaginary part of the complex number. This is Python's one of the best features. It accepts complex numbers, many other programming languages are not supported complex numbers.
Bool data type
Boolean datatype having two values. They are 'True' and 'False'. It is named after "George Boole", who first defined an algebraic of logic in the mid 19th century. These two values are the primary result of conditional statements.
Sequences
In Python, the Sequences are an ordered datatype. There are several types of sequences in Python given below.
- String
- Bytes
- Bytearray
- List
- Tuple
- Range
Strings
Strings are identified as a set of characters represented in the quotation marks. Python allows both single and double quotations. In Python string is the immutable object we cannot change it after once declared. But we can concatenate two strings using the '+' operator. Every string in Python takes additional 49-80 bytes of memory for stores information such as hash, length, length in bytes, encoding type, and string flags. An empty string takes 49 bytes of memory. Each character of a string occupies 1 byte of memory.
Bytes
The byte data type is the group of byte numbers like an array. Byte numbers are ranging from 0 to 255, byte array can not store negative numbers
Syntax: bytes([source[,encoding[,errors]]])
source - A source to use when creating the bytes object.
encoding - The encoding of the string.
error - Specifies what to do if the encoding fails.
Return a new "bytes" object which is an immutable sequence of small integers in the range 0 <= x < 256 print as ASCII characters when displayed bytes is an immutable version of bytearray it has the same non-methods and the same indexing and slicing behavior. Usually, the list is mutable but we can change it as immutable by using byte().
Bytearray
Bytearray is similar to byte data type but bytes data type array can not be modified whereas the bytearray type array can be modified
Syntax:bytearray([source[,encoding[,errors]]])
source - A source to use when creating the bytes object.
encoding - The encoding of the string.
error - Specifies what to do if the encoding fails.
Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x <256. It has most of the usual methods of mutable sequences.
- If it is a string you must give the encoding parameter. bytearray() converts the string to bytes using str.encode().
- If it is an integer, the array will have that size and will be initialized with a null byte.
Lists
A list contains items separated by commas and enclosed within square brackets( [] ). The values which are stored in the list may have different data types. The values stored in a list can be accessed by the slice operator [:]. We can concatenate two lists using the + operator and the asterisk (*) is the list repetition operator.
Tuples
A tuple is another sequence data type that is similar to the list. The main difference between lists and tuples is lists are enclosed in square brackets ( [] ) and that is mutable while tuples are enclosed in parentheses ( () )and it is immutable tuples can be thought of as read-only lists.
Range datatype
The range datatype in Python very useful to generate sequences of numbers in the form of a list.
Syntax:range(start,end,step)
The given endpoint is never part of the generated list. The numbers in the range are not modifiable.
Sets
The set data type is a collection type. It contains an unordered collection of unique and immutable objects. The set data type is, as the name implies, a Python implementation of the sets as they are known from mathematics. The element may not appear in the same order as they entered into the set. Sets are enclosed within ( {} ) bracket. In set, there are 2 types they are
1) Set datatype 2)Frozenset type
The major difference between set and frozenset is modifiable whereas the elements in the frozenset cannot modifiable. Set remove duplicates and perform mathematical operations such as intersection, union, difference, and symmetric difference.
Mapping (Dictionary)
Python's dictionaries are kind of hash table types. They work like associative arrays or hashes found in Perl. It contains a key-value pair. The key should be unique and any data type. Dictionary is enclosed by curly braces ( {} ) and values can be assigned and accessed using square braces ( [] ). Dictionary also an unordered data type.
Post a Comment