Showing posts with label How to Read Data From KeyBoard. Show all posts
Showing posts with label How to Read Data From KeyBoard. Show all posts

Sunday, 12 March 2017

How to Read Data From KeyBoard

Reading data from the Keyboard:-

Read the data from the keyboard in any programming language  by default it takes string format only,
browser(string format),
command prompt(text format).
  • in c-language by using  scanf function.
  • in java language by using InputBufferReader, InputStream,Scanner.
  • in python2.7 by using input( ) and raw_input( ).
  • in python3.5 by using input( )

We can read the data from the keyboard in python2.x by using input( ) and raw_input( )

  • Python2.x ----> input( ), raw_input( )  for string data type.
  • in python2.x-----> type conversion is implicitly

If we use Python 2.7 version for taking input as String data type then we must use raw_input whereas for other data types we can use as below.

Example 1:
x=raw_input ('enter fname :')
print(x)
y=raw_input ('enter lname :')
print(y)
print(x+y)

Output:
========= RESTART: C:/Python27/keyboard.py =========
enter fname: siva
siva
enter lname: krishna
krishna
sivakrishna

Example 2:

x=input ('enter first integer :')
print(x)
y=input ('enter second integer :')
print(y)
print(x+y)

Output:
========= RESTART: C:/Python27/keyboard.py =========
enter first integer:3
3
enter second integer:6
6
9

 We can read the data from the keyboard in python3.x by using input( )


  • In Python3.x -----> input( )
  • In Python3.x -----> type conversion is explicitly

Example 1:
x=input('enter fname: ')
print(x)
y=input('enter lname: ')
print(y)
print(x+y)

Output:
enter fname: siva
siva

enter lname: krishna
krishna

sivakrishna

Example2:
x=input('enter first integer: ')
print(x)
y=input('enter second integer: ')
print(y)
print(x+y)

Output:
enter first integer: 4
4

enter second integer: 5
5

45
Note:
above example my requirement is addition of two numbers, but output will be show concatination. here we are going type conversion mechanism.

Example3:
x=int(input('enter first integer: '))
print(x)
y=int(input('enter second integer: '))
print(y)
print(x+y)

Output:
enter first integer: 4
4

enter second integer: 5
5

9

Example4:
x=input('enter first integer: ')
print(x)
i=int(x)
y=input('enter second integer: ')
print(y)
j=int(y)
print(x+y)

Output:
enter first integer: 4
4

enter second integer: 5
5


9