image
image
image

Input, Output, and Import in Python

image

In Python input and output (I/O) tasks are performed by inbuilt functions. The print() performs output/display, while input() performs input tasks.  The print() is used to output data to standard output devices such as a screen. However, it is also possible to copy data to a file. You are now familiar with the print function written as a print(). It is an inbuilt function because we do not have to write it and specify what and how it works. They are two main types of functions, inbuilt/built-in and user-defined functions that we will handle later. When we want the print function to reference or pick the value of a variable, we do not use double quotes.

––––––––

image

Example

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

number=6  #variable definition and assignment

print(number)  #displaying value stored in variable number

Note: Now assume we want to offer some explanation to the user running this program as he or she will only 6 and does not understand why 6 appears on the screen.

number=6

print(“This is a number stored in the variable number, it is:” number)

Note: We can display string and other data types in the print function. A comma is generally used to demarcate the end of one data type and the beginning of another.

Practice Exercise

a.  Given score=5, write a Python program that displays “The score is 5”. The program should reference/extract the value of score in its print function.

b.  Given age=26, write a Python to display “Richard’s age is 26”. The program should reference/extract the value of score in its print function.

c.  Given marks=87, write a Python program that displays “The average marks in the class is 87”. The program should reference/extract the value of score in its print function.

Note that the new line in Python is gotten by “\n” without the double quotes and outside the string or line code.