Overview of data types and objects

·

4 min read

Contains summary from the Book* : Python Data Structures and Algorithms

In the last blog, we discussed about variables. We learned that everything in Python is an object and variables are basically pointers to these memory locations containing different objects.

Let's expand on that a little bit. Each object in python has a type, a value, and an identity. When we say x=3, we are creating an instance of Int class with a value of 3 and identity of x. The identity of an object (or variable) is an pointer to the object's location in memory. So, in simple terms, Python is storing the object of type Int in memory which can be referenced by the variable x.

According to the type of operations that can be performed in these objects, there are different data types. We can identify these data types by using the type keyword, and can access the memory of the objects created from these data types by using the id keyword.

a = [1,2,3]
print(type(a))
print(id(a))

// output
> <class 'list'>
> 140247365461792

Similarly, we can compare different objects by using the == and is operator.

h==i //means h and i have the same value. (see what I did there? I said hi. And I said hi again wink ;)

c is d // This tells us if  c and d are the same object

type(c) is type(d) // Tells us if c and d are of same type.

Before we jump into data types, let's talk about mutability of an object. There are two types of objects. Mutable and immutable. It's really simple. You can change the values of an mutable objects in memory and you can't for the immutable. For example: Lists are mutable. You can use methods like .append() or .insert() to add values to the lists, while Strings are immutable. You cannot change the value of a string in memory after you have declared it. Of course, you can perform operations on it, and save it in a different memory location, but the original memory location will not be affected.

//Lists are mutable
a=[1,2,3]
a.append(2)
print(a)
> [1,2,3,2]

//Lists are not mutable. However, you can store the operations on different memory location. 
variable="Hello"
capitalized_variable = s.capitalize()
print(s)
>"Hello"
print(capitalized_variable)
>"HELLO"

In total, Python has 12 built in data types, which can be divided into 4 different categories.

  1. Numeric: int, float,complex,bool (and yeah bool are numeric type. Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False. Crazy right? I didn't know that)
  2. Sequence: str,list,tuple,range
  3. Mapping: dict
  4. Set types: set, frozenset

In this blog, we will discuss about String and List data types.

1. String

Strings are derived data types that are composed of a sequence of characters. Each character represents an element in the sequence. They are contained within either single, double or triple quotes. "..." '...' or """ ... """ . If you need to type quotations inside the string then you can use \ before the quotes. Everything within the two quotes are a part of the string.

For example:

string = "Hello World!"

string2 = 'hello world'

string3="""
By using triple strings we can declare multiline string
like this
"""
escapingQuotes = ' I don \'t like python. I love it!'

Here, the variable first string is composed of sequence of characters H,e,l, l, o, W, o, r,d,!

And as we discussed above, Strings are immutable. We cannot change their value in the memory once they are defined. We can however, use Python's built in functions to perform String operations. We need to be mindful that these operations do not change the value in memory but each method just returns a value, which we can save in another memory location.

For Example:

variable="Hello"
capitalized_variable = s.capitalize()
print(s)
>"Hello"
print(capitalized_variable)
>"HELLO"

I just repeated myself, but I wanted to make sure that you understood this difference.

Also, there are variety of operations that you can do with strings. I am not going into details of string operations, but you can find all of them here . You can also always refer to python documentation .

2. Lists Lists are mutable data type that can be composed of any other data types. Each element in the list are called items. These items can be of any data types. List are defined within square brackets. [ ... ]. Each item in the list is index starting with 0. You can have as many items in the list as you want. Usually, lists are used when you want to have similar related data together.

Eg: chocolates = ['vanilla' , 'dark', 'strawberry', 'Sorry I don't know much about chocolates' ]