Lab Instructions Working With Primitive Data Types in Python
Lab Instructions Working With Primitive Data Types in Python
Python
Introduction
Objective
Welcome to the Working with Primitive 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 objectives are covered in this lab:
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 - Working with Primitive Data Types in Python
Python is an interactive, object-oriented, high-level, and user-friendly programming language.
The Integrated Development Environment (IDE) of Python is called IDLE. There are two modes
in IDLE - interactive and script. The Python Shell window is the interactive mode, and the
Editor window is the script mode.
In a programming language, data type defines the type of data used in the programs or
scripts. Just as in other programming languages, primitive data types are frequently used in
Python. The data types include string, integer, float, and boolean. You need not explicitly
mention the data types in Python programs. Data types are dynamically derived when you
assign values to it. This is known as the dynamic derivation feature of Python.
In this exercise, you will write and execute Python programs, using variables of primitive data
types, in both script and interactive mode.
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.
Username: PRACTICELABS\Administrator
Password: Passw0rd
To switch to script mode and create a file, click File > New File.
4 Untitled window is displayed.
To add a comment and declare a variable of string data type with a value, type the following
code:
#Declare a string variable
s = ‘Planet Earth’
print (s)
You can add comments in Python’s source code by marking the comment with a # symbol at
the beginning. As a developer, you can include comments in the source code for better
understanding and clarity. The Python interpreter will not process the text included with the #
symbol.
Unlike other programming languages, there is no keyword in Python to represent the primitive
data types. The Python interpreter dynamically considers the data types of variables based on
the values assigned to it.
In the code, a variable named “s” is declared, and a set of characters (‘Planet Earth’) enclosed
in single quotes is assigned to it. Therefore, the interpreter will interpret variable s as a string
data type. Instead of a set of characters, if you assign a numerical value to variable s the
interpreter will interpret it as a numerical data type.
The print function takes the variable name as its parameter and displays the value of the
variable.
5 To save the file, click File > Save.
7 Back on the script, notice the title bar now reads Datatypes.py and mentions the
8 The program is executed, and the output is displayed in the Python 3.7.2 Shell window.
Next, add the declaration of an integer variable with a value in the program. Use the following
code:
x = 5
Note: The integer data type in Python stores positive and negative numbers.
Add the comment text and the code to declare a variable of an integer data type with a value:
#Declare a string variable
s = ‘Planet Earth’
print (s)
#Declare an integer variable
x = 5
print (x)
In the code, a variable named x is declared, and a value of 5 is assigned to it. Therefore, x
becomes an integer variable. The print function displays the value of the variable x.
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 Python 3.7.2 Shell window.
Number 5 is displayed as the output, along with the output Planet Earth from the preceding
code in the file.
Note: The output of the integer variable is displayed along with the output of the
string variable.
Next, add the declaration of a float variable with a value in the program. Use the following
code:
y = 12.53
Add the comment text and the code to declare a variable of float data type with a value:
#Declare a string variable
s = 'Planet Earth'
print (s)
#Declare an integer variable
x = 5
print (x)
#Declare a float variable
y = 12.53
print (y)
To save the code, press Ctrl + S.
In the code, a variable named y is declared, and a value of 12.53 is assigned to it. Therefore, y
becomes a float variable. The print function will display the value of the variable y.
13 Run the program or script by clicking Run > Run Module, or pressing F5.
The program is executed, and the output is displayed in Python 3.7.2 Shell window.
Note: The output of the float variable is displayed along with the output of string
and integer variables.
14 Next, add the declaration of a boolean variable with a value in the program. Use the
following code:
z = x>y
The boolean data type in Python stores either True or False according to the evaluation of the
specified condition or comparison.
Add the comment text and the code to declare a variable of boolean data type with a value:
#Declare a string variable
s = 'Planet Earth'
print (s)
#Declare an integer variable
x = 5
print (x)
#Declare a float variable
y = 12.53
print (y)
#Declare a boolean variable
z = x>y
print (z)
In the code, variable z is declared. A comparison of the values of variables x and y is made. If
the value of x is greater than the value of y, the value of variable z will be True; else, it will be
False. Therefore, z is a boolean variable.
In the previous steps, variables x and y are assigned values 5 and 12.53 respectively. As
number 5 is not greater than 12.53, the value of variable z will be displayed as False. The print
function will display the value of variable z.
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.
Note: The output of the boolean variable is displayed along with the output of
string, integer, and float variables. Note: You can save Datatypes.py file on your
system. For more information on how to save the files on local systems, please visit
our Help and Support page.
Task 2 - Write and Execute a Program in Interactive Mode
In the interactive mode, you get immediate output for every code typed at the prompt.
However, you cannot save the typed code in a program file.
In this task, you will directly write the code at the prompt in Python Shell (the interactive
mode), and execute it. You can immediately see the output of each code written in the
interactive mode. However, you cannot save the code in a .py file. Therefore, the code written
in the interactive mode cannot be stored and reused.
You will write code to declare variables of string, integer, float, and boolean data types and
assign values to them. You will use the print function to display the assigned values. For the
purpose of this demonstration, you will be using the same code learned in the previous task.
If not already displayed, you can access the Python 3.7.2 Shell window by selecting the Start
charm > Python 3.7 > IDLE menu-options.
Note: If you want, you can maximize the Python 3.7.2 Shell window.
At the prompt, type the following code to declare a string variable with a value:
s = ‘Planet Earth’
Press Enter.
print (s)
Press Enter.
The string Planet Earth is displayed as the output using the print function.
3 At the next prompt in the Python 3.7.2 Shell window, type the following code to declare
x = 5
Press Enter.
print (x)
Press Enter.
5 At the next prompt in the Python 3.7.2 Shell window, type the following code to declare
y = 12.53
Press Enter.
print (y)
Press Enter.
7 At the next prompt in the Python 3.7.2 Shell window, type the following code to declare
z = x>y
Press Enter.
In the code, the boolean variable z is declared. The variable z will hold a value of either True or
False. The code will determine if the value of x is greater than the value of y. If yes, the output
will be True. If not, it will be False.
Note: There is no output for a variable declaration code.
8 At the next prompt, type the following code to print the value of the boolean variable:
print (z)
Press Enter.
In the previous steps, you have considered the value of variables x as 5 and y as 12.53. As the
number 5 is not greater than 12.53, the result of the comparison is False. The value of variable
z is False and is displayed as the output.
Leave the devices in their current states and continue to the next exercise.
Review
Well done, you have completed the Working with Primitive 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.