campusx-official / dsmp-2022-23

All the codes and tasks taught in the CampusX Data Science Mentorship Program 2022-23
7 stars 1 forks source link

Type Conversion while taking input #1

Open Sayan-Roy-729 opened 1 year ago

Sayan-Roy-729 commented 1 year ago

When you put 'int' in first when taking input from the user, the type of fnum and snum is permanently changed. But you said type conversation couldn't change data type permanently.

Sayan-Roy-729 commented 1 year ago

Let's see the code first.

fnum = input("Enter first number")
snum = int(input("Enter second number"))
var1 = int(fnum)

print(type(fnum))
print(type(snum))
print(type(var1))

In the first line, you are taking the user input and storing it in the fnum variable. But in the second line, the user input is not stored directly in the snum variable, first, the input is converted to an integer, and then that integer value is stored in the snum variable. So in the snum variable, we are not storing the actual input value.

While you are doing type conversion, rather than changing the dtype of literals, Python internally creates a new object with the desired dtype object. For better understanding, run the code.