Strings are used to store a mixture of alphanumerical characters, or other characters, such as . , @:() etc.
Data that might be stored as a string include:
- Telephone numbers (because they don’t just contain numbers…)
- Sentences / paragraphs
Most input functions store inputs as text by default, so if they need to stored in an alternative data type they need to be cast or reassigned.
5 200 -75 73000000000
Integers are positive or negative whole numbers
Data the might be stored in integers include:
- Weight( in whole kgs)
- Currency amounts (e.g. pence)
Floats / Real
4.234543 -41.00 43.719
Floats (or Reals) are positive or negative numbers that include a decimal place.
They can be used to store values where a decimal place is needed.
- Currency in pounds E.g. 4.25
- Precise measurements in meters 4.25m
- Temperatures with decimal places
Booleans are used to store True / False values
Any data where a True or False value is appropriate:
- Is a member of a club?
- Is the person coming?
A char is used where there are a limited number of possibilities, but too many to use a Boolean. Chars use up less space than strings because only one value is stored.
- Infant, Child or Adult can be stored under I/C/A
- Gender types male,female,other M/F/O
print(“You are ” + str(age) + ” years old”)
Often a variable is stored as one type but is sometimes needed to be represented temporarily as another type. In this instance variable casting needs to be used. The blue text shows the casting tasking place.
Here an integer has been stored, but needs to be concatenated with 2 strings. It needs to be cast first price = 5.23 print("The total cost of you shopping is £" + str(price) + ". Thankyou")