Input and Output Statements
Input and Output Statements
1. raw_input()
2. input()
1. raw_input():
This function always reads the data from the keyboard in the form of String Format. We
have to convert that string type to our required type by using the corresponding type
casting methods.
Eg:
x=raw_input("Enter First Number:")
print(type(x)) It will always print str type only for any input type
2. input():
input() function can be used to read data directly in our required format.We are not
required to perform type casting.
x=input("Enter Value)
type(x)
10 ===> int
"durga"===>str
10.5===>float
True==>bool
***Note: But in Python 3 we have only input() method and raw_input() method is not
available.
Python3 input() function behaviour exactly same as raw_input() method of Python2. i.e
every input value is treated as str type only.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Eg:
Q. Write a program to read 2 numbers from the keyboard and print sum.
-----------------------------------------------------
-----------------------------------------------------------
Q. Write a program to read Employee data from the keyboard and print that data.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
2 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
13) D:\Python_classes>py test.py
14) Enter Employee No:100
15) Enter Employee Name:Sunny
16) Enter Employee Salary:1000
17) Enter Employee Address:Mumbai
18) Employee Married ?[True|False]:True
19) Please Confirm Information
20) Employee No : 100
21) Employee Name : Sunny
22) Employee Salary : 1000.0
23) Employee Address : Mumbai
24) Employee Married ? : True
Note: split() function can take space as seperator by default .But we can pass
anything as seperator.
Q. Write a program to read 3 float numbers from the keyboard with , seperator and print
their sum.
eval():
eval Function take a String and evaluate the Result.
Eg: x = eval(“10+20+30”)
print(x)
Output: 60
Eg: Write a Program to accept list from the keynboard on the display
1) l = eval(input(“Enter List”))
2) print (type(l))
3) print(l)
Within the Python Program this Command Line Arguments are available in argv. Which is
present in SYS Module.
test.py 10 20 30
Note: argv[0] represents Name of Program. But not first Command Line Argument.
argv[1] represent First Command Line Argument.
import argv
print(type(argv))
D:\Python_classes\py test.py
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
4 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
10) The List of Command Line Arguments: [‘test.py’, ‘10’,’20’,’30’]
11) Command Line Arguments one by one:
12) test.py
13) 10
14) 20
15) 30
Note1: usually space is seperator between command line arguments. If our command line
argument itself contains space then we should enclose within double quotes(but not
single quotes)
Eg:
Note2: Within the Python program command line arguments are available in the String
form. Based on our requirement,we can convert into corresponding type by using type
casting methods.
Eg:
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
5 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
4)
5) D:\Python_classes>py test.py 10 20
6) 1020
7) 30
Note3: If we are trying to access command line arguments with out of range index then
we will get Error.
Eg:
Note:
In Python there is argparse module to parse command line arguments and display some
help messages whenever end user enters wrong input.
input()
raw_input()
output statements:
We can use print() function to display output.
Form-2:
1) print(String):
2) print("Hello World")
3) We can use escape characters also
4) print("Hello \n World")
5) print("Hello\tWorld")
6) We can use repetetion operator (*) in the string
7) print(10*"Hello")
8) print("Hello"*10)
9) We can use + operator also
10) print("Hello"+"World")
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
6 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Note:
If both arguments are String type then + operator acts as concatenation operator.
If one argument is string type and second is any other type like int then we will get Error
If both arguments are number type then + operator acts as arithmetic addition operator.
Note:
1) print("Hello"+"World")
2) print("Hello","World")
3)
4) HelloWorld
5) Hello World
1. a,b,c=10,20,30
2. print("The Values are :",a,b,c)
3.
4. OutputThe Values are : 10 20 30
By default output values are seperated by space.If we want we can specify seperator by
using "sep" attribute
1. a,b,c=10,20,30
2. print(a,b,c,sep=',')
3. print(a,b,c,sep=':')
4.
5. D:\Python_classes>py test.py
6. 10,20,30
7. 10:20:30
1. print("Hello")
2. print("Durga")
3. print("Soft")
Output:
1. Hello
2. Durga
3. Soft
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
7 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
1. print("Hello",end=' ')
2. print("Durga",end=' ')
3. print("Soft")
Note: The default value for end attribute is \n,which is nothing but new line character.
We can pass any object (like list,tuple,set etc)as argument to the print() statement.
Eg:
1. l=[10,20,30,40]
2. t=(10,20,30,40)
3. print(l)
4. print(t)
We can use print() statement with String and any number of arguments.
Eg:
1. s="Durga"
2. a=48
3. s1="java"
4. s2="Python"
5. print("Hello",s,"Your Age is",a)
6. print("You are teaching",s1,"and",s2)
Output:
%i====>int
%d====>int
%f=====>float
%s======>String type
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
8 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Syntax:
1) a=10
2) b=20
3) c=30
4) print("a value is %i" %a)
5) print("b value is %d and c value is %d" %(b,c))
6)
7) Output
8) a value is 10
9) b value is 20 and c value is 30
Eg 2:
1) s="Durga"
2) list=[10,20,30,40]
3) print("Hello %s ...The List of Items are %s" %(s,list))
4)
5) Output Hello Durga ...The List of Items are [10, 20, 30, 40]
Eg:
1) name="Durga"
2) salary=10000
3) gf="Sunny"
4) print("Hello {0} your salary is {1} and Your Friend {2} is waiting".format(name,salary,gf))
5) print("Hello {x} your salary is {y} and Your Friend {z} is waiting".format(x=name,y=salary,z=
gf))
6)
7) Output
8) Hello Durga your salary is 10000 and Your Friend Sunny is waiting
9) Hello Durga your salary is 10000 and Your Friend Sunny is waiting
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
9 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com