Updated On : May-01,2020  python, basics # 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¶

In :
```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 [ ].

In :
```a = [1, 2.2, 'python']
```

### Program To Understand The Use Of Python List Data Type¶

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

# a = 15
print("a = ", a)

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

# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])
```
```a =  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¶

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

# t = 'program'
print("t = ", t)

# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])
```
```t =  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¶

In :
```s = "This is a string"
print(s)
s = '''A multiline
string'''
print(s)
```
```This is a string
A multiline
string
```

### 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¶

In :
```d = {1:'value','key':2}
print(type(d))

print("d = ", d);

print("d['key'] = ", d['key']);
```
```<class 'dict'>
d =  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¶

In :
```a = {5,2,3,1,4}

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

# data type of variable a
print(type(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))

In :
```# 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¶

In :
```n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)
```
```Enter number: 23
Reverse of the number: 32
```

Dolly Solanki