2. Data Types

Data Types define a particular kind/domain of a data item. They define the type of data a variable is pointing to. They also define the operations allowed on that data type. Python doesn't require declaration of data types like in C/C++/Java (as we saw before variables are just pointers). Any variable can be assigned any data type/object, a string variable can be assigned int or float or any other object it doesn't matter. Unlike in Java, there is no final (used for declaring a constant variable) keyword in Python. The constant variables in Python are defined inside another .py file in capital letters and then they are imported inside the current module.

Three types of Data Types in programming.

  1. >Primitive: Are built-in or predefined data types in a programming language, Eg. int, float, double (n/a in Python), char (n/a in Python), bool etc.
  2. >Composite/Derived: Are data types which are constructed using two/more data types, Eg. Array (list in Python), Record (tuple in Python), Union (dict in Python), Strings (str in Python), Functions, Pointers (variables in Python), Structures (n/a in Python) etc.
  3. >Abstract: They define operations on objects using functions but without specifying the exact implementations of those functions (the underlying implementation can differ from a programming language to another but the working has to stay the same), Eg. Stack, Queue, Map, Tree, Graphs etc.

Mutable and Immutable types in Python.

  1. >Immutable: Values cannot be altered/added/removed once created or are read-only types once created, Eg. int, float, complex, bool, None, str, tuple, frozenset.
  2. >Mutable: Values can be altered/added/removed after creation, Eg. list, dict, set.

Data types explained in this chapter are int, float, complex, str, bool, byte and User-defined Data Type. Later we'll take a look at the None object and some built-in functions. For simplicity I've arranged the rest of them in the Data Structure section. Alright, let's begin.