Data Types
Python & Data
In Python, all built-in data that can be used are all considered as objects. The concept of objects will be covered in a future chapter. Due to such concept, we must understand the concept of mutability.
Immutability & Mutability
A data object in Python is can be considered either immutable or mutable.
An immutable data cannot be modified without recreating the data through an assignment operation
A mutable data can be modified without recreating the data. (It can have its inner content changed without an assignment operation)
Immutable Data Objects
Integer objects are whole numbers that range from negative infinity to positive infinity. Python will have limitations of the computer system itself.
Floating Point objects are decimals of Python; moreover, floating points represent all the real numbers.
Since classical computers use a binary-number system, it does struggle with decimals. There is a great video that you can watch here.
String objects represent any textual data in Python. To create string data, you must trap the text in either single (''
) or double quotation marks (""
).
Strings are a powerful object that will come with built-in for manipulation and comparison which will be studied later.
Any long-text that you want to preserve formatting we often use triple quotation marks.
Some characters will be illegal (example: "Jane "The Programmer" Doe")
will not be a valid string as there are too many quotation marks in a single line.
The fix:
name = "Jane \"The Programmer\" Doe"
Escape Character can be used to help meditate any issues on adding such illegal characters to strings properly.
Boolean objects represents the concept of logical truth. It can have two possible values: True
and False
.
Booleans are fundamental in decision-making and controlling the flow of programs.
Tuple is one of many data objects that allows multiple items to be stored in a single variable container.
Mutable Data Objects
Lists, Sets, and Dictionaries are all similar to strings in that they have their respective methods. These are important data objects; therefore, they will be highlighted in their own pages.
List objects are a mutable container of any data where we can dynamically add new items, extend it with another list, and remove values from it.
Set objects are based on mathematical definition of sets. Sets can only contain a single copy of an item (no duplicates allowed), and the items that are immutable. Sets are often used for their membership operators as it is much faster than other data types.
Dictionaries objects are ways to create a collection of key (address) to value pairs. Dictionaries allow you to have a unique address for each of your items for fast retrieval.
Type Casting
Type casting, also known as type conversion, is the process of converting an object from one data type to another in Python. It allows you to change the interpretation and representation of data. Python provides several built-in functions for type casting, which allow you to convert variables from one type to another.
The following are the type casting function to convert one data to another
int()
| will convert to integers if possible, will round down alwaysfloat()
| will convert to decimals if possible, whole numbers will be given.0
str()
| will convert any data to stringbool()
| will convert any data to a Booleantuple()
| will convert any sequence like data to a tuplelist()
| will convert any sequence like data to a listset()
| will convert any sequence like data to a set, duplicates will be removed, order may be differentdict()
| will convert any sequence with a some sort of key value pairing to a dictionary
Last updated