Data types store the value of a variable. The value stored as such indicates which type it belongs to.
Always remember one thing, the language we understand is not understood by computer.
Look at the image given above.
We understand Vijanthi tutorial as alphabet, but computer understands it as string.
We call 70 a whole number. But the computer says it is an integer.
We call 5.7 a decimal point number, but the computer calls it a floating point value.
String is abbreviated as str, integer is int and floating point value is taken by computer as float.
Don't think that there are only these 3 data types in Python. There are many data types.
Let's see how to get input from user.
User is the person who uses the computer.
The input() function is used to get an input from the user.
name=input("Enter your name: ")
age=input("Enter your age: ")
height=input("Enter your height: ")
print(name)
print(age)
print(height)
Description:-
Name, Age, height are all three variables.
Whatever we type inside the input() function by putting double quotes inside it will be visible on the output screen.
I have given Enter your name inside the input function in the Name variable. So the output screen says Enter your name and next to it the cursor is blinking. The reason why the cursor is blinking means that the user has to give the appropriate value to the input. After the user gives the value, that value is saved in the variable called name. This is the same with the two variables, age, height.
Finally we display all the values stored in the variable using the print function.
Let's see how we can find the datatype of the value we have.
The datatype of the value can be found using the type( ) function.
name=input("Enter your name: ")
age=input("Enter your age: ")
height=input("Enter your height: ")
print(name)
print(age)
print(height)
print(type(name))
print(type(age))
print(type(height))
''''
output:
Enter your name: vijanthi tutorial
Enter your age: 70
Enter your height: 5.7
Vijanthi tutorial
70
5.7
<class 'str'>
<class 'str'>
<class 'str'>
''''
Description:-
The variable name is given inside the first type function.
When the program is run, the value vijanthi tutorial is saved in the variable name inside the function type.
Vijanthi tutorial value depends on a string datatype. So the type function shows str on the output screen.
The next type(age) line is executed. But it displays str on the output screen. The value stored in the age variable is 70. We have read that the computer understands the number 70 as an integer data type. But it shows us that str.
The reason is that in python all the values that we get using the input function are taken as string data type.
Similarly, when the line type(height) is executed, the output shows str. A decimal value of 5.7 is stored in the height variable. In case of decimal value, the computer takes it as floating data type, but we take it as string data type.
The reason is that the value is retrieved using the input() function.
Data type Conversions:-
Data type conversion is converting from one data type to another data type.
age=int(input("Enter your age: "))
height=float(input("Enter your height: "))
print(age)
print(height)
print(type(age))
print(type(height))
''''
Output:
Enter your age: 70
Enter your height: 5.7
70
5.7
<class 'int'>
<class 'float'>
''''
Description:-
First, memory is created for the variable age. Next, the value 70 is obtained using the input() function. The value obtained will be string data type. We convert it to integer data type using int() function. The changed value is stored in the variable age.
Memory is created for the next variable called height. A value of 5.7 is given to this variable. Since the given value is string data type, it should be converted to floating data type. We use float() function for that. Then we store that value in a variable called height.
Finally when giving type() and type(height) age The data type of int for e variable and float for height variable is displayed on the output screen.
This is how one data type can be converted to another data type.