0% found this document useful (0 votes)
3 views18 pages

Lab Instructions Working With Primitive Data Types in Python

This document outlines a practice lab focused on working with primitive data types in Python, including string, integer, float, and boolean. It provides step-by-step instructions for writing and executing programs in both script and interactive modes using Python's IDLE. The lab aims to enhance hands-on programming skills and covers key exam objectives related to programming concepts.

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)
3 views18 pages

Lab Instructions Working With Primitive Data Types in Python

This document outlines a practice lab focused on working with primitive data types in Python, including string, integer, float, and boolean. It provides step-by-step instructions for writing and executing programs in both script and interactive modes using Python's IDLE. The lab aims to enhance hands-on programming skills and covers key exam objectives related to programming concepts.

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/ 18

Working with Primitive Data Types in

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:

Exercise 1 - Working with Primitive Data Types in Python

After completing this lab, you will be able to:

Write and execute a program in script mode


Write and execute a program in interactive mode

Exam Objectives:
The following exam objectives are covered in this lab:

4.1 Compare and contrast programming language categories


4.3 Explain the purpose and use of programming concepts

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:

Write and execute a program in script mode


Write and execute a program in interactive mode

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

PLABWIN10 - Workstation (Windows 10 Pro)

Task 1 - Write and Execute a Program in Script Mode


In the script mode, you can write a block of code, save it as a .py file, and execute it. The
output of the .py file is displayed at the Shell prompt. You can reuse the stored .py file and
make necessary changes whenever required.
In this task, you will write a program in Python to declare and print the values of primitive
data types such as string, integer, float, and boolean.

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, click Python 3.7 > IDLE.


3 Python 3.7.2 Shell window is displayed. This is the interactive mode in Python.

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

This is the script mode in Python.

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.

6 In the Save As dialog box, navigate to This PC > Desktop folder.

In the File name box, type the following file name:


Datatypes
Click Save.

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

7 Back on the script, notice the title bar now reads Datatypes.py and mentions the

directory path of the file.

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

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

The string Planet Earth is displayed as the output.


9 Close the output screen to access the Datatypes.py file.

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)

To save the code, press Ctrl + S.

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.

12 Again, close the shell window to access the python script.

Next, add the declaration of a float variable with a value in the program. Use the following
code:
y = 12.53

The float data type in Python stores floating point values.

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.

Number 12.53 is displayed as the output.

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)

To save the code, press Ctrl + S.

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.

The value of the boolean variable is displayed as False.

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.

1 Ensure that you are connected to PLABWIN10.

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.

The string variable “s” is declared with a value.

Note: There is no output for a variable declaration code.


2 At the next prompt, type the following code to print the value of the string variable:

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

an integer variable with a value:

x = 5

Press Enter.

The integer variable “x” is declared with a value of 5.

Note: There is no output for a variable declaration code.


4 At the next prompt, type the following code to print the value of the integer variable:

print (x)

Press Enter.

Number 5 is displayed as the output.

5 At the next prompt in the Python 3.7.2 Shell window, type the following code to declare

a float variable with a value:

y = 12.53

Press Enter.

The float variable y is declared with a value of 12.53.

Note: There is no output for a variable declaration code.


6 At the next prompt, type the following code to print the value of the float variable:

print (y)

Press Enter.

The number 12.53 is displayed as the output.

7 At the next prompt in the Python 3.7.2 Shell window, type the following code to declare

a boolean variable with a value:

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:

Exercise 1 - Working with Primitive Data Types in Python

You should now be able to:

Write and execute a program in script mode


Write and execute a program in interactive mode

Feedback

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

You might also like