Variables and Data types
Create your own Program
Set your Learning Objectives to red.
Open your PowerPoint from last lesson.
If you need to download a copy of thePowerPoint Click here and open Task 1.
We use an identifier to name a variable. A variableis used to store a data value that can change.
A Constant is also a used to store a value, but it's value cannot change
Variable names should be in lowercase letters with words separated by underscores (_):
my_name = "Bob" is good but My Name = "Betty" is not good.In python, you’ll get an error message if you try to make a variable name with a space in it
Constant names should be in UPPERCASE letters with words separated by underscores (_):
MY_NAME = "Bob" is good but MY NAME = "Betty" is not good.
Both variable names and constant names should describe the data that they store.
score = 0 is good but x = 0 is not good as it tells you nothing about the data.
Both variable names and constant names should not start with a number.
player1 = "Joe" is good but 1player = "Joe" will give you an error message.
Fill in the gaps in the boxes (click on the image to open the powerpoint slide) then add the completed powerpoint slide into your PowerPoint.
In python we assign data values to variables to store them in our program.
Let's use the examples from our previous task and create them in python
Copy the code above into your python window.
Save the code name as variables.
Run the code.
You should see this
Every variable in Python has a datatype. Data types help python decide how much memory it needs to store the data values.
There are four main data types used in Python.
You can find out the data type of a variable by using the type() function.
Explain your answers in your Powerpoint next to a screenshot of your code
1 print("8+3") 2 print(8+3) 3 print("8" + "3")
Line 2 printed out 11 because our numbers were not inside speechmarks so were treated as integers.
To solve the problem we could simply remove the speech marks. However in code we try to make sure all of our data is stored inside of variablesso our code might look like this:
There is a solution to this problem, just like we can use the function type() to find out the data type of a variable we can use another function which uses the first three letters of the data type e.g. int() to turn our string into another data type in this case an integer let's try it:
Success! you can see that by putting our variables inside the brackets of our int() function we turned them back into numbers, cool right?
Now it's your turn.
Don't forget to cast your variables to the correct data type.
Tick each task when you have successfully done it in the following list:
Lets look at the learning outcomes and decide which one best describes our current level of understanding :
Tick the one you feel is closest to your level