8. Input And Output Statements
8. 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.
-----------------------------------------------------
-----------------------------------------------------------
Q. Write a program to read Employee data from the keyboard and print that data.
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
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.
D:\Python_classes\py test.py
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:
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")
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
1) print("Hello"+"World")
2) print("Hello","World")
3)
5) Hello World
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("KDN")
3. print("Soft")
Output:
1. Hello
2. KDN
3. 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)
arguments. Eg:
1. s="KDN"
2. a=48
3. s1="java"
4. s2="Python"
Output:
%i====>int
%d====>int
%f=====>float
%s======>String type
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]
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) print(f"Hello {name} your salary is {salary} and Your Friend {gf} is waiting")
7)
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