Lab Instructions Converting Data Types in Python
Lab Instructions 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:
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.
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:
Your Devices
You will be using the following device in this exercise. Please power this on now.
In this task, you will write a program in python to use the type() function to determine the
data types of the declared variables.
Username: PRACTICELABS\Administrator
Password: Passw0rd
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.
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.
Click Save.
To run the script, access the menu bar at the top and click Run > Run Module.
8 The program is executed, and the output is displayed in the Python 3.7.2 Shell window.
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))
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))
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.
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))
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.
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.
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.
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.
You use the built-in float() function to convert a string variable to a float type variable.
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.
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:
You need to use the built-in float() function to convert an integer variable to a float type
variable.
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.
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.
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))
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:
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.
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:
Feedback
Shutdown all virtual machines used in this lab. Alternatively, you can log out of the
lab platform.