Variables: Difference between revisions

From Computer Science Wiki
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 3: Line 3:
The official definition: in computer programming, a variable is a storage location paired with an associated symbolic name (an identifier) which contains a value.<ref>https://en.wikipedia.org/wiki/Variable_(computer_science)</ref>
The official definition: in computer programming, a variable is a storage location paired with an associated symbolic name (an identifier) which contains a value.<ref>https://en.wikipedia.org/wiki/Variable_(computer_science)</ref>


In other words, '''a variable is a storage location for data'''. Variables have names. Some computer language mandate you assign a [[data type]] to variables. When asked for the "official definition" please use the definition stated above.  
In other words, '''a variable is a storage location for data'''. Variables have names. Some computer language mandate you assign a [[data type]] to variables. When asked for the "official definition" (or best definition) please use the definition stated above.  
 
Different programming languages assign variables in different ways. In general, though, there are three parts to setting a variable:
 
IDENTIFIER of the variable  | an ASSIGNMENT OPERATOR  | the VALUE of the variable
 
for example:
 
AGE = 17
 
In the example above, the IDENTIFIER of the variable is "AGE". The assignment operator is '''=''' and the VALUE of the variable is 17.
 
=== Variables in Python ===
<syntaxhighlight lang="python">
 
name = "Mr. MacKenty"
position  = "Teacher"
 
# in the example above, we have a variable with the identifier "name" and it has been assigned a '''value''' of "Mr. MacKenty"
# there is also a variable with the identifier "position" and the value of that variable is "Teacher". In memory, a section of memory has been reserved for a variable with the identifiers "name" and "position". 
 
# variables can change. For example, if we wanted to change the variable position, we could simply:
 
position = "New Teacher"
 
# now the value of "position" is "New Teacher".
 
</syntaxhighlight>
 
== Naming variables ==
W3 schools has a nice simple list to help use understand the rules for naming variables<ref>https://www.w3schools.com/python/python_variables.asp</ref>
 
 
* A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
 
Rules for Python variables:
 
* A variable name must start with a letter or the underscore character
* A variable name cannot start with a number
* A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
* Variable names are case-sensitive (age, Age and AGE are three different variables)


== See also ==
== See also ==


[[Variables in php]]
<html>
<iframe width="560" height="315" src="https://www.youtube.com/embed/G41G_PEWFjE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</html>
 
You should also look at:
 
<html>
<iframe width="560" height="315" src="https://www.youtube.com/embed/ijjVDBPwA1o" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</html>
 
== Do I understand this? ==
 
<html>
<iframe src="https://assess.computersciencelearning.org/h5p/63/embed" width="846" height="434" frameborder="0" allowfullscreen="allowfullscreen" lang="en"></iframe><script src="https://assess.computersciencelearning.org/modules/h5p/vendor/h5p/h5p-core/js/h5p-resizer.js" charset="UTF-8"></script>
</html>
 
If you are still stuck, or you have other questions,  you may want to [https://discuss.computersciencewiki.org/ '''ask a question on our discussion board'''].


== Standards ==  
== Standards ==  

Latest revision as of 12:44, 14 July 2021

Programming basics[1]

The official definition: in computer programming, a variable is a storage location paired with an associated symbolic name (an identifier) which contains a value.[2]

In other words, a variable is a storage location for data. Variables have names. Some computer language mandate you assign a data type to variables. When asked for the "official definition" (or best definition) please use the definition stated above.

Different programming languages assign variables in different ways. In general, though, there are three parts to setting a variable:

IDENTIFIER of the variable   | an ASSIGNMENT OPERATOR  | the VALUE of the variable

for example:

AGE = 17

In the example above, the IDENTIFIER of the variable is "AGE". The assignment operator is = and the VALUE of the variable is 17.

Variables in Python[edit]

name = "Mr. MacKenty"
position  = "Teacher"

# in the example above, we have a variable with the identifier "name" and it has been assigned a '''value''' of "Mr. MacKenty"
# there is also a variable with the identifier "position" and the value of that variable is "Teacher". In memory, a section of memory has been reserved for a variable with the identifiers "name" and "position".  

# variables can change. For example, if we wanted to change the variable position, we could simply: 

position = "New Teacher"

# now the value of "position" is "New Teacher".

Naming variables[edit]

W3 schools has a nice simple list to help use understand the rules for naming variables[3]


  • A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for Python variables:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

See also[edit]

You should also look at:

Do I understand this?[edit]

If you are still stuck, or you have other questions, you may want to ask a question on our discussion board.

Standards[edit]

  • Define the terms: variable, constant, operator, object.

References[edit]