Read values in Python 3 | input() function in Python

You must first complete Display output in Python 3 | print() function in Python 3 before viewing this Lesson

Read values in Python using input() function.

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:

Read and display a string value.
str1=input('Enter a string value=')
Enter a string value=Ladder Python

>>> str1
'Ladder Python'
Description

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
>>> num1=int(input('Enter an integer value'))
Enter an integer value10

>>> num1
10
Description
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
>>> num2=float(input('Enter a real value'))
Enter a real value2.5

>>> num2
2.5
Description
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
>>> num2=float(input('Enter real value in scientific form'))
Enter real value in scientific form2.54e5

>>> num2
254000.0
Description

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
>>> val=bool(input('Enter True of False='))
Enter True of False=True

>>> val
True
Description
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
>>> val1=complex(input('Enter a complex value='))
Enter a complex value=2.5+3.5j
>>> val1
(2.5+3.5j)
Description
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
>>> l1=input('enter a list')
enter a list[1,3,4,5]

>>> l1
[1, 3, 4, 5]
Description
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 a tuple
>>> t1=eval(input('enter a tuple'))
enter a tuple(1,3,4,'a')

>>> t1
(1, 3, 4, 'a')
Description
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
>>> d1=input('enter a dictionary')
enter a dictionary{1: 'a', 2: 'b'}

>>> d1
[1, 3, 4, 5]
Description
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
>>> d1=input('enter a set')
enter a set{3,4,2,3}

>>> s1
[1, 3, 4, 5]
Description
Value of set variable s1 have been input as {3,4,2,3}. Values need to be entered within pair of braces.



Spread the love
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
Spread the love