Data types and input function
Programs use input to do certain work, and in order to receive it from another user, we can use the input function.
The input() function always turns the user input into a string, no matter which type of data was originally put into it.
Example:
cost_of_living=input() #user may write down an answer print(type(cost_of_living)) #to specify the data type of the input user has given
answer: <class 'str'> = string
But! You can't do math with strings, so sometimes we need to convert existing data type of a value.
cost= input() #default data type is a string cost=int(cost) #converts into integer cost=float(cost) #converts into float cost=str(cost) #back to string
Side notes: 1. You cannot convert any type of value to an integer or the float, only numeric values!
cost= "one hundred" cost=int(cost) #would result into an error
while:
cost= "100" cost=int(cost) #would convert string into an integer, as desired
2. Another important thing to remember, is that when we are creating a value as an integer, we use quotes "".
But in order to convert a variable into a string, we have to use a function str().
apples = 2 print("apples") # prints "apples", not the string with value 2 apples = str(apples) print(type(apples)) #<class 'str'> print(apples) #2















