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.
Table of Contents
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 print(str)
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.
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
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. f.close()
Reading From a file
For reading a file, there are several functions. In this below program, we will explore those functions.
- You can read a certain number of byte from the file with
f = open("input.txt") # open "input.txt" file str=f.read(10) #read first 10 bytes from the file. print(str) #print first 10 bytes from the file. f.close()
You can read file line by line with
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. f.close()
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. f.close()
Writing to a file
For writing something into a file is pretty simple and similar to file read. We will use
f = open("output.txt",'w') # open "output.txt" file #write something in the file. f.write("this is my first line\n") f.write("this is my second line\n") f.close()
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 number=16 #square root this number. number=sqrt(number) print(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 number=16 #square root this number. number=math.sqrt(number) print(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