Friday, December 20, 2019

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()

raw_input() : This function always read the data from the keyboard in the form of string format. We have to convert that string type to our required type by sung the corresponding type casting methods.

Eg:  k = raw_input(“Enter First Number”) 
       print(type(x))  —> it will always print str  type only for any input type.

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 python3 we have only input() method and raw_input method is not available.
Python3 input() function behaviors exactly same as raw_input  method of python2 . ie. Every input value is treated as str type only 
Raw_input function of python2 is renamed as input() function in python3

>>>>>>> type(input(“Enter Value:”))
Enter value:10
<class  ‘str’>

Enter value:10.5
<class  ‘str’>

Enter value:True
<class  ‘str’>

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

x=input(“Enter First Number”)
y=input(“Enter Second Number”)
i = int(x)
j = int(y)
print(“The Sum:”, i+j)

Enter First Number: 100
Enter Second Number: 200
The Sum: 300

x=int(input("Enter First Number:”))
y=int(input(“Enter Second Number:”))
print(“The Sum:”,x+y)

Print(“The Sum:”,int(input(“Enter First Number:”))+int(input(“Enter Second Number:”)))

Write a program.to read employee data from the keyboard and print the data 

eno=int(input(“Enter Employee No:”))
ename=input(“Enter Employee Name:”)
esal=float(input(“Enter Employee  Salary:”))
eaddr=input(“Enter Employee Address:”)
married=bool(input(“Employee Married ? [True[False]:”))
print(“Employee No:”,eno)
print("Employee Name:”,ename)
print("Employee Salary:”,esal)
print("Employee Address:”,eaddr)
print(“Employee Married:”,married)

D:\Python_classes>py test.py

Enter Employee No:100
Enter Employee Name:Sunny
Enter Employee  Salary:1000
Enter Employee Address:Mumbai
Employee Married ? [True[False]:True
Please confirm information 

Enter Employee No:100
Enter Employee Name:Sunny
Enter Employee  Salary:1000.0
Enter Employee Address:Mumbai
Employee Married ? :True

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

a,b = [int(x) for x in input(“Enter 2 numbers:”).split()]
print(“Product is:”, a*b)

D:\Python _classes>py test.py
Enter 2 numbers: 10 20 
Product is : 200

Note: split() function can take space as separator by default . But we can pass anything as separator 

Write a program to read 2 float numbers from the keyboard with separator and print their sum ?

a,b,c= [float(x) for x in input(“Enter 3 float numbers :”).split(‘ , ‘)]
print(“The Sum is :”, a+b+c)

D:\Python_classes>py test.py
Enter 2 float numbers: 10.5,20.6,20.1
The Sum is 53.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 keyboard on the display.

l = eval(input(“Enter List”))
print (type(i))
print(i)



No comments:

Post a Comment