Variables and Assignments

Essential Knowledge:

  • A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
  • Using meaningful variable names helps with readability of program code and understanding of what values are represented by the variables.
  • Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.
  • Some values are better suited to representation using one type of datum rather than another.

What is a variable?

  • An abstraction inside a program that can hold a value

An example naming variables

  • You want to store the highest score you scored in a game. So, you want to call this variable highScore
  • You want to store a name, so you can call the variable firstName
  • It is also possible to store true or false in a variable. For example, the variable could be called isSunny, where true and false are stored in the variable.
  • phoneNumber could be a variable that stores phone numbers.
  • These examples are short and convenient as opposed to a long description
  • However, they give insight to the basic goal of the variable.
  • Spaces also cause a problem because it is not allowed
  • Dashes and numbers are also less ideal.

Data Type examples

  • highScore --> an integer
    • probably doing addition or subtraction, save it as an integer or numbers.
  • firstName --> text(or string)
    • since a name is text, it should be saved as a string
  • isSunny --> boolean
    • only gives two options: true or false within variable
  • phoneNumber --> text(string)
    • no math involved just a string of numbers

Practice

Question: What would be the best variable name and data type to store a user's age in a program?

  • Answer: name: age data type: integer

Q: What would be the best variable name and data type to store the number of students in your class?

  • A: name: numStudents data type: integer to add and subtract

Q: What would be the best variable name and data type to store the title of a movie?

  • A: name: movieTitle data type: text(or string)

Q: What would be the best variable name and data type to store if someone's pet is a dog.

  • A: name: isDog data type: boolean

Homework/Hacks

You want to store the number of apples in a shop. What is the best variable name and data type?

  1. numApples, integer
  2. apples, text
  3. numApples, string
  4. isApples, boolean
Click for the answer! 1. Because the name is descriptive of what is stored. Also, the number of apples in the store can change so the integer lets you add a nd subtract to the number of apples.

You are storing true or false in a variable that asks if the classroom is cold. What is the best variable name and data type?

  1. weather, integer
  2. weather, boolean
  3. isCold, boolean
  4. cold, string
Click for the answer! 3. Because this is a true or false question. It is either cold or not, so the variable name describes the circumstance, then the boolean data type specifies true or false.

How do you store the ID numbers for the students in the classroom? Choose the best variable name and data type:

  1. IDnumber, string
  2. whatID, integer
  3. IDnumberofthestudentsintheclassroom, boolean
  4. IDnumberofthestudentsintheclassroom, integer
Click for the answer! 1. IDnumber because it is descriptive but not too long and innefective. it is also a string because it doesn't change and its a string of numbers.

Is itisRainingtodayinsandiego a better option than isRaining?

  1. Yes
  2. No
Click for the answer! 2. No, because it is too long and inefficient to use when trying to store variables.

Which of the following types of data is best for a true or false question?

  1. Boolean
  2. String
  3. Float
  4. All of the above
Click for the answer! 1. A boolean data type allows a variable to store true and false.

What is the difference between an integer and string of numbers?

  1. An integer is just a set data type while a string of numbers can be changed with addition and subtraction
  2. An integer can be letters and numbers while a string is just numbers
  3. An integer is just numbers while a string is just words
  4. An integer can be changed with addition and subtraction and a string is a set number or string of letters.
Click for the answer! 4. This is the only answer that has two true statements

Now, make at least 3 of your own practice questions making a scenario and then adding the variable name and data type. Add this to you blog!