Updated On : May-01,2020 Time Investment : ~10 mins

Learning About Data Types In Python

Every programing lanaguage have their own set of Data Types. Therefore, every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Strings
  • Python Dictionary
  • Python Set

Understanding Python Numbers:

There are interegers, floating number and complex numers in the category of Python Numbers.These data types are defined as int, float, and complex classes.

You can use the type() function to know which class a variable or a value belongs to.

Similarly, the isinstance() function is used to check if an object belongs to a particular class.

Important Points For Data Types In Python

Integer : It can be of any lenght and limited by the memory available.

Floating Point Number: The floating point numbers are accurate up to 15 decimal places.

Complex Number: It is written x + yj, where x is the real part and y is the imaginary part.

Let's see the example to understand the data types in Python.

Program For Data Types

a = 5
print(a, "is of type", type(a))

a = 2.0
print(a, "is of type", type(a))

a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True

Python List

Listis basically an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.

Items separated by commas are enclosed within brackets [ ].

a = [1, 2.2, 'python']

Program To Understand The Use Of Python List Data Type

a = [5,10,15,20,25,30,35,40]

# a[2] = 15
print("a[2] = ", a[2])

# a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])

# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])
a[2] =  15
a[0:3] =  [5, 10, 15]
a[5:] =  [30, 35, 40]

Python Tuples

Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified.

Tuples are used to write-protect data and are usually faster than lists as they cannot change dynamically.

It is defined within parentheses () where items are separated by commas.

Program To Understand The Use Of Tuples

t = (5,'program', 1+3j)

# t[1] = 'program'
print("t[1] = ", t[1])

# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])
t[1] =  program
t[0:3] =  (5, 'program', (1+3j))

Python Strings

String is sequence of Unicode characters.

We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ''' or """.

Program To Understand The Use Of Python Strings

s = "This is a string"
s = '''A multiline
This is a string
A multiline

Python Dictionary

Dictionary is an unordered collection of key-value pairs.

Dictionary is generally used when you are dealing with huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.

In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.

Program To Understand The Application Of Python Dictionary

d = {1:'value','key':2}

print("d[1] = ", d[1]);

print("d['key'] = ", d['key']);
<class 'dict'>
d[1] =  value
d['key'] =  2

Python Set

Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.

Program To Understand The Application Of Python Set

a = {5,2,3,1,4}

# printing set variable
print("a = ", a)

# data type of variable a
a =  {1, 2, 3, 4, 5}
<class 'set'>

Program To Calculate The Are Of Triangle

The variable a, b and c are three sides of a triangle. Then,

Formula Of Triangle

s = (a+b+c)/2 area = √(s(s-a)(s-b)(s-c))

# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
The area of the triangle is 14.70

In the above program you can even take the input ( the values for 3 sides of triangle from the user. Taking input from the user is already explained in "Part-1")

A Simple Program To Reverse The Number

n=int(input("Enter number: "))
print("Reverse of the number:",rev)
Enter number: 23
Reverse of the number: 32
Dolly Solanki  Dolly Solanki

Share Views Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.