0% found this document useful (0 votes)
76 views4 pages

Inputs in Python

The document discusses how to take input from users in Python using the input() function. It shows examples of taking string, integer, and float inputs and converting them using int() and float() functions. It also demonstrates using the math module to access mathematical functions like sin(), cos(), and pow().

Uploaded by

Abhishek Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
76 views4 pages

Inputs in Python

The document discusses how to take input from users in Python using the input() function. It shows examples of taking string, integer, and float inputs and converting them using int() and float() functions. It also demonstrates using the math module to access mathematical functions like sin(), cos(), and pow().

Uploaded by

Abhishek Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

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.

name = input("What is your name >>>")

Output

What is your name >>>xyz

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.

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.


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

Output

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?

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.

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.

print(int(7.4))

Output

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.

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.

You might also like