input() function in Python 3

input() function in Python

We can use input() function in Python to read some value entered by the user at Python shell.

By default input() function is used to reads a string value from keyboard.

Cursor  will not proceed to next line until you press enter key after typing the value

The Syntax of input() function is:

 input(argument)

Here argument is optional.

It is the string message you want to display while reading the value. It generally contains a suitable message specifying the purpose of input() function.

1. Reading string values using input() function

We can use input() function to read string values as follows:

a. Reading a string value using input() function without argument

>>> s=input()

hello Python

>>> print(s)

hello Python

 

In the above statements I have use the input() function as:

s=input()

Above statement will allow you to type any value which will be stored in variable s as a string value.

print(s) will display value of string variable s i.e. hello Python as output

b. Reading a string value using input() function with argument

>>> name=input(“Enter your name:”)

Enter your name:Lovejot

>>> print(name)

Lovejot

In the above statements I have used input() function as :

>>> name=input(“Enter your name”)

Above statement will display a message Enter your name on the Python shell.

It allows you to type any value which will be stored in variable name as a string value.

print(name) will display value of string variable name i.e. Lovejot as output.

2. Reading  integer values using input function()

We can also use input() function to read numeric values in Python

To read an integer value, we need to use data conversion function int() along with input() function as:

>>> n=int(input(“Enter an integer value:”))

Enter an integer value:10

>>> print(n)

10

In the above statements I have used input() function as :

>>> n=int(input(“Enter an integer value:”))

Above statement will display a message Enter an integer value: at Python shell. It allows you to type integer value which will be stored in variable n as an integer value.

If we don’t type integer value, it will generate error.

Example:

>>> n=int(input(“Enter an integer value:”))

Enter an integer value:2.5

Traceback (most recent call last):
File “<pyshell#6>”, line 1, in <module>
n=int(input(“Enter an integer value:”))
ValueError: invalid literal for int() with base 10: ‘2.5’

When we try to enter a real value, it will give error because input() function is basically meant to read a string value.

On typing 2.5 , it will consider 2.5 a string value which can’t be converted to integer value.

3. Reading a real value using input() function

To read a real value, we need to use data conversion function float() along with input() function as:

Example 1:

>>> n=float(input(“Enter a real value:”))

Enter a real value:2.5

>>> print(n)

2.5

In the above statements I have used input() function as :

>>> n=float(input(“Enter a real value:”))

Above statement will display a message Enter a real value: at Python shell. It allows you to type floating point value which will be stored in variable n.

We can also input real values in scientific form as:

Example 2:

>>> n=float(input(“Enter a real value in scientific form:”))

Enter a real value in scientific form:2.5e3

>>> print(n)

2500

In the above example, value has been input in real form as 2.5e3

The output is 2500 because 2.5 is multiplied with 103
If we don’t type numeric value, it will generate error .

We need to use float() function because input() function basically reads string value which needs to be converted to floating point value.

4. Reading  a Boolean value using input() function

We can read Boolean value by enclosing input() function within the parenthesis of data type conversion function bool() as follows:

>>> choice=bool(input(“Enter True or False:”))
Enter True or False:True>>> print(choice)True

In the above example, value has been input in boolean form as True which will be stored in boolean variable  choice. which is printed using print(choice) command.

We need to use bool() function because input() function basically reads string value which needs to be converted to Boolean value.

Python supports two Boolean values True and False where True represents 1 and False represents 0.

5. Reading  a complex value using input() function

We can read a complex value by enclosing input() function within the parenthesis of data type conversion function complex() as follows:

>>> c=complex(input(“Enter  a complex value:”))
Enter a complex value:2+3j>>> print(c)(2+3j)

In the above example, complex value 2+3j has been input in variable c. which is printed using print(c) statement. Complex value 2+3j is printed within the pair of parenthesis

We need to use complex() function because input() function basically reads string value which needs to be converted to complex value.

In Python complex values are of the form A+Bj.

Here A and B are real values. A is termed as real part and B is termed as imaginary part.

j represents square root of -1. Which is an imaginary number.

6. Reading  a list using input() function

We can read a list by using input() function along with split() function of Python.

>>> list1=input(‘enter a list:’).split()

enter a list:3 4 5 6

>>> print(list1)

[‘3’, ‘4’, ‘5’, ‘6’]

In the above example, input() function has been used to read list of values as:

>>> list1=input(‘enter a list:’).split()

Values are entered as 3 4 5 6 (Separated by blank spaces). It is actually a string value.

split() function converts the above value into a list on the basis of spaces in between the values. So a list will be formed as [‘3’, ‘4’, ‘5’, ‘6’] which will be stored in variable list1.

which is printed using print(list1) statement.

7. Reading  a tuple using input() function

We can read a tuple by enclosing input() function within the pair of parenthesis of  eval() function of Python. But it can’t accept string values.

>>> t1=eval(input(‘enter a tuple:’))

enter a tuple:2,2.5,True,2+3j

>>> print(t1)

(2, 2.5, True, (2+3j))

In the above example, input() function has been used to read tuple of values as:

>>> t1=eval(input(‘enter a tuple:’))

Values are entered as 2,2.5,True,2+3j (Separated by commas). It is actually a string value.

split() function converts the above values into a tuple on the basis of commas in between the values. So a tuple will be formed as (2, 2.5, True, (2+3j)) which will be stored in variable t1

which is printed using print(t1) statement.

8. Reading  a dictionary using input() function

We can create a dictionary by joining two lists one for creating keys and other for creating values. Following code should be written in script mode to create a dictionary after reading two lists.

keys=input(‘enter a list of keys’).split(‘,’)
values=input(‘enter a list of values’).split(‘,’)d = {}
for k in keys:
     for v in values:
          d[k] = v
          values.remove(v)
          break print (“Dictionary : ” ,d)

Output:

enter a list of keys1,2,3,4
enter a list of values a,b,c,d
Dictionary : {‘1’: ‘a’, ‘2’: ‘b’, ‘3’: ‘c’, ‘4’: ‘d’}

In the above example, input() function has been used to read two lists namely keys and values as:

keys=input(‘enter a list of keys’).split(‘,’)

values=input(‘enter a list of values’).split(‘,’)

Values in these two lists are input as follows

enter a list of keys1,2,3,4
enter a list of values a,b,c,d

Values are separated by commas because comma(,) has been used inside split() function.

Following code has been used to create dictionary out of above lists:

d = {}

for k in keys:

     for v in values:

          d[k] = v

          values.remove(v)

          break

print (“Dictionary : ” ,d) statement will display values inside the dictionary.

Dictionary : {‘1’: ‘a’, ‘2’: ‘b’, ‘3’: ‘c’, ‘4’: ‘d’}

Leave a Reply

Your email address will not be published. Required fields are marked *