Taking input in python — digipodium

zaid
3 min readAug 28, 2019

--

Welcome to my first article, where i use my naive grammer skills to inform you about the different ways to take simple input in Python. Yes i will be talking about only python3 ( version > 3.5 acutally) here. Now, without wasting your time, let us begin with baby steps.

Option 1.1 : taking input as string

name = input('hello, whats your name:')

Well in python 3 when we use the input function. we pass an argument, that will become your message aka prompt for the user to understand, what he/she need to type for the input. And after the user types and presses enter, the data is taken as a string and stored in the variable on the left hand side (obviously).

Option 1.2 : taking input as int or float

x = int(input('what is your favorite number, son :'))
rainfall = float(input('rainfall, in cm =')

This, here is the shortcut way of taking a numerical input from user. Here the programmer is totally putting a trust in the user to enter a numerical value which is actually taken as string, and then the int or float function tries to converts (typecasts) it, hopefully. It may work, but the correct version should look like this,

x = input('what is your favorite number, son :')
if not x.isnumeric():
print("Its not a number son, dont mess with me')
else:
x = int(x)

here is the float version, which is totally different

rainfall = input('rainfall, in cm= ')
if not rainfall.count('.') == 1:
print('wrong, give me accurate answers')
else:
rainfall = float(rainfall)

Now lets upgrade

Sometimes, we want to take multiple values through single input, like this

Option 2.1 : taking input as a list

fruits = input('enter fruit names : ').split() 
we are taking input by giving fruit names divided by single space. Easy

This was supereasy and timesaving. You could have also taken a list and then added items throug a for loop. but we can achieve that with a small code of list comprehension

Option 2.2 : taking input using list comprehension

print('write the names of 5 avengers heroes')
avengers = [input(f'{i} => ') for i in range(1,6)]

The above program creates a loop for 1 to 5 values and then at each turn takes input. this input is then stored as an item to the avengers list. I also used my favourite new f string operator, which can print variable value within a string

Did you get that. this is called list comprehension

Option 2.3 : the beautiful map function based input

Nowadays, many competetive exams has questions that want a sequence to be taken as input, and here is where python’s beautiful map function or comprehension comes to play. lets me show with an example (complex one).

print('enter values seperated with commas')
values = list(map(int,input('>>>').split(',')))
and its done

Heres hoping that you found this article helpful for your coding endeavour.

You can learn this and much more in our Python data science course, which is your window to explore python as well as data science stuff like pandas, visualization and more, only at digipodium

--

--

zaid
zaid

Written by zaid

Coder, android developer, python programmer, data science evangelist, Maching learning trainer, and raspberry pi lover & works at digipodium, india

No responses yet

Write a response