Words and numbers are different.
When you add numbers together you get a total. eg 3 + 5 gives you 8
When you add words together you get a longer word. eg Hot + dog gives you Hotdog
Python is flexible and will change the data type of a variable to match the data stored in it, many other languages don't do this.
The three most commonly used data types are:
int Integer (Whole numbers) examples: 15, 0, 3, -27, 892341
float Floating Point Decimals examples: 2.7, 3.14, -23.0983
str String (Text) examples: Whakatane, #ABC, three,
Anything that comes from the keyboard (from an input() command) is text, even if it is made of digits.
This can lead to problems if we want to use the input as a number, suppose we are selling apples for $3 each
number_of_apples = input('How many apples would you like ? ')
cost = 3 * number_of_apples
print("That will be $", cost)
If you enter 5 in the above program it will try to charge you $555 !!
This is because the program treats number_of_apples as text and so '3 * 5' is treated as '3 lots of the character 5' rather than '3 times 5'.
If you typed in 'dog' the program would charge you 'dogdogdog' which would make sense.
To turn the input into an integer we need to put it inside the int( ) function, like this:
number_of_apples = int(input('How many apples would you like ? '))
The data comes in from the keyboard as a string and is then converted into an integer which is stored as number_of_apples.
This can cause other problems if the user enters data that can't be turned into an integer such as a decimal or some text we would get a different error. We will look at how to solve this issue later using try/except. For the time being expect the user to only enter appropriate data.
Similarly, we can turn a variable into a decimal by using float( )
height = float(input('How tall are you in metres ? '))
or we can force numbers to act like strings using str( )
payment = input('How would you like to pay for your ' + str(number_of_apples) + 'apples ? ')
We don't often need to do this. The only time you are likely to need to do this is in cases like the one above. An input can only have one piece of text inside its brackets, so you need to concatenate the three bits of text into one by adding them together, (we call this 'concatenating', pronounced con-cat-en-ate-ing). We can only concatenate strings, if you use + between a string and a number the program won't know what to do.
Note: A print command can take lots of different bits so
print("How would you like to pay for your", number_of apples, "apples ? ")
would be fine, and we don't need to turn number_of_apples into string.
You can find out what type of data is being held in a variable by using the keyword type( ) like this
print(type(number_of_apples))
In the payment example above number_of_apples is still of type int, the str function provides a string version of number_of_apples for that instance, allowing the program to create the one piece of text for the input. It doesn't change the original number_of_apples variable to string, you can keep using it as a number for calculations later in the program.
If we wanted a program to work out the total cost if you told it the price of an item and how many you wanted. The price will need to be a float so we can input a decimal and the number of items will be an int because you can't buy half an item!
price = float(input("How much is the item ? "))
quantity = int(input("How many are you buying ? "))
cost = price * quantity
print("That will be $", cost)
Note: cost will be a float in this cast because we are multiplying a float by an integer so we get a float as the answer. If we were multiplying an integer by an integer we would get an integer.