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

# numeric data
number = 42 # an integer based variable
decimal = 3.14159 # a floating point number based variable

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.

# text-based data
birthday = "January 1st 2000"

user_name = 'Jane Doe'

lorem_ipsum = """
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
"""

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.

# Decisional Data: boolean

is_late = False
loop = True

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.

# Collection Data 1: Tuple

user = ("Jane Doe", "January 1st 2000", 1)

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.

# Collection Data 2: List

nums = [3,1,4,1,5,9]
words = ["Hello", "World"]
user = ["Jane Doe", "January 1st, 2000", 1]

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.

# Collection Data 3: Sets

fruits = {"Apple", "Banana", "Strawberry"}
nums = {3,1,4,1,5,9}

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.

 # Collection Data 4: Dictionary
 
 user = {
     "id" : 1,
     "name" : "Jane Doe",
     "birthday" : "January 1st, 2000"
 }

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 always

  • float() | will convert to decimals if possible, whole numbers will be given .0

  • str() | will convert any data to string

  • bool() | will convert any data to a Boolean

  • tuple() | will convert any sequence like data to a tuple

  • list() | will convert any sequence like data to a list

  • set() | will convert any sequence like data to a set, duplicates will be removed, order may be different

  • dict() | will convert any sequence with a some sort of key value pairing to a dictionary

Last updated