Presentation is loading. Please wait.

Presentation is loading. Please wait.

OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.

Similar presentations


Presentation on theme: "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants."— Presentation transcript:

1 OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants

2 OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 6: Variables and constants Variables are essential in any program so that we can process data. Variables have names. The data in variables can be changed during the running of a program. There are lots of different types of variables. The computer needs to know what type a variable is so that it can process it in the way that we require.

3 OCR Computing GCSE © Hodder Education 2013 Slide 3 Python 6: Variables and constants Python has five basic data types. However, there are sub-types within each group. Numbers String List Tuple Dictionary For example, numbers can be integers, floating point or long integers.

4 OCR Computing GCSE © Hodder Education 2013 Slide 4 Python 6: Variables and constants You can assign a variable data type by initialising it. In other words, you hand it the sort of value that you want it to have. counter is an integer (a whole number) temperature is a floating point number (it has a decimal point) name is a string (notice the quote signs)

5 OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 6: Variables and constants Python lets you change the data type of a variable during a program. This is permitted:

6 OCR Computing GCSE © Hodder Education 2013 Slide 6 Python 6: Variables and constants A numeral such as 7 can be an string or an integer. ‘7’ is not the same as 7. You can’t do arithmetic on ‘7’. If you try something like this: Python obligingly gives you three copies of the string ‘7’:

7 OCR Computing GCSE © Hodder Education 2013 Slide 7 Python 6: Variables and constants We can use the int() function to convert a numeral in a string to a proper number that we can do arithmetic with. Then we get:

8 OCR Computing GCSE © Hodder Education 2013 Slide 8 Python 6: Variables and constants Here is a longer example: This gives us:

9 OCR Computing GCSE © Hodder Education 2013 Slide 9 Python 6: Variables and constants What we need is: Which gives us:

10 OCR Computing GCSE © Hodder Education 2013 Slide 10 Python 6: Variables and constants Constants Constants are items of data that do not change during the running of a program. Most languages allow the programmer to define constants such as pi or VAT rate. Python does not support this and if you really need to protect a value from change, then you would have to write a function to handle that. The simple thing to do is to set up a variable and then not change it!


Download ppt "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants."

Similar presentations


Ads by Google