Data Types and type() Method

Datatypes:

- Numbers :

      -- int: 3 , 5, 7

      -- Float: 2.5 , 0.5 , 4.6

      -- Complex Numbers: 2+1j

- String : a set of characters

- List : my_list = [1, 2, 3, 4, 5]

- Tuple : my_tuple = ("dogs", "cats", "birds")

- Dictionary : my_dict = {

                  "brand": "Audi",    
                  "model": "Q5",
                  "year": 2020
                }

- Boolean : True , False -> True is 1, False is 0

Example : Checking type of input with type()

In [16]:
A = 4

B = 3.5

C = 3+2j

D = "hello"

my_list = [1, 2, 3, 4, 5]
    
my_tuple = ("dogs", "cats", "birds") 
    
my_dict = {
                      "brand": "Audi",    
                      "model": "Q5",
                      "year": 2020
                    }
In [17]:
# checking type of A , B , C , D , my_list, my_tuple, my_dict

print(type(A))

print(type(B)) 

print(type(C))

print(type(D))

print(type(my_list))

print(type(my_tuple))

print(type(my_dict))
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>

Example (True(1) & False(0) -> Boolean Type)

In [36]:
# What is the type of True/False?

print(type(True))

print(type(False))
<class 'bool'>
<class 'bool'>
In [37]:
# What is the interger True/False?

print(int(True))

print(int(False))
1
0
In [39]:
# What is the boolean of 0/1?

print(bool(0))

print(bool(1))
False
True

Converting Datatypes

Example ( Converting int to float) -> float(input)

In [24]:
K = 70

print(K, type(K))

# Convert type K from int to float
print(float(K))
70 <class 'int'>
70.0

Example ( Converting float to int) -> int(input)

In [32]:
K = 3.56

print(K, type(K))

# Convert type K from float to int

z = int(K)
print(z , type(z))
3.56 <class 'float'>
3 <class 'int'>

Example ( Converting float/int to string) -> str(input)

In [35]:
M = 3.56
N = 12

print(M, type(M))
print(N, type(N))

# Convert type K from float/int to string

Z1 = str(M)
Z2 = str(N)

print(Z1, type(Z1))
print(Z2, type(Z2))
3.56 <class 'float'>
12 <class 'int'>
3.56 <class 'str'>
12 <class 'str'>