Professional Documents
Culture Documents
9.python Input and Output
9.python Input and Output
Home /
Tutorial /
Python Input and Output
While working in a programming language, you may require to input some data and then use instruction to output a value. Also,
you might work on files that may need to be opened, closed, read, or write. Thus, you should have knowledge of input and
output in Python. In this part of the Python tutorial, you learn how to input and output data. Also, you will gain knowledge about
file handling.
Python Tutorial
Install Python?
Python Syntax
Comments in Python
Python Variables -
Constant, Global & Static
Variables
Numbers in Python
String in Python
Python Lists
Tuple in Python
Python Sets
Python Dictionary
Python Operators
Type conversion in
Python
Python If Else
Statements
Python Functions -
y
Define & Call a Functions
in Python
Lambda Function in
Python
Python Built in
Functions with Examples
Python Arrays
Python Modules
Python Dates
Python JSON
Python RegEx
PIP Python
User Input
Syntax
Where prompt is a string that will be display for the user to view which should provide some kind of indication of the usage
and type of data you are expecting and return is the returned value from the function.
e.g.
enter a value: i
>>> print i
Output
Hello Intellipaat
raw_input() function
raw_input() function is used to take input from the user. It takes the input from the Standard input in the form of a string
and reads the data from a line at once.
Syntax
raw_input(?statement?)
e.g.
print i
Output
Enter value
Hello intellipaat
e.g.
>>> print x
Then it shows error because numbers are not handled by raw_input() directly. To use numbers conversion is used.
e.g.
print x
User Output
Print statement is used to get the output.
Syntax
print expression
e.g.
x=20
print x
Output
20
File Handling
File is an external storage on hard disk from where data can be stored and retrieved. Python supports reading data form
files and writing data to the files.
Operations on Files:
Opening a file
– To open file built in function open() is used. It returns an object of File which is used with other functions.
Syntax:
obj=open(file_name , access_mode , buffer)
access_mode specify the access_mode in which the file has to be open, i.e., read, write, append, etc.
buffer – It represents that buffering is performed or not, if buffer value is 0 then no buffering is performed and when
buffer value is 1 then line buffering is performed while accessing the file.
Access Modes
Modes Description
w Opens a file for writing only. Overwrites the file if the file exists.
a Opens a file for appending. It does not overwrite the file just add the data in the file and if file is not
created then it creates new file
Closing a File:
It is used to close a file. For this purpose close() function is used.
Syntax:
File_object.close()
Writing to a File:
write() method is used to write a string into the file.
Syntax:
File_object.write(string str)
i=open("intellipaat.txt","w")
i.write("Hello Intellipaat")
i.close()
Syntax:
File_object.read(data)
e.g.
j=open("intellipaat.txt","r")
k=j.read()
print k
j.close()
Output
Hello Intellipaat
Methods in File Handling
There are different methods are used which are as follows:
Syntax:
eg:
import os
os.rename("abc.txt","xyz.txt")
Syntax:
os.remove(file_name)
e.g.
import os
os.remove("abc.txt")
Syntax:
os.chdir("new directory")
e.g.
import os
Syntax:
os.mkdir("new directory")
e.g.
import os
os.mkdir("abc")
Syntax
os.rmdir("directory name")
e.g.
import os
Syntax
os.getcwd()
e.g.
import os
print os.getcwd()
This blog will help you get a better understanding of Automate Your Coding with Python!
Previous Next
Course Schedule
Name Date
2022-05-07 2022-05-08
2022-05-14 2022-05-15
2022-05-21 2022-05-22
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name * Email *
Post Comment
Browse Categories