Data Types
Data Types
Data type specifies the type of value a variable requires to do various operations without causing an error. By default, python provides the following built-in data types:
Numeric data: int, float, complex
int: 3, -8, 0
float: 7.349, -9.0, 0.0000001
complex: 6 + 2i
more on numeric data types in the number chapter.
Text data: str
str: “Hello World!!!”, “Python Programming”
Boolean data:
Boolean data consists of values True or False.
Sequenced data: list, tuple, range
list: A list is an ordered collection of data with elements separated by a comma and enclosed within square brackets. Lists are mutable and can be modified after creation.
Example:
Output:
tuple: A tuple is an ordered collection of data with elements separated by a comma and enclosed within parentheses. Tuples are immutable and can not be modified after creation.
Example:
Output:
range: returns a sequence of numbers as specified by the user. If not specified by the user then it starts from 0 by default and increments by 1.
Example:
Output:
Mapped data: dict
dict: a dictionary is an unordered collection of data containing a key:value pair. The key:value pairs are enclosed within curly brackets.
Example:
Output:
Binary data: bytes, bytearray, memoryview
bytes: bytes() function is used to convert objects into byte objects, or create empty bytes object of the specified size.
Example:
Output:
bytearray: bytearray() function is used to convert objects into bytearray objects, or create empty bytearray object of the specified size.
Example:
Output:
memoryview: memoryview() function returns a memory view object from a specified object.
Example:
Output:
Set data:
Set is an unordered collection of elements in which no element is repeated. The elements of sets are separated by a comma and contained within curly braces.
Example:
Output:
None:
None is used to define a null value. When we assign a None value to a variable, we are essentially resetting it to its original empty state which is not the same as zero, an empty string or a False value.
Example:
Output: