0% found this document useful (0 votes)
4 views28 pages

Lab Instructions Converting Data Types in Python

The document provides a hands-on lab module for converting data types in Python, focusing on identifying and converting various data types using built-in functions. Participants will learn to manage data type conversions through exercises that include using the type() function and converting between strings, integers, and floats. The lab emphasizes practical skills aligned with exam objectives related to fundamental data types and their characteristics.

Uploaded by

joeyfjr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

Lab Instructions Converting Data Types in Python

The document provides a hands-on lab module for converting data types in Python, focusing on identifying and converting various data types using built-in functions. Participants will learn to manage data type conversions through exercises that include using the type() function and converting between strings, integers, and floats. The lab emphasizes practical skills aligned with exam objectives related to fundamental data types and their characteristics.

Uploaded by

joeyfjr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Converting Data Types in Python

Introduction
Objective
Welcome to the Converting Data Types in Python practice lab. In this module, you will be
provided with the instructions and devices needed to develop your hands-on skills.

Overview

Learning Outcomes:
In this module, you will complete the following exercise:

Exercise 1 - Managing Data Type Conversions in Python

After completing this lab, you will be able to:

Identify data types


Convert data types in Python

Exam Objectives:
The following exam objective is covered in this lab:

1.2 Compare and contrast fundamental data types and their characteristics.

Note: Our main focus is to cover the practical, hands-on aspects of the exam
objectives. We recommend referring to course material or a search engine to
research theoretical topics in more detail.

Exercise 1 - Managing Data Type Conversions in Python


Python provides the type() function to identify the data types of variables used in programs.
At times, you may need to convert the existing variables in the programs to other data types.
For example, you might need to join strings and numerals, or add whole numbers and decimal
numbers. To handle these situations, one option is to re-initialize all the required variables with
suitable data types. However, re-initialization of variables in lengthy and complex programs
can be a time-consuming intense effort.

To take care of such situations, Python supports data type conversions.

In this exercise, you will write a program to identify the data types of given variables as well
as perform data type conversions in Python.

Please refer to your course material or use your favorite search engine to research this topic in
more detail.

Learning Outcomes
After completing this exercise, you will be able to:

Identify data types


Convert data types in Python

Your Devices
You will be using the following device in this exercise. Please power this on now.

PLABWIN10 - Workstation (Windows 10 Pro)

Task 1 - Identify Data Types


The type() function is a built-in function in Python programming language. The type() function
takes the relevant variable as its parameter and returns the data type of the variable.

In this task, you will write a program in python to use the type() function to determine the
data types of the declared variables.

1 Click the Start button.


2 Power on and connect to PLABWIN10.

Sign in with the following credentials:

Username: PRACTICELABS\Administrator

Password: Passw0rd

Click the Start charm.

On the Start menu, select Python 3.7 > IDLE.


3 The Python 3.7.2 Shell window is displayed.

Note: This is the interactive mode in Python. To create a file and save it for reuse,
you switch to script mode.

To switch to script mode and create a file, click File > New File.
4 Untitled window is displayed.

At the prompt, type the following code:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))

In the code, the variable a is declared and assigned the value 12. The built-in type() function
determines the data type of the variable passed as its argument. The print function displays
the data type of the variable.

5 To save the file, access the menu bar at the top and select File > Save.
6 The Save As dialog box is displayed.

On the navigation pane at the left, navigate to This PC > Desktop.

In the File name box, type the following:


Converttypes

Click Save.

The file Converttypes.py is saved in the Desktop folder.


7 Back on the program or script, notice that the title bar changes to Converttypes.py.

To run the script, access the menu bar at the top and click Run > Run Module.

Alternatively, you can press F5.

8 The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

The data type of the variable a is an integer.


9 Close or minimize the Python 3.7.2 Shell window to access the Converttypes.py file.

Next, use the type() function to identify the string data type.

Note: In Python, you can assign string values in single or double quotes.

To identify the data type of a string variable assigned value in double quotes, use the
following code:
#Use the type() function to identify the data type of variable 'b'
b=”Galaxy”
print('The data type of variable b is:', type(b))

Add this code to the existing script to use the type() function:
#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))

To save the code, press Ctrl + S.

In the code, variable b is declared with a value, enclosed in double quotes. The variable b is
then passed as the argument to the type() function and the function returns the data type of
the variable. The print statement displays the data type of variable b.

10 To run the program or script, click Run > Run Module, or press F5.
11 The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

Notice that the output states that the data type of variable b is string.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.

12 Close the Python 3.7.2 Shell window. The Convertypes.py script is displayed.

Next, use the type() function to identify the data type of the variable when the value is
enclosed in single quotes. Type:
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))

Add the code to the Conerttypes.py script:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))

To save the code, press Ctrl + S.

In the code, variable c is declared with a value enclosed in single quotes. The variable c is
passed as the argument to the type() function. The data type of variable c is displayed as the
output.
13 To run the program or script, click Run > Run Module, or press F5.

The program is executed, and the output is displayed in Python 3.7.2 Shell window.

The data type of variable c is string.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.

14 Next, use the type() function to identify the float data type. Use the following code:

#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))

Add this code to the code saved in the Converttypes.py file:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))
#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))

To save the code, press Ctrl + S.

In the code, variable d is declared with a value. The variable d is passed as the argument to
the type() function. The data type of variable d is displayed as the output.

15 To run the program or script, click Run > Run Module, or press F5.

The program is executed, and the output is displayed in Python 3.7.2 Shell window.

The data type of variable d is float.


Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.

Task 2 - Convert Data Types in Python


In Python, you can easily convert the data type of one variable to another data type with the
help of built-in type conversion functions.

The built-in type conversion functions in Python include:

float(): This function helps to convert a variable of any data type to a floating point type.
int(): This function is used to convert a variable of any data type to an integer type.
str(): This function helps convert an integer variable to a string variable.
chr(): This function is used to determine the character value of an integer value that is
passed as the parameter.

In this task, you will write a program to perform the following data type conversions using the
built-in type-conversion functions:

String to integer
String to float
Integer to float
Float to integer
Integer to character
Integer to string

1 Ensure that you are connected to PLABWIN10 and the Converttypes.py file is

displayed.

Enter the following code to convert a string variable to an integer type variable:
#Convert a string variable to an integer type variable and perform addition
e='6537'
f=10
g=int(e)+f
print('The sum of e and f is:',g)
The code uses the built-in int() function to convert a string variable to an integer type variable.

Add the code to the Converttypes.py file:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))
#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))
#Convert a string variable to an integer type variable and perform addition
e='6537'
f=10
g=int(e)+f
print('The sum of e and f is:',g)

To save the code, press Ctrl + S.

In the code, e is a string variable, and f is an integer variable. If you try to add e and f directly,
the interpreter will throw an error message. Therefore, you use the built-in int() function to
convert variable e to integer type and then perform the addition. The sum of e and f will be
displayed as the output.

2 To run the program or script, click Run > Run Module, or press F5.
The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

The sum of e and f is 6547 and is displayed as the output.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.

3 The code to convert a string variable to a float type variable:

#Convert a string variable to a float type variable and perform addition


h='5537'
i=10.463
j=float(h)+i
print('The sum of h and i is:',j)

You use the built-in float() function to convert a string variable to a float type variable.

Add the code to the file:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))
#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))
#Convert a string variable to an integer type variable and perform addition
e='6537'
f=10
g=int(e)+f
print('The sum of e and f is:',g)
#Convert a string variable to a float type variable and perform addition
h='5537'
i=10.463
j=float(h)+i
print('The sum of h and i is:',j)

To save the code, press Ctrl + S.

In the code, h is a string variable, and i is a float variable. If you try to add h and i directly, the
interpreter will throw an error message. Therefore, you need to use the built-in float() function
to convert variable h to float type and then perform the addition. The sum of h and i will be
displayed as the output.

4 To run the program or script, click Run > Run Module, or press F5.
The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

The sum of h and i is 5547.463 and is displayed as the output.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.

5 Next, convert an integer variable to a float type variable. Use the following code:

#Convert an integer variable to a float type variable


k=872
print('The converted floating point value of k is:',float(k))

You need to use the built-in float() function to convert an integer variable to a float type
variable.

Add the code to convert an integer variable to a float type variable:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))
#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))
#Convert a string variable to an integer type variable and perform addition
e='6537'
f=10
g=int(e)+f
print('The sum of e and f is:',g)
#Convert a string variable to a float type variable and perform addition
h='5537'
i=10.463
j=float(h)+i
print('The sum of h and i is:',j)
#Convert an integer variable to a float type variable
k=872
print('The converted floating point value of k is:',float(k))

To save the code, press Ctrl + S.

In the code, k is an integer variable. To convert the value of k into a floating-point value, you
use the built-in float() function. The floating-point value of k will be displayed as the output.
6 To run the program or script, click Run > Run Module, or press F5.

The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

The floating point value of k is 872.0 and is displayed as the output.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.

7 Next, convert float variables to integer type variables. Use the following code:
#Convert float variables to integer type variables and perform addition
l=1834.56
m=1294.68
n=int(l)+int(m)
print('The sum of l and m is:',n)

You use the built-in int() function to convert float variables to integer type variables.

Add the code to the Converttypes.py file:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))
#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))
#Convert a string variable to an integer type variable and perform addition
e='6537'
f=10
g=int(e)+f
print('The sum of e and f is:',g)
#Convert a string variable to a float type variable and perform addition
h='5537'
i=10.463
j=float(h)+i
print('The sum of h and i is:',j)
#Convert an integer variable to a float type variable
k=872
print('The converted floating point value of k is:',float(k))
#Convert float variables to integer type variables and perform addition
l=1834.56
m=1294.68
n=int(l)+int(m)
print('The sum of l and m is:',n)

To save the code, press Ctrl + S.

In the code, l and m are declared as float variables. The built-in int() function converts the
variables l and m to integer values. The integer values the variables l and m are added, and
the sum is stored in the variable n. The value of the variable n will be displayed as the output.
8 To run the program or script, click Run > Run Module, or press F5.
The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

The sum of the variables l and m is 3128 and is displayed as the output.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.

9 Next, use the chr() function to convert an integer variable to character type.
Use the following code:
#Convert an integer variable to character type
o=100
print('The character value of number 100 is:',chr(o))

Add the following code to use the chr() function to convert an integer variable to character
type:
#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))
#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))
#Convert a string variable to an integer type variable and perform addition
e='6537'
f=10
g=int(e)+f
print('The sum of e and f is:',g)
#Convert a string variable to a float type variable and perform addition
h='5537'
i=10.463
j=float(h)+i
print('The sum of h and i is:',j)
#Convert an integer variable to a float type variable
k=872
print('The converted floating point value of k is:',float(k))
#Convert float variables to integer type variables and perform addition
l=1834.56
m=1294.68
n=int(l)+int(m)
print('The sum of l and m is:',n)
#Convert an integer variable to character type
o=100
print('The character value of number 100 is:',chr(o))

To save the code, press Ctrl + S.

In the code, o is an integer variable. The chr() function will convert the value of variable o to its
corresponding character value. The character corresponding to the value of the variable o,
that is number 100 will be displayed as the output.

Note: The computer stores each character as an integer value. You can determine
the corresponding character of a given integer value by using the built-in chr()
function.
10 To run the program or script, click Run > Run Module, or press F5.
The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

The corresponding character stored in the system for number 100 is ‘d’ and is displayed as the
output.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.
11 Next, use the str() function to convert an integer variable to string type. Use the

following code:

#Convert an integer variable to string type


p="My age is"
q=20
print(p + "", str(q))

The str() function converts the integer variable to string type.

Add the code to use the str() function:


#Use the type() function to identify the data type of variable 'a'
a=12
print('The data type of variable a is:', type(a))
#Use the type() function to identify the data type of variable 'b'
b="Galaxy"
print('The data type of variable b is:', type(b))
#Use the type() function to identify the data type of variable 'c'
c='Fruit'
print('The data type of variable c is:', type(c))
#Use the type() function to identify the data type of variable 'd'
d=12.45
print('The data type of variable d is:', type(d))
#Convert a string variable to an integer type variable and perform addition
e='6537'
f=10
g=int(e)+f
print('The sum of e and f is:',g)
#Convert a string variable to a float type variable and perform addition
h='5537'
i=10.463
j=float(h)+i
print('The sum of h and i is:',j)
#Convert an integer variable to a float type variable
k=872
print('The converted floating point value of k is:',float(k))
#Convert float variables to integer type variables and perform addition
l=1834.56
m=1294.68
n=int(l)+int(m)
print('The sum of l and m is:',n)
#Convert an integer variable to character type
o=100
print('The character value of number 100 is:',chr(o))
#Convert an integer variable to string type
p="My age is"
q=20
print(p + "", str(q))

To save the code, press Ctrl + S.

In the code, p is a string variable, and q is an integer variable. The interpreter will not allow
you to join the values of p and q. Therefore, to concatenate the value of q with p, you need to
convert q into a string value. You can perform this conversion by using the built-in str()
function.
In the print function, an extra space is added within the double quotes to display the output in
a presentable format.

12 To run the program or script, click Run > Run Module, or press F5.
The program is executed, and the output is displayed in the Python 3.7.2 Shell window.

The string My age is 20 is displayed as the output.

Note: You have appended the code to the existing code. You will see the output of
the previous code first, followed by the output of the newly added code.
Review
Well done, you have completed the Converting Data Types in Python practice lab.

Summary
You completed the following exercise:

Exercise 1 - Managing Data Type Conversions in Python

You should now be able to:

Identify data types


Convert data types in Python

Feedback

Shutdown all virtual machines used in this lab. Alternatively, you can log out of the
lab platform.

You might also like