logo codesdope

Taking Input in Python


Now, you know how to print something on a screen. So, let's learn how to take input from the user.

  • Python 2
  • Python 3
name = raw_input("What is your name >>>")
name = input("What is your name >>>")
Output
What is your name >>>xyz
  • Python 2
  • Python 3
print "Enter your name"
x = raw_input()
y = raw_input("age >>>") #age>>> will be printed before input
print "Your name is",x,"and","and your age is",y
print("Enter your name")
x = input()
y = input("age >>>") #age>>> will be printed before input
print("Your name is",x,"and","and your age is",y)
Output
Enter your name
xyz
age >>>20
Your name is xyz and and your age is 20

'input()' is used to take input from the user. We can also write something inside input() to make it appear before the input, as done in the previous example, input("age >>>").

Let's see some examples.

  • Python 2
  • Python 3
print "Enter your wish"
wish = raw_input()
print "May your wish come true!"
print("Enter your wish")
wish = input()
print("May your wish come true!")
Output
Enter your wish
I want to be greatest coder ever
May your wish come true!

Anything given to input is returned as a string. So, if we give an integer like 5, we will get a string i.e. '5' (a string) and not 5 (int).

Now, let's learn to take integer input from the user.

  • Python 2
  • Python 3
x = input("Enter an integer >>>")
print "You have entered",x
x = int(input("Enter an integer >>>"))
print ("You have entered",x)
Output
Enter an integer >>>12
You have entered 12
Enter an integer >>>12
You have entered 12

int changes a string to an integer. For example, int('12') will give us an integer 12.

In the above example, we are taking the input from the user as a string with the input() and then we are using the int() to change it to an integer. So, if a user will enter 10, then the input() will become '10' (a string) and the above statement int(input()) will become int('10') and we will get 10 as an integer.

We can also break the above example in one more step to understand better.

x = input("Enter an integer >>>")
y = int(x)
print("You have entered",y)
Output
Enter an integer >>>12
You have entered 12

Here, we passed 12. Thus, x became '12' and then int(x) turned it into an integer.

The 'raw_input()' of Python 2 is same as 'input()' of Python 3 and the 'input()' in Python 2 is used to directly take integer inputs from a user.

What does input() do?


  • Python 2
  • Python 3
x = raw_input(">>>")
print x
x = input(">>>")
print(x)
Output
>>>10
10

This code is similar to x = '10'.
We gave 10 to the input(). You can think that input() becomes 10. So, x = input() will be similar to x = 10 after the input is given.

How to take float (decimals)?


Similar to taking input of integers, we can use float() to take an input from the user and change it to a float.

  • Python 2
  • Python 3
x = float(raw_input())
print x
print type(x)
x = float(input())
print(x)
print(type(x))
Output
12.32
12.32
<class 'float'>

It is similar to taking input of integers. We entered 12.32 to input() which will return '12.32' (a string). Thus, the expression float(input()) will become float('12.32'). And this will give us a float 12.32.

Now, let's try this.

  • Python 2
  • Python 3
print int(7.4)
print(int(7.4))
Output
7

This is type conversion. 7.4 is a float (decimal). So by writing int(7.4), we are converting it into an integer and thus, it got converted to 7.

This is very useful in some situations, where you want to convert a variable in the middle of your program.

So, in the initial examples of this chapter, we used int() to convert strings (the inputs we were taking were strings) into integers and then converted floats into integers. Let's proceed ahead and learn some mathematical functions available in Python.

Let your computer do some maths for you.


  • Python 2
  • Python 3
import math
print math.sin(30)
print math.cos(10)
print math.pow(2,3)
import math
print(math.sin(30))
print(math.cos(10))
print(math.pow(2,3))
Output
-0.9880316240928618
-0.8390715290764524
8.0

import math → This will include Python's inbuilt directory 'math'. It contains many mathematical functions for our use. import is a keyword used to import any available directory.
math.sin() computes sine of a given angle.
math.cos() computes cosine of a given angle.
math.pow(a,b) computes a raised to the power of b (ab).

We have imported 'math' and these functions are inside it, so 'math.sin()' can be understood as 'sin()' from 'math' directory.

Checkout python's official documentation for more functions available in math.

Knowledge is of no values unless you put it into practice.
- Anton Chekhov


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.