Hello Space - CTXz/Python-Sandbox GitHub Wiki


Subject : HelloSpace.py


Variables

Variables are a fundamental part of programming languages. They allow you to store, modify and call data inside of them. You can imagine variables as boxes that can store different kinds information. Each box has its very own name and its very own purpose. Once the box is needed, it can be opened and the information can be taken out of it. You may also change the information inside of the box.

  • When you place data into a variable, you're defining it
  • When you get data from a variable, you're referencing it
  • When you replace data inside of a variable, you're redefining it

Here's an visual example of a variable called Color that is currently defined to "black"

____________
|          |
| "black"  |  Variable with the name Color that is currently defined to black.
|__________|
    Color

In python, this would look the following way:

Color = "black"

It's that simple. As you might have already figured out, the syntax to define a variable is as it follows. First comes the name, which may only contain characters from A-Z a-z _ and 0-9 and must not start with a number, followed by a equal sign (=) and the data to be placed inside of it. To reference a variable, its name must be called. The interpreter then proceeds to reference the data inside of the variable. Like stated before, this can be imagined as taking the content out of the box, just that it gets placed back again after it has been used.

Each variable has its very own type assigned. The type of the variable depends on the data that is defined to it. In this case Color is a str. To check the type of a variable, pass it as an parameter into the type() function.

Example:

Color = "black"
print(type(Color).__name__) # Get type of Color

We may now reference the variable in print for example:

Color = "black"
print("My favorite color is " + Color) # Outputs My favorite color is black

In this example, we addition "My favorite color is " with the data referenced in Color which works fine as both are of type str. The addition of both strings eventually results in "My favorite color is black".

You may also pass color as a separate parameter into print(). A additional parameter can added after a comma, where the comma acts as a separator. This is only possible if the function allows multiple parameters. You'll get to know more about function parameters later on.

Note: Passing values as a new parameters to print will insert a white space into the output.

Example:

Color = "black"
print("My favorite color is", Color) # Outputs My favorite color is black

If you're uncertain why there is no no white space at the end of the first parameter("My favorite color is"), please read the notation mark above.

You may also define one variable to the referenced data of another variable

Example:

Color = "black"
Color2 = Color # This will reference to "black"

You may also addition referenced data of another variable

Example

Number  = 10
Number2 = Number + 10      # This will result in 20
Number3 = Number + Number  # This will result in 20
Number4 = Number + Number2 # This will result in 30

Redefining

Variables may be redefined during run-time. For instance, Color may be redefined to red later in the program.

Example:

Color = "black"
print("My favorite color is " + Color) # Outputs My favorite color is black

Color = "red" # Redefine to red
print("My favorite color is " + Color) # Outputs My favorite color is red

You can append data to the previous definition by referencing the variable itself on redefinition

Example:

Text = "My favorite color is "
Text = Text + " black" # Text becomes "My favorite color is black"

Hello Space

With variables done, we may proceed to the script.

First we're defining planet to Mars

planet = "Mars" # Define variable planet to Mars

We then proceed to output "Hello Mars" and "Bye Mars"

print("Hello", planet); # Call planet variable(mars) and append it to "Hello"
print("Bye", planet);   # Call planet variable(mars) and append it to "Bye"

We then repeat this process after redefining planet to "Saturn"

Usage

Variables are mainly used to:

  • make the code more readable and understandable
  • avoid common repetition
  • change data and values during run-time
  • temporary store data