Ticker

6/recent/ticker-posts

Python: Input And Output Statements

Reading dynamic input from the keyboard:

In Python 2 the following 2 functions are available to read dynamic input from the keyboard.

  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

Eg:

1) >>> type(input("Enter value:")) 
2) Enter value:10 
3) <class 'str'> 
4) 
5) Enter value:10.5 
6) <class 'str'> 
7) 
8) Enter value:True 
9) <class 'str'> 

Q. Write a program to read 2 numbers from the keyboard and print sum.

1) x=input("Enter First Number:") 
2) y=input("Enter Second Number:") 
3) i = int(x) 
4) j = int(y) 
5) print("The Sum:",i+j) 
6) 
7) Enter First Number:100 
8) Enter Second Number:200 
9) The Sum: 300 

-----------------------------------------------------

1) x=int(input("Enter First Number:")) 
2) y=int(input("Enter Second Number:")) 
3) print("The Sum:",x+y) 

-----------------------------------------------------------

1) print("The Sum:",int(input("Enter First Number:"))+int(input("Enter Second Number:"))) 

Q. Write a program to read Employee data from the keyboard and print that data.

1) eno=int(input("Enter Employee No:")) 
2) ename=input("Enter Employee Name:") 
3) esal=float(input("Enter Employee Salary:")) 
4) eaddr=input("Enter Employee Address:") 
5) married=bool(input("Employee Married ?[True|False]:")) 
6) print("Please Confirm Information") 
7) print("Employee No :",eno) 
8) print("Employee Name :",ename) 
9) print("Employee Salary :",esal) 
10) print("Employee Address :",eaddr) 
11) print("Employee Married ? :",married) 
12) 
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 

How to read multiple values from the keyboard in a single line:

1) a,b= [int(x) for x in input("Enter 2 numbers :").split()] 
2) print("Product is :", a*b) 
3) 
4) D:\Python_classes>py test.py 
5) Enter 2 numbers :10 20 
6) Product is : 200

Q. Write a program to read 3 float numbers from the keyboard with , seperator and print their sum.

1) a,b,c= [float(x) for x in input("Enter 3 float numbers :").split(',')] 
2) print("The Sum is :", a+b+c) 
3) 
4) D:\Python_classes>py test.py 
5) Enter 3 float numbers :10.5,20.6,20.1 
6) The Sum is : 51.2 

eval():

eval Function take a String and evaluate the Result.

Eg: x = eval(“10+20+30”)
 print(x)
 Output: 60
Eg: x = eval(input(“Enter Expression”))
 Enter Expression: 10+2*3/4
 Output: 11.5

eval() can evaluate the Input to list, tuple, set, etc based the provided Input.

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)