Data Types in Python

What are Data Types?

Data types are the classification or categorisation of knowledge items. It represents the type useful that tells what operations are often performed on specific data. Since everything is an object in Python programming, data types are classes, and variables are instances (object) of those classes.

Data types are an important concept within the python programming language. Every value has its own python data type, in the python programming language. The classification of knowledge items or to place the info value into some kind of data category is named Data Types.

Data Types Supported by Python:

There are a variety of data types that are supported by Python. These different data types are given below -

1. Text (str):

A string is a collection of one or more characters within a quotation mark, a double quote or a triple quote. For example-

string="Hello World!"

print(string)

The output-

Hello World!

Some operations can be performed on a string. They are-

Concatenation:

The joining of two or more strings together is known as concatenation. For example:

string1="Hello"
string2="World!"

print(string1 + string2)

The output-

HelloWorld!

Slicing:

Slicing refers to extracting different parts of the string. For example:

string="Hello"

print(string[1:3])

The output-

ell

Repetition:

Repetition refers to repeating a string many times. For example:

string="Hello"

print(string*3)

The output-

HelloHelloHello

2. Lists (list):

A list is created in Python by placing all the elements inside square brackets, separated by commas. A list can have infinitely many elements and they may be of different types like string, decimal, etc. A list can be modified or updated too. For example:

List=[1,6,45,9.2,'Hi']

print("The list is", List)
print("The 5th element of the list is", List[4]) 
"""The numbering in Python starts from 0. That is, the number of the first element is 0, second element is 1 and so on."""

List[4]='Bye'
print("The edited list is", List)

The output:

The list is [1, 6, 45, 9.2, 'Hi']
The 5th element of the list is Hi
The edited list is [1, 6, 45, 9.2, 'Bye']

In the above example, the list List has been updated and its 5th element has been changed.

3. Tuple (tuple):

A tuple is just like a list but the only difference is that it can’t be modified or updated. For example:

Tuple=(46, 7.3, -20, 'Hi')

print("The second element of 'Tuple' is",Tuple[1], ".")

print("The first 3 elements of 'Tuple' are",Tuple[0:3], ".")
"""The upper limit here is not included. So, the element 1,2 and 3 will be printed only. The fourth element (numbered 3) will not be printed."""

The output-

The second element of 'Tuple' is 7.3.
The first 3 elements of 'Tuple' are 46, 7.3, -20

4. Sets (set):

A set is a group of elements, to which elements can be added or removed but not edited. A for loop can also be executed on a set. A set is made by enclosing the entities in curly brackets {}.

A set doesn’t allow duplicate items.

For example-

Set = {"Hello", "Hi", "Hello"}

print(Set) #The output will have "Hello" only once because duplicates are not allowed in sets.

Set.add("Bye")

print(Set)

Set[1]="Banana" """This will give an error as the entities of a set cannot be edited or changed."""

The output-

{'Hello', 'Bye', 'Hi'}
Traceback (most recent call last):
  File "/Users/'username'/'folder'/'file name'", line 5, in <module>
    Set[1]="Banana" #This will give an error as the entities of a set cannot be edited or changed.
    ~~~^^^
    TypeError: 'set' object does not support item assignment

5. Boolean (bool):

Boolean data types are used to represent the truth value of an expression. In other words, they are used to represent if an expression is true or false.

For example-

print(1+1==2)

print(1==2)

The output:

True
False

6. NoneType (None):

This data type is used to define no value. It is not the same as 0, False, or an empty string and only ‘None’ can be NoneType. For example-

x = None

if x is True:
 print("Do you think None is True?")

elif x is False:
 print("Do you think None is False?")

elif x is None:
 print("None is neither True nor False. None is None!")

Output:

None is neither True nor False. None is None!


That is all for this post on Python Data Types. Hope it was informative and understandable. You can find more detailed posts on Python Data Types.

Happy coding,
Aarav Iyer

Aarav Iyer

I am a technology and programming enthusiast, currently a high school student. I love drawing and am quite interested in aeronautics and astrophysics too. My favourite pastimes are reading books, blogging and skywatching with my telescope.

Post a Comment

Previous Post Next Post