Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
Responsive image

Python - Variables and Data types

What am I Learning today?

I am learning how to explain what a variable is and how we use them
I am learning how to identify different data types

Knowledge Organiser

×

Lesson

Task 1 - Getting Organised - PowerPoint Click to see more

  1. Set your Learning Objectives to red.

  2. Open your PowerPoint from last lesson.

If you need to download a copy of thePowerPoint Click here and open Task 1.



Task 2 - Understanding identifiers - Python Click to see more

Variables

We use an identifier to name a variable. A variableis used to store a data value that can change.

Question

What does it mean to identify something?

Constants

A Constant is also a used to store a value, but it's value cannot change


Whats in a name?

  • When we are naming(identifying) a variable or a constant there are certain rules or conventions we are meant to follow. Here are some of the rules or naming conventions for naming variables or Constants.
    1. 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

    2. Constant names should be in UPPERCASE letters with words separated by underscores (_):

      MY_NAME = "Bob" is good
      but MY NAME = "Betty" is not good.

    3. 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.

    4. 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.


    Activity 1

    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.



    Task 3 - Creating variables Click to see more

    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

  • Here is the first one for you:
  • name = "Jenny"
    print("The user name of blue fox is" , name)
    1. Copy the code above into your python window.

    2. Save the code name as variables.

    3. Run the code.

    You should see this



    Activity 2

    1. Create the rest of the variables and their values from Task 2 in python
    2. Create an output each one in a suitable sentance using the name of the variable like I did in my example.
    3. Run your code and then screenshot it into your PowerPoint.

    Task 4 - Data types in Python Click to see more

    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.

    1. string ('str'):- this is used to store multiple alphanumeric characters or words like "Hello" or "abc123"
    2. integer ('int'):- this is used to store whole numbers like 72 or 311
    3. float ('float):- this is used to store floating point numbers like 3.14 or 1.618
    4. boolean ('bool'):- this is used to store the valuesTrue or False a boolean can only store those two values.

    You can find out the data type of a variable by using the type() function.



    Activity 3

    1. Find out the type of the rest of the variables by using the type() function like i did in the example above

    Explain your answers in your Powerpoint next to a screenshot of your code



  • When you are finished don't forget to save your your powerpoint

  • Task 5 - Casting data types Click to see more

  • Here are the lines of code we printed out last week using numbers.

  • 1 print("8+3")
    2 print(8+3)
    3 print("8" + "3")


  • You should have noticed that they all printed out different things:

    Line 2 printed out 11 because our numbers were not inside speechmarks so were treated as integers.

    InLine 3 our numbers were inside speechmarks so were treated as "chars" and when we add two characters toghether or two strings we simply join or concatenate them. So "8" + "3" = "83"

    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:

  • Casting (changing data types)

    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.

    1. Copy the following code into python:
      first = "21"
      second = "33"

      Add the two variables together and then print out the result as a whole number
    2. Copy the following code into python:
      PI = "3.14"
      radius = "7"

      Work out the circumferance of the circle, the formula is:
      2 * PI * radius
      the * is the multiplication symbol in Python, you can find it below the number 8 then print out the result as a number.

      Don't forget to cast your variables to the correct data type.

    Explain your answers in your Powerpoint next to a screenshot of your code



  • When you are finished don't forget to save your your powerpoint

  • Task 6 - Lesson review Click to see more

    Review task:

    Tick each task when you have successfully done it in the following list:


    Declare three variables each of a different data type.
    Show what the data types are using the type() function.
    Try and add 3 variables together and print out the result.


    Task 7 - Update Objectives Click to see more

    Summing it all up

    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

    Learning Outcomes I need to learn how to explain what a variable is and how we use them

    • I have a basic understanding of how I can explain what a variable is and how we use them with a little help from my teacher
    • I can show my teacher that I can explain what a variable is and how we use them without their help.
    • I can explain what a variable is and how we use them independently and I can also explain it to others and can complete any extension tasks I am given.

    🠜 Now update your learning objectivesClick on the Assessment image

    My Notes: coding-p

    Student_Comment_4 not found

    Task Notes/Comments - Add here Click to see more

    Comments/Notes

    Copyright © 2013 - 2024 LearnITwithMrC