Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Science, Technology, Engineering and Mathematics

Empowerment Technologies
SY 2021 – 2022

Unit 2: Introduction to Python


Lesson 8: Python Input, Output, and Import

Introduction
Good day students! How are you? I
hope you are doing good. Today we will add
a new topic to your learnings about Python.
We are going to discuss today about input,
output and import in Python, which will help
you to improve your creation of a more
interactive program.

Learning Objectives
After studying and completing this module with 80% to 100% mastery of the topic and get at
least 8 correct answers to the learning activity and you are also expected to:
1. Understand the use of input, output, and import in creating a program.
2. Create a program using input and output function.
3. Display appreciation in learning the function of input and output in creating a program.

Pre-requisite Skills / Review


Before we begin to our lesson, let us have a short activity.

4 Pics 1 Word

Direction: Guess the specific word the fits with the theme of the images presented.

1. ______________________________

2. _______________________________

Page 1 of 8
Science, Technology, Engineering and Mathematics
Empowerment Technologies
SY 2021 – 2022

3. _______________________________

Lesson 1.1 – Python Input, Output, and Import

Python provides numerous built-in functions that are readily available to us at the Python prompt.
Some of the functions like input () and print () are widely used for standard input and output
operations.

Python Output Using print () function

The simplest way to produce output is using the print () function where you can pass zero or
more expressions separated by commas. This function converts the expressions you pass into
a string before writing to the screen.

Space can be added between the string and the value of the variable by default, but this can
be change.

The actual syntax of the print () function is:

Page 2 of 8
Science, Technology, Engineering and Mathematics
Empowerment Technologies
SY 2021 – 2022

OBJECTS – is the value to be printed in program.


SEP – separator used between values or objects.
END – It specifies what to print at the end and it defaults into a new line.
FILE – is the object where the values are printed and its default value is
sys.stdout (screen or standard output stream).
FLUSH - a Boolean, specifying if the output is flushed (True) or buffered
(False). Default: False
o TRUE - the output will be written as a sequence of characters one after
the other. This process is slow simply because it is easier to write in chunks
rather than writing one character at a time.

Here is an example to illustrate this.

OUTPUT FORMATTING

Here, the curly braces {} are used as placeholders. We can specify the order in which they
are printed by using numbers (tuple index).

We can even use keyword arguments to format the string.

Page 3 of 8
Science, Technology, Engineering and Mathematics
Empowerment Technologies
SY 2021 – 2022

➢ Python Input

The value of variables was defined or hard coded into the source code. To allow
flexibility, we need to take the input from the user. In Python, we have the input
() function to allow this. input () : This function first takes the input from the user and
then evaluates the expression, which means Python automatically identifies whether user
entered a string or a number or list.

The syntax for input () is:

Where prompt is the string, we wish to display on the screen. It is optional.

➢ Python Import
A module is a file containing Python definitions and statements. Python modules have
a filename and end with the extension (.py). Definitions inside a module can be
imported to another module or the interactive interpreter in Python. We use
the import keyword to do this.

Example: Created a Module named as constant.py, then import it in the new module
named as main.py.

Page 4 of 8
Science, Technology, Engineering and Mathematics
Empowerment Technologies
SY 2021 – 2022

OUTPUT:

o Import from Module

We can also import some specific attributes and functions only, using the from keyword.

Example:

OUTPUT:

Page 5 of 8
Science, Technology, Engineering and Mathematics
Empowerment Technologies
SY 2021 – 2022

Key Concepts

Python Output - The simplest way to produce output is using the print () function where
you can pass zero or more expressions separated by commas.
The actual syntax of the print () function is:

➢ OBJECTS – is the value to be printed in program.


➢ SEP – separator used between values or objects.
➢ END – It specifies what to print at the end and it defaults into a new line.
➢ FILE – is the object where the values are printed and its default value is
sys.stdout (screen or standard output stream).
➢ FLUSH - a Boolean, specifying if the output is flushed (True) or buffered
(False). Default: False
o TRUE - the output will be written as a sequence of characters one after
the other. This process is slow simply because it is easier to write in
chunks rather than writing one character at a time.

Python Input - This function first takes the input from the user and then evaluates the
expression, which means Python automatically identifies whether user entered a string or a
number or list.
The syntax for input () is:

Python Import - Definitions inside a module can be imported to another module or the
interactive interpreter in Python. We use the import keyword to do this.

Page 6 of 8
Science, Technology, Engineering and Mathematics
Empowerment Technologies
SY 2021 – 2022

Learning Activity

Create a simple program with the same output below, where you can input your personal
information and will display a welcome note below the information.

Study Questions/Forum:

1. What is the use of input, output and import in creating a program in Python?
2. What is the difference between the functions input and output?

Tutorial Videos

Here are some YouTube videos that can help you to with this topic.

Python Input and Output


o https://www.youtube.com/watch?v=C6_el8rxbkA

References

• Book:
o Python Notes for Professionals – Free Python Programming Book (goalkicker.com)
o A Practical Introduction to Python Programming - Brian Heinold
Department of Mathematics and Computer Science Mount St. Mary’s University

Page 7 of 8
Science, Technology, Engineering and Mathematics
Empowerment Technologies
SY 2021 – 2022

• Online Sources
o https://www.w3schools.com/python/python_intro.asp
o https://www.programiz.com/python-programming/first-program
o https://www.geeksforgeeks.org/python-language-introduction/
o https://www.educba.com/python-literals/
o https://www.codespeedy.com/literals-in-python-with-examples/

Enrichment Activity

Create a registration form like program which can be used to input your information when creating
a social media account.

Page 8 of 8

You might also like