Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
Common Features of All Data Types
This section describes some common features of all data types: unique identifiers, id(object) and type(object) functions, data type categories, casting to Boolean values, etc.
What Are Common Features of All Data Types? All data types share the following common features:
1. Every object of every data type has a unique identifier, which can be viewed as the object’s address in memory. You can use the id(object) function to return the identifier of the given object:
>>> id(None) 4547296864 >>> id(1) 4547439248 >>> id(2) 4547439280 # 1+1 returns the same object as 2 >>> id(1+1) 4547439280 >>> id(True) 4547219352 # 1==1 returns the same object as True >>> id(1==1) 4547219352 # "id" is the variable name referring to the id() function object >>> id(id) 4548982640
You can use "is" and "is not" operator to perform identity comparisons between 2 data objects of any data types.
# "int" literal returns the same object for the same value. >>> 1 is 1 <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="? True # "str" literal returns the same object for the same value. >>> 'hello' is 'hello' <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="? True # [] always creates a new object >>> [] is [] False
2. If you are not sure what is the type of a data object, you can use the type(object) function to find out:
>>> type(314) <class 'int'> >>> type(3.14) <class 'float'> >>> type(True) <class 'bool'> >>> type(None) <class 'NoneType'> >>> type(type(None)) <class 'type'> # "type" is the variable name referring to the type() function object >>> type(type) <class 'type'> >>> type(type(type)) <class 'type'>
3. Every object of every data type stores a data value. Based on the nature of their data values, data types can be categorized as:
Primitive Data Types - A primitive data type can be used to store a single piece of information. For example, "bool", "int" and "float" are primitive data types.
Structured Data Types - A structured data type can be used to store multiple pieces of information. For example, "str" and "list" are structured data types.
Mutable Data Types - The value stored in a mutable data type is changeable. For example, "list" and "dict" are mutable data types.
Immutable Data Types - The value stored in an immutable data type is unchangeable. For example, "int" and "tuple" are immutable data types.
Container Data Types - The value stored in a container data type is a reference to another object. For example, "list" and "tuple" are container data types.
4. Every object of every data type can be implicitly casted to a Boolean value in a Boolean context. Here is the casting logic for an object represented by "obj":
5. Every object of every data type occupies certain amount of storage in memory. You can use the sys.getsizeof(object) to find out the storage size of any given data object.
>>> import sys >>> sys.getsizeof(10) 28 >>> sys.getsizeof(1) 28 >>> sys.getsizeof(0) 24 >>> sys.getsizeof(-1) 28 >>> sys.getsizeof([1]) 64 >>> sys.getsizeof([0]) 64 >>> sys.getsizeof([0,1]) 72
Table of Contents
►Common Features of All Data Types
Data Type - NoneType for Nothing
Data Type - 'bool' for Boolean Values
Data Type - 'int' for Integer Values
Data Type - 'float' for Real Numbers
Data Type - 'bytes' for Byte Sequence
Data Type - 'str' for Character String
Data Type - 'tuple' for Immutable List
Data Type - 'list' for Mutable List
Data Type - 'dict' for Dictionary Table
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager