Read values in Python 3 | input function in Python

Preview
You must first complete Data types in Python before viewing this Lesson

input() function in Python

We can use input() function in Python to read some value at python shell. This function is basically used to read string value.

The Syntax of input() function is:

varname=input()

Here varname is the name of variable that we want to read.

1. Reading string values using input()

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

Reading a string value

Description

str1=input(‘Enter a string value=’)

Enter a string value=Ladder Python
>>> str1
‘Ladder Python’

Value of string variable str1 has been input  as Ladder Python.

It is shown as ‘Ladder Python’ in output. Output is shown within pair of single quotes

2. Reading numeric values using input function()

We can also use input() function in Python shell to read numeric values as follows:

a. Reading integer value

We need to use int() function along with input() function to read integer values

Reading integer value

Description

>>> num1=int(input(‘Enter an integer value’))

Enter an integer value10
>>> num1
10

Value of integer variable num1  has been input  as 10.
It is shown as 10 in output.

b. Reading floating point value

We need to use float() function along with input() function to read floating point  values.

Example 1:

Reading floating point value

Description

>>> num2=float(input(‘Enter a real value’))

Enter a real value2.5
>>> num2
2.5

Value of floating point variable num2  has been input  as 2.5.
It is shown as 2.5 in output.

Example 2:

Reading floating point value in scientific form

Description

>>> num2=float(input(‘Enter real value in scientific form’))

Enter real value in scientific form2.54e5

>>> num2

254000.0

Value of floating point variable num2  has been input  as 2.54e5.

It is shown as 254000 in output. It is because 2.54 is multiple by 105 i.e. 100000

3. Reading Boolean values using input function()

We need to use bool() function along with input() function to read a boolean value at Python shell.

Reading boolean value

Description

>>> val=bool(input(‘Enter True of False=’))

Enter True of False=True
>>> val
True

Value of variable val has been input  as True.
It is shown as True in output.

4. Reading Complex numbers values using input() function

We need to use complex() function along with input() function to read a complex value at Python shell.

Reading complex value

Description

>>> val1=complex(input(‘Enter a complex value=’))
Enter a complex value=2.5+3.5j
>>> val1
(2.5+3.5j)
Value of variable val1 has been input  as 2.5+3.5j.
It is shown as (2.5+3.5j) in output. Value is shown within pair of parenthesis.

3. Reading sequences using input function()

We can use input() along with eval() function at Python shell to read sequences as follows:

a. Reading a List

Reading elements of a List

Description

>>> l1=eval(input(‘enter a list’))
enter a list[1,3,4,5]
>>> l1
[1, 3, 4, 5]
Value of list variable l1 have been input  as [1,3,4,5].
Values need to be entered within pair of square brackets.

b. Reading a Tuple

Reading elements of tuple

Description

>>> t1=eval(input(‘enter a tuple’))
enter a tuple(1,3,4,’a’)
>>> t1
(1, 3, 4, ‘a’)
Value of tuple variable t1 have been input as (1,3,4,’a’).
Values need to be entered within pair of parenthesis.

c. Reading a Dictionary

Reading elements of a dictionary

Description

>>> d1=eval(input(‘enter a dictionary’))
enter a dictionary{1: ‘a’, 2: ‘b’}
>>> d1
[1, 3, 4, 5]
Value of dictionary variable d1 have been input as {1: ‘a’, 2: ‘b’}.
Values need to be entered within pair of braces as key value pairs.

d. Reading a Set

Reading elements of a Set

Description

>>> s1=eval(input(‘enter a set’))
enter a set{3,4,2,3}
>>> s1
[1, 3, 4, 5]
Value of set variable s1 have been input as {3,4,2,3}.
Values need to be entered within pair of braces.
Lesson tags: input value in pytho, read complex values in python, read integers in python, read real values in python, read values in python, readl boolean value in python, readl string in python
Back to: Python Programming Tutorial