Python Input, Output, Python Import With Examples

In this tutorial we will cover the basics of Python Input/Output and also how to import a module in python program. Previously we learned about Python Data types. You can learn that from Python Data types.

Python Input Output

I/O means Input and Output. We will learn about basic functions of python input and output.

Printing Output to the Screen

We are already familiar with the print() function. It is used to print something to the screen.

print("Print something to the screen")

Moreover, we can pass two or more different strings in the print function separated by either commas (,) or plus (+) signs. Like this;

print("This is first String " + "Followed by this string");
#Or can be written like this also
print("This is another String " , "Followed by another string");

Taking User Input

Sometimes our program may need to read keyboard inputs given by user. For that reason we will use input() function. The input() function reads one line from standard input. Or in other words input function reads from keyboard input until a line feed is given ( i.e. Pressed Enter).

#takes input from keyboard and stores in a string
str = input("Enter your input: ");
#then we can print the string

If you run this program you will see a prompt like this below picture, waiting for you to give input. It will take as many characters as you type until Enter is pressed.


Python I/O – File Operations

Sometimes we need to read and write from files. For this reason there are some functions available.

File Opening

For opening a file we use open() functions. This is a built-in function in Python.

f = open("input_file.txt")    # open "input_file.txt" file in current directory
f = open("C:/Python34/README.txt")  # specifying full path of a file

You can also specify the mode or purpose for opening that file.

f = open("input_file.txt",'r')    # open "input_file.txt" file to read purpose
f = open("input_file",'w')  # open "input_file.txt" file to write purpose
f = open("input_file",'a')  # open "input_file.txt" file to append purpose

File Closing

When we are done with a file, we need to close it. For that purpose, we will use close() function.

f = open("input_file.txt")    # open "input_file.txt" file
#do file operation.

Reading From a file

For reading a file, there are several functions. In this below program, we will explore those functions.

  1. You can read a certain number of byte from the file with read() function.
    f = open("input.txt")    # open "input.txt" file #read first 10 bytes from the file.
    print(str)     #print first 10 bytes from the file.
  2. You can read file line by line with readline() function.
    f = open("input.txt")    # open "input.txt" file
    str=f.readline() #read first line from the file.
    print(str)     #print the first line.
    str=f.readline() #read second line from the file.
    print(str)     #print the second line.
  3. You can also read all the lines at once and store the lines in a list of strings.
    f = open("input.txt")    # open "input.txt" file
    str=f.readlines() #read all the lines from the file at once. and store as a list of string
    print(str)     #print list of all the lines.

Writing to a file

For writing something into a file is pretty simple and similar to file read. We will use write() function.

f = open("output.txt",'w')    # open "output.txt" file
#write something in the file.
f.write("this is my first linen")
f.write("this is my second linen")

Python Import

Whenever we want to use a package or module in our Python program, firstly we need to make it accessible. For that reason we need to import that package or module in our program.

Suppose, we have a number. we want to print it’s square root. So if we write this below program, it should work fine.

#get a variable having value 16
#square root this number.

But if we run this program, it will show errors like this-


This is because, sqrt() function is under module name “math”. So if we want to use this function, we need to make this module accessible by importing the “math” module. So the correct code will be like this –

#first import math module
import math
#get a variable having value 16
#square root this number.

So, that’s it for Python Input Output and Python Import. For more information about these topics you can check the official Python documentation from these below links.
Python Documentation I/O
Python Import

By admin

Leave a Reply

%d bloggers like this: