Data type Conversion in Python ppt

The process of converting a Python data type into another data type is known as type conversion. There are mainly two types of type conversion methods in Python, namely, implicit type conversion and explicit type conversion.

In this module, we will go through the following topics:

So, without any further delay, let’s get started.

Implicit Type Conversion in Python

In Python, when the data type conversion takes place during compilation or during the run time, then it’s called an implicit data type conversion. Python handles the implicit data type conversion, so we don’t have to explicitly convert the data type into another data type. We have seen various examples of this kind of data type conversion throughout the tutorial.

Don’t worry if you can’t recall it. Let’s see another example.

Let’s add two Python variables of two different data types and store the result in a variable and see if the Python compiler is able to convert the data type of the resultant variable or not.

a = 5
b = 5.5
sum = a + b
print (sum)
print (type (sum)) #type() is used to display the datatype of a variable


Output:
10.5

In the above example, we have taken two variables of integer and float data types and added them. Further, we have declared another variable named ‘sum’ and stored the result of the addition in it. When we checked the data type of the sum variable, we can see that the data type of the sum variable has been automatically converted into the float data type by the Python compiler. This is called implicit type conversion.

The reason that the sum variable was converted into the float data type and not the integer data type is that if the compiler had converted it into the integer data type, then it would’ve had to remove the fractional part and that would’ve resulted in data loss. So, Python always converts smaller data type into larger data type to prevent the loss of data.

In some cases, Python cannot use implicit type conversion and that’s where explicit type conversion comes into play. Moving ahead, let’s learn more about it.

Enroll yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming!

Get 100% Hike!

Master Most in Demand Skills Now !

Explicit Type Conversion in Python

Explicit type conversion is also known as typecasting. Explicit type conversion takes place when the programmer clearly and explicitly defines the same in the program. For explicit type conversion, there are some in-built Python functions. The following table contains some of the in-built functions for type conversion, along with their descriptions.

FunctionDescriptionint(y [base])It converts y to an integer, and Base specifies the number base. For example, if you want to convert the string in decimal numbers then you’ll use 10 as base.float(y)It converts y to a floating-point number.complex(real [imag])It creates a complex number.str(y)It converts y to a string.tuple(y)It converts y to a tuple.list(y)It converts y to a list.set(y)It converts y to a set.dict(y)It creates a dictionary and y should be a sequence of (key, value) tuples.ord(y)It converts a character into an integer.hex(y)It converts an integer to a hexadecimal string.oct(y)It converts an integer to an octal string

Now that we know the in-built functions provided by Python that are used for explicit type conversion, let’s see the syntax for explicit type conversion:

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.

There are various data types in Python. 

Python Numbers

  • Integers, floating point numbers and complex numbers are listed under Python Numbers Category.
  • Python numbers are defined as 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    1, 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    2 and 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    3 classes in Python.
temp = 5
print(temp, "is of type", type(temp))

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

complexNum = 1+2j
print(a, "is complex number?", isinstance(1+2j,complexNum))
  • Integers can be of any length and it is only limited by the memory available.
  • A floating-point number is accurate up to 15 decimal places.
  • Integer and floating points are separated by decimal points. 1 is an integer, 1.0 is a floating-point number.
  • Complex numbers are written in the form, 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    4, where x is the real part and y is the imaginary part. Here are some examples.
temp = 1234567890123456789
print(temp)
#    1234567890123456789
b = 0.1234567890123456789
print(b)
#    0.12345678901234568
m = 1+2j
print(m)
#     (1+2j)

Python List

  • List in python is 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 can be different data type.
  • list Items separated by commas are enclosed within brackets 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    5.
  • Use the slicing operator 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    5 to extract an item or a range of items from a list.
  • The index starts from 0 in Python.
  • Lists are mutable, meaning, the value of elements of a list can be altered.
temp = [5,10,15,20,25,30,35,40]

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

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

# temp[5:] = [30, 35, 40]
print("temp[5:] = ", temp[5:])

a = [1, 2, 3]
a[2] = 4       # mutable
print(a)

Python Tuple

  • 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 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    7 where items are separated by commas.
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])

# Generates error
# Tuples are immutable
t[0] = 10

Python Strings

  • Strings is sequence of Unicode characters.
  • use single quotes or double quotes to represent strings.
  • Multi-line strings can be denoted using triple quotes, 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    8 or 
    temp = 1234567890123456789
    print(temp)
    #    1234567890123456789
    b = 0.1234567890123456789
    print(b)
    #    0.12345678901234568
    m = 1+2j
    print(m)
    #     (1+2j)
    9
str1 = 'Hello world!'

# str1[4] = 'o'
print("str1[4] = ", str1[4])

# str1[6:11] = 'world'
print("str1[6:11] = ", str1[6:11])

# Generates error
# Strings are immutable in Python
str1[5] ='d'

Python Set

  • Set is an unordered collection of unique items.
  • Set is defined by values separated by comma inside braces 
    temp = [5,10,15,20,25,30,35,40]
    
    # temp[2] = 15
    print("a[2] = ", temp[2])
    
    # temp[0:3] = [5, 10, 15]
    print("temp[0:3] = ", temp[0:3])
    
    # temp[5:] = [30, 35, 40]
    print("temp[5:] = ", temp[5:])
    
    a = [1, 2, 3]
    a[2] = 4       # mutable
    print(a)
    0.
  • Items in a set are not ordered.
  • set are unordered collection, indexing has no meaning. Hence, the slicing operator 
    temp = [5,10,15,20,25,30,35,40]
    
    # temp[2] = 15
    print("a[2] = ", temp[2])
    
    # temp[0:3] = [5, 10, 15]
    print("temp[0:3] = ", temp[0:3])
    
    # temp[5:] = [30, 35, 40]
    print("temp[5:] = ", temp[5:])
    
    a = [1, 2, 3]
    a[2] = 4       # mutable
    print(a)
    1 does not work.
test = {5,2,3,1,4}

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

# data type of variable a
print(type(test))


sample = {1,2,3}
print(sample[1])  # does not work

Python Dictionary

  • Dictionary is an unordered collection of key-value pairs.
  • Dictionaries are used when there is large dataset processing.
  • Dictionaries are optimized for retrieving data.
  • key is required to retrieve the value.
  • Dictionaries are defined within braces 
    temp = [5,10,15,20,25,30,35,40]
    
    # temp[2] = 15
    print("a[2] = ", temp[2])
    
    # temp[0:3] = [5, 10, 15]
    print("temp[0:3] = ", temp[0:3])
    
    # temp[5:] = [30, 35, 40]
    print("temp[5:] = ", temp[5:])
    
    a = [1, 2, 3]
    a[2] = 4       # mutable
    print(a)
    2 with each item being a pair in the form 
    temp = [5,10,15,20,25,30,35,40]
    
    # temp[2] = 15
    print("a[2] = ", temp[2])
    
    # temp[0:3] = [5, 10, 15]
    print("temp[0:3] = ", temp[0:3])
    
    # temp[5:] = [30, 35, 40]
    print("temp[5:] = ", temp[5:])
    
    a = [1, 2, 3]
    a[2] = 4       # mutable
    print(a)
    3.
  • Key and value can be of any type.
dict1 = {1:'value','key':4}
print(type(dict1))

print("dict1[1] = ", dict1[1])

print("dict1['key'] = ", dict1['key'])

Conversion between data types

Data types can be converted using different type conversion functions like 

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

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

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

# temp[5:] = [30, 35, 40]
print("temp[5:] = ", temp[5:])

a = [1, 2, 3]
a[2] = 4       # mutable
print(a)
4, 
temp = [5,10,15,20,25,30,35,40]

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

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

# temp[5:] = [30, 35, 40]
print("temp[5:] = ", temp[5:])

a = [1, 2, 3]
a[2] = 4       # mutable
print(a)
5, 
temp = [5,10,15,20,25,30,35,40]

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

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

# temp[5:] = [30, 35, 40]
print("temp[5:] = ", temp[5:])

a = [1, 2, 3]
a[2] = 4       # mutable
print(a)
6, etc.

float(5)
int(10.6)
float('2.5')
str(25)

Type Conversion

The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has two types of type conversion.

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Conversion

In Implicit type conversion, Python converts one data type to another data type. This process doesn’t need any user involvement.

var_int = 123
var_float = 1.23

var_new = var_int + var_float

print("datatype of var_int:",type(var_int))
print("datatype of var_float:",type(var_float))

print("Value of num_new:",var_new)
print("datatype of var_new:",type(var_new))

Addition of string(higher) data type and integer(lower) datatype

var_int = 300
var_str = "500"

print(var_int + var_str)

Explicit Type Conversion

In Explicit Type Conversion, users convert the data type of an object to required data type. Users use the predefined functions like 

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

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

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

# temp[5:] = [30, 35, 40]
print("temp[5:] = ", temp[5:])

a = [1, 2, 3]
a[2] = 4       # mutable
print(a)
4, 
temp = [5,10,15,20,25,30,35,40]

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

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

# temp[5:] = [30, 35, 40]
print("temp[5:] = ", temp[5:])

a = [1, 2, 3]
a[2] = 4       # mutable
print(a)
5, 
temp = [5,10,15,20,25,30,35,40]

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

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

# temp[5:] = [30, 35, 40]
print("temp[5:] = ", temp[5:])

a = [1, 2, 3]
a[2] = 4       # mutable
print(a)
6, etc to perform explicit type conversion.

What is data type conversion in Python?

Type conversion is the process of converting a data type into another data type. Implicit type conversion is performed by a Python interpreter only. Explicit type conversion is performed by the user by explicitly using type conversion functions in the program code. Explicit type conversion is also known as typecasting.

What is data type conversion explain with an example?

In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.

What are the types of type conversion in Python?

There are two types of Type Conversion in Python:.
Implicit Type Conversion..
Explicit Type Conversion..

What are data type conversions?

Data type conversion occurs automatically for different numeric types such as from floating-point database column values into integer C variables, and for character strings, such as from varying-length Ingres character fields into fixed-length C character string buffers.