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 14 are the standard or built-in data type of Python:
- Numeric: Integer, Float, Complex, Boolean
- Sequence Type: String, List, Tuple
- Set, Frozen Set, Dictionary
- Bytes, Bytearray
- Range
- None
Explanations:
1. Integer: This value is represented by int class. It contains positive or negative whole numbers (without fraction or decimal). In Python there is no limit to how long an integer value can be...
2. Float: This value is represented by float class. It is a real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation...
3. Complex Numbers: Complex number is represented by complex class. It is specified as ( real part ) + ( imaginary part ) j. For example – 2 + 3j...
4. Boolean: Data 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 booleans otherwise python will throw an error...
5. Range: The range function in python is also one of the data types. The range function in python only works with integers or whole numbers. Arguments passed in the range function cannot be any other data type other than an integer data type. All three arguments passed can be either positive or negative integers...
6. String: In Python, Strings are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one. It is represented by str class...
7. List: Lists are just like the arrays, declared in other languages which is a ordered collection of data. It is very flexible as the items in a list do not need to be of the same type...
Creating List: Lists in Python can be created by just placing the sequence inside the square brackets [ ]...
8. Tuple: Just like list, tuple is also an ordered collection of Python objects. The only difference between tuple and list is that tuples are immutable i.e. tuples cannot be modified after it is created. It is represented by tuple class...
Creating Tuple: In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the use of parentheses for grouping of the data sequence. Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.)...
Note: Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple...
Note: Creation of Python tuple without the use of parentheses is known as Tuple Packing...
9. Set: 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...
Creating Sets: Sets can be created by using the built-in set( ) function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’. Type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set...
10. Frozen Set: In Python, frozenset is the same as set except the frozensets are immutable which means that elements from the frozenset cannot be added or removed once created. This function takes input as any iterable object and converts them into an immutable object...
11. Dictionary: 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 Dictionary: In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. 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 it to curly braces{}...
Note: Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly...
12. Bytes: bytes() function returns 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-mutating methods and the same indexing and slicing behavior...
13. Byte Array: bytearray() function returns 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, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Byte Array Methods...
14. None: The None keyword is used to define a null variable or an object. In Python, None keyword is an object, and it is a data type of the class NoneType . We can assign None to any variable, but you can not create other NoneType objects. Note: All variables that are assigned None point to the same object...