Understanding Variables Python Understanding Variables
Variables What is a variable? Definition A named area of storage in a computer program that can change as the program runs
Are all numbers the same? Content of Variables Words and letters These are known as strings Numbers Boolean Are all numbers the same?
Working with variables The type of information stored in a variable is referred to as its datatype The datatype controls how the program will process the data Eg Arithmetic can’t be performed on strings Numbers can only contain 0-9 and . – Special functions convert between strings and numbers – This is called Casting
Casting a Numeric Variable Numbers typed into Python arrive as a string They must be converted to a number before using in arithmetic number1 “29” 29 number1 number1 “29”
Variable Names A Variable name MUST start with a letter or an underscore NOT a number. Numbers can be included within the name, but not as the first character. A Variable name can be any length but is best to keep it reasonably short. Must consist of a continuous string of characters. That means they CANNOT have any spaces. Identify words by either capitalising the first letter of each word, or by using the underscore character. firstName first_name Only in exceptional circumstances use single letters for variable names Should be meaningful - describe the data they contain