# Python Input

Now, you know how to print something on the screen and not just that, you also know about the different data types available in Python and how to use them. Let’s move forward with this chapter and learn a new Python concept.

Till now, we have assigned values to variables in the code. However, there can be times when we want the user to enter the value which has to be assigned to a variable. Take an example of a calculator in which the user enters the values to be added or subtracted.

input() function is used to take input from the user. So, let’s see how to take values entered by the user.

name = input()

xyz

The statement in the above code will read the value entered by the user (xyz) and will assign it to the variable name.

We can also print a message on the screen while asking the user for input by passing that message to the input() function.

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


In this example, the message “What is your name >>>” will get printed on the screen and the value entered by the user after that will be assigned to the variable name. The message displayed on the screen while taking input is helpful to understand what type of input the user is expected to enter. Therefore, it is a good practice to display such messages while taking input.

Let’s look at some other examples.

print("Enter your name")
x = input()
y = input("age >>>") #age>>> will be printed before input

xyz
age >>>20
print("Enter your wish")
wish = input()

I want to be greatest coder ever

### What does input() do?

x = input(">>>")
print(x)

>>>10
10

If the user enters 10, then the first statement is equivalent to x = '10'.

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

### Taking String Input in Python

By default, any input entered by the user is of type string.

num = input()
print(type(num))

10
<class 'str'>

As you can see, the user entered a number 10 as input but its type is str. Therefore, any input entered by the user is always read as string.

### Taking Integer Input in Python

We just saw that input is always read as a string. So what if we want to read an integer?

We can do this by converting the string input to int using the int() function.

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

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() function and then we are using int() to change it to an integer. So, if a user enters 12, then input() will become '12' (a string) and the above statement int(input()) will become int('12') and we will get 12 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)

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.

### Taking Float (Decimals) Input in Python

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))

12.32
12.32
<class 'float'>

It is similar to taking input of integers. We entered 12.32 to input() which returned '12.32' (a string). Thus, the expression float(input()) became float('12.32'), and that gave us a float 12.32.

## Let your computer do some maths for you

import math
print(math.sin(30))
print(math.cos(10))
print(math.pow(2,3))

-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 (a^b).

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

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

To learn from simple videos, you can always look at our Python video course on CodesDope Pro. It has over 500 practice questions and over 20 projects.
In theory, there is no big difference between theory and practice. But in practice, there is.
- Yogi Berra

Recent Posts
asds
jasd
as