Data Types in Python

Spread the love

Data Types –

There are various data types in Python like strings, numbers, lists, dictionaries etc. We will look at them one by one in this post.

Strings –

A string is a series of characters. Anything inside quotes is a string in Python. You can use single quote, double quotes or even triple quotes.

In [1]: s1 = 'This is a string'

In [2]: s2 = "This is also a string"

In [3]: s3 = '''This is a
   ...: multi-line string'''

To check the data type of an object in python, you can use the type method.

In [4]: # check data types

In [5]: print(type(s1))
<class 'str'>

In [6]: print(type(s2))
<class 'str'>

In [7]: print(type(s3))
<class 'str'>

You can also combine single quotes and double quotes like this.

In [8]: p1 = 'I think "python is awesome"'

In [9]: p2 = "I'm learning python"

In [10]: print(p1)
I think "python is awesome"

In [11]: print(p2)
I'm learning python

But this is not a valid string.

In [12]: p3 = 'I'm learning python'
  File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_34928\2892361417.py", line 1
    p3 = 'I'm learning python'
            ^
SyntaxError: invalid syntax

There are various operation, you can do with strings like access a character from the string.

In [13]: py = "python"

In [14]: py[2]
Out[14]: 't'

In python index starts from 0 that is why we wrote py[2] to access the third character instead of py[3].

You can also access multiple characters using list slicing. The syntax for slicing is py[start:end]

In [15]: py[1:4]
Out[15]: 'yth'

In python, when we do slicing the start is included and the end is excluded that is why you get yth instead of ytho.

String are immutable, once created you can not modify them.

In [16]: py[0] = 'c'
Traceback (most recent call last):

  File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_34928\1715983538.py", line 1, in <module>
    py[0] = 'c'

TypeError: 'str' object does not support item assignment

There are lots of things you can do with strings and we will learn more about them in great details in our upcoming posts, so make sure to check out our blog later or subscribe to get notified.

Numbers –

Numbers in python are sub divided into integers, floats.

Integers –

You can add, subtract, multiply and divide integers in python.

In [1]: 5 + 10
Out[1]: 15

In [2]: 7 - 4
Out[2]: 3

In [3]: 4 * 6
Out[3]: 24

In [4]: 8 / 2
Out[4]: 4.0

In python two multiplication symbols is used to represent exponents.

In [5]: 2 ** 2
Out[5]: 4

In [6]: 2 ** 3
Out[6]: 8

Floats –

Any numbers with a decimal point is a float in Python. Just like Integers, you can also do addition, subtraction, multiplication and division.

In [7]: 0.3 + 0.4
Out[7]: 0.7

In [8]: 5.3 - 2.5
Out[8]: 2.8

In [9]: 4.5 * 2.1
Out[9]: 9.450000000000001

In [10]: 5.6 / 2.1
Out[10]: 2.6666666666666665

Lists –

A list is an ordered sequence of values, where each value is identified by an index. To create a list, we use square brackets

In [11]: l1 = [5, 10, 15, 20, 25]

A list can store any kind of data inside of list even an another list.

In [12]: l2 = ["hi", 10, 4.5, [15, 20]]

And just like string, we can also access the elements of a list using it’s index.

In [13]: l2[0]
Out[13]: 'hi'

In [14]: l2[3]
Out[14]: [15, 20]

In [15]: l2[2:4]
Out[15]: [4.5, [15, 20]]

And if you try to access an index out of the list range, python will throw an error.

In [16]: l2[4]
Traceback (most recent call last):

  File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_25816\2956889587.py", line 1, in <module>
    l2[4]

IndexError: list index out of range

Lists are mutable means you can modify them after they are created compared to strings which are immutable.

In [22]: fruits = ['apple','banana','orange','mango']

In [23]: fruits[2] = 'grapes'

In [24]: fruits
Out[24]: ['apple', 'banana', 'grapes', 'mango']

Tuples –

Like String, tuples are immutable ordered sequences of elements. In python, tuples are created by enclosing a comma-separated list of elements within parentheses.

In [17]: t1 = (1, 'two', 3)

In [18]: type(t1)
Out[18]: tuple

We can access the elements of a tuple like strings and lists.

t1[0]
Out[19]: 1

t1[0:2]
Out[20]: (1, 'two')

But you can not modify them once created as tuples are immutable.

In [21]: t1[2] = 10
Traceback (most recent call last):

  File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_34928\502093124.py", line 1, in <module>
    t1[2] = 10

TypeError: 'tuple' object does not support item assignment

Dictionary –

A dictionary is a set of key-value pair. They are like lists except that we index them using keys. To create a dictionary we use curly braces and each element is written as a key followed by a colon followed by a value.

In [25]: prices = {'apple':90,'banana':20,'grapes':60,'mango':70}

In [26]: type(prices)
Out[26]: dict

To access the prices of fruits, we have to use the keys of the dictionary.

In [27]: prices['banana']
Out[27]: 20

In [28]: prices['mango']
Out[28]: 70

Rating: 1 out of 5.

Leave a Reply