Variables

Variables

A variable is like a mailbox for computers. It is used to hold a value. That value can be a number (an integer or double) or a word (a string).

Prerequisite

This page assumes you have completed the C# Intro and/or Python Intro lessons.

Intro

We can create a variable within C# by using the keyword var. For Python, we don’t need to add a keyword; simply assign a name and that name is now a variable. In this example, we assigned the name myVariable.

var myVariable = 5;
myVariable = 5

Variable names should be meaningful. You should know what a variable is holding based on the name we give it. This makes the code much easier to read and understand what’s happening inside. We can give the variable any name, but the name we create can’t contain spaces, can’t start with a number, and can’t contain a symbol.


Variable Types

We’re going to discuss 3 different basic types of variables. There are others that you will learn as you get more advanced.

  • Integer (int) – An integer is a whole number, like 1, 42, -53, 4834, etc. It can be a positive or negative number but can’t have a decimal point.
  • Double (double) – A number with a decimal point, like 0.1, 3.5, 55, -10.2, etc. It can be a positive or negative number and can have decimal point.
  • String (string) – A group of characters. It can be a single word or a long sentence. String variables are always defined by using quotation marks. For example in C#, “This is a string”. Python language also allows for single quotation marks, ‘This is also a string in Python’.

The languages know what kind of variable we’re creating based on value we’re assigning to it.

// creates an integer
var number = 10;

//creates a double
var fraction = 3.5;

//creates a string
var name = "John";
# creates an integer
number = 10 

# creates a double
fraction = 3.5

# creates a string
name = "John"

Some languages, like C#, are a fixed variable type. Basically, once we create the variable it must contain the same type of value for the rest of the program.

// create an integer variable, named x
var x = 10;

// change the value of the variable
x = 83;

// this is wrong and will be an error.
x = "hello";

In fact, C# requires a type for any variable, but using var will let C# pick the most appropriate type for us.

These two lines are technically identical.

var x = 10;
int x = 10;

Python is dynamic and allows to switch types. In the following example, the first line will create a variable named x, since this is the first time we assign a value to x. The variable type will be an integer. The second line will change the variable type to a string and assign “Hello” to it.

x = 10
x = "Hello"

Arrays

Arrays are a way to store a list of variables that are the same type into one variable name. Let’s create an array of string variables called flowers, and initialize it with values.

var flowers = {"Roses", "Violets", "Daisies", "Mums", "Orchids", "Lilies"};
flowers = ["Roses", "Violets", "Daisies", "Mums", "Orchids", "Lilies"]

We can access and assign variables to specific spots in an array. We do this by including a number in brackets after the name, as shown below.

Print(flowers[1]);
Print(flowers[1])

You may wonder why ‘Violets’ appeared instead of ‘Roses’? This is because arrays start counting from zero. To print ‘Roses’, we would have to use flowers[0].

We can easily set a value to a specific spot in an array just like we would set any other variable. It just has to be the same variable type as the array we created.

flowers[3] = "Marigolds";
flowers[3] = "Marigolds"

Now the flowers[3] slot has replaced ‘Mums’ with ‘Marigolds’.


What’s Next?

Learn about Loops and how they are used to repeat tasks.


BrainStorm

We know that anything between double-quotes is a string. (Python allows single-quotes as well). With that in mind, what is the difference between 53 and “53”? Can you guess what this code will print? Try it!

By the way, the last line is different as C# and Python handle adding to a string differently. When adding a string to a string (“5″+”5”) the result is a one string that has both strings (“55”) but adding a numerical value to a string (“5” + 4) does not work. C# automatically converts the numerical value to a string. A 4 becomes “4” but Python does not and will issue an error. We will need to tell it to change the value to a string by adding (str) before the numerical value.

Print(53);
Print("53");
Print(53 + 1);
Print("53" + "1");
Print("53" + 1);
Print(53)
Print("53")
Print(53 + 1)
Print("53" + "1")
Print("53" + (str)1)