Jump to content

Fundamentals of Programming: Variables

From Wikibooks, open books for an open world

PAPER 1 - ⇑ Fundamentals of programming ⇑

← Intro to programming Variables Data types →


Let's take a look at this program:

VB.NET Python
        Dim name As String
        Dim age As Integer
        name = "Peter"
        age = 29
        Console.WriteLine("Hello " & name & " you are " & age & " years old")
        Console.WriteLine("This also means you are " & age * 12 & " months old")
        Console.WriteLine("Bye " & name & "!")
        Console.ReadLine()
        name = "Peter"
        age = 29
        print("Hello " + name + " you are " + str(age) + " years old")
        print("This also means you are " + str(age * 12) + " months old")
        print("Bye " + name + "!")

you might be expecting both programs to print out:

Hello name you are age years old

But instead they say:

   Code Output

Hello Peter you are 29 years old
This also means you are 348 months old
Bye Peter!

What friendly programs!

Let's break the VB.NET program down line by line:

  1. dim is a variable declaration, creating a temporary data store, a variable, and calling it name It also makes sure that whatever goes into name will be a string by setting it to as string
  2. We declare another variable called age and make sure it is stored as an integer (a whole number)
  3. The variable name that we created earlier is now assigned a value and as it's a string we better use speech marks - "Peter"
  4. The variable age that we created earlier is now assigned a value and as it's an integer we better not use speech marks - 29
  5. This line writes things to the screen, starting with the text "Hello " which attaches that variable we saw earlier to, but instead of putting the variable name, it puts the contents of the variable ("Hello Peter"), then it attaches some more text ("Hello Peter you are ") and adds another variable, age. Even though age is an integer we can stick it together with a string ("Hello Peter you are 29"). Then finally it uses the ampersand once more to attach the final piece of text ("Hello Peter you are 29 years old)
  6. This line works in pretty much the same way, but it does a calculation, working out the age in months. Computers are like giant calculators you can use to do all the things your little pocket calc can do (and far far more!)
  7. The great thing about variables is that we can use them again and again. Here we say "Bye " and use an ampersand to stick on the name of the person. This is great, by using a variable we only need to write "Peter" once and save it as name. If someone else came along and wanted to change the program they just need to change the value of name. Programming is all about being as lazy as possible.
  8. Good old console.readline() stops the screen disappearing too fast.
Variables work like labelled boxes that allow you to store things inside them to retrieve later.

What you have just seen is a declaration of two variables, name and age. A variable is a known or unknown value that has been given a symbolic name. This allows the name to be used independently of the value. It is advisable that a meaningful name for readability and convenience. This name is known as the identifier. To declare a variable in VB.NET we do the following:

Dim identifierName As datatype

Most programming languages have rules about identifiers: they generally have to use only Alphanumeric characters (a..Z, 0..9) and some languages are case sensitive (name != Name).

variable - short term memory used to store temporary values in programming code


Once you have declared a variable you need to assign it a value. Assignment, in programming terms, is the giving of a value to a variable, for example:

identifierName = 7

Here we are assigning the value 7 to the variable identifierName, so when we use identifierName, we mean the value 7:

dim identifierName as integer
identifierName = 7
console.writeline("The value stored in identifierName is: " & identifierName)

Producing:

   Code Output

The value stored in identifierName is: 7


Exercise: Variables

Update the code above to display the age in days, hours, minutes and seconds. No use of calculators! Use the code to do all the work for you.

Answer:

dim name as string
dim age as integer
name = "Syeda"
age = 31
console.writeline("Hello " & name & " you are " & age & " years old")
console.writeline("This also means you are " & age * 12 & " months old")
console.writeline("This also means you are " & age * 365 & " days old")
console.writeline("This also means you are " & age * 365 * 24 & " hours old")
console.writeline("This also means you are " & age * 365 * 24 * 60 & " minutes old")
console.writeline("This also means you are " & age * 365 * 24 * 60 * 60 & " seconds old")
console.writeline("Bye " & name & "!")
console.readline()

Give a good reason why you made age a variable in the previous code

Answer:

To keep track of a changing value that is used in many places but only needs to be updated in one.

What will the following code output:

dim x, y as integer
x = 45
y = 9
console.writeline("The sum of x + y = " & x + y)
console.writeline("y goes into x " & x / y & " times")
console.writeline("x multiplied by y = " & x * y)
console.readline()

Answer:

   Code Output

The sum of x + y = 54
y goes into x 5 times
x multiplied by y = 405

A couple of things to note here:

  • on line 1 We declared two variables on the same line. I told you programmers were lazy
  • on line 5 we did a division using a forward slash. Look at your keyboard there isn't a division sign
  • on line 6 we performed a multiply using a star/asterisk. If x can be used as a variable name we better use another symbol, the symbol is *


Let's break the Python program down line by line:

  1. This line contains a variable declaration, creating a temporary data store, a variable, called name. We assign a value to it with = "Peter". In this case it's a string (which Python can tell automatically) so we better use speech marks1
  2. We declare another variable called age , assigning a value to it. As it's an integer we better not use speech marks - 29.
  3. This line writes things to the screen, starting with the text "Hello ". Next, the plus symbol (+) is used to attach the name variable we saw earlier, but instead of putting the variable name, it puts the contents of the variable ("Hello Peter"). It attaches some more text ("Hello Peter you are ") and adds the other variable, age ("Hello Peter you are 29"). To add the age integer variable we convert it to a string using the built-in string function str(). Finally it uses the plus symbol (+) once more to attach the final piece of text ("Hello Peter you are 29 years old).
  4. This line works in pretty much the same way, but it does a calculation, working out the age in months. Computers are like giant calculators you can use to do all the things your little pocket calc can do (and far far more!)
  5. The great things about variables is that we can use them again and again. Here we say "Bye " and use a plus to stick on the name of the person. This is great, by using a variable we only need to write "Peter" once and save it as name. If someone else came along and wanted to change the program they just need to change the value of name. Programming is all about being as lazy as possible.
Variables work like labelled boxes that allow you to store things inside them to retrieve later.

What you have just seen is a declaration of two variables, name and age. A variable is a known or unknown value that has been given a symbolic name. This allows the name to be used independently of the value. It is advisable to use a meaningful name for readability and convenience. This name is known as the identifier. To declare a variable in Python we simply create it with a meaningful name.

Most programming languages have rules about identifiers: they generally have to use only Alphanumeric characters (a..Z, 0..9) and some languages are case sensitive (name != Name).

variable - short term memory used to store temporary values in programming code


Once you have declared a variable you need to assign it a value. Assignment, in programming terms, is the giving of a value to a variable, for example:

identifierName = 7

Here we are assigning the value 7 to the variable identifierName, so when we use identifierName, we mean the value 7:

identifierName = 7
print("The value stored in identifierName is: " + identifierName)

Producing:

   Code Output

The value stored in identifierName is: 7


Notes

[edit | edit source]
1.^ You get a choice in Python between using speech marks or apostrophes for strings, so "Peter" and 'Peter' are the same. This is important to know if you start looking online for examples of Python code since some people will use speech marks whereas other people like to use apostrophes instead.