0% found this document useful (0 votes)
45 views

Code Variables: X Becomes A Shorthand For 7 (Whatever Is in The Box)

The document introduces the concept of variables in code. It explains that a variable acts like a box that can store a value, and that using the assignment operator (=) stores a value into the variable. It notes that variables allow a value to be stored once and then reused across multiple lines of code, avoiding repetition. An example shows assigning the value "7" to the variable x, and then printing x multiple times to retrieve and reuse that stored value.

Uploaded by

Tarun Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Code Variables: X Becomes A Shorthand For 7 (Whatever Is in The Box)

The document introduces the concept of variables in code. It explains that a variable acts like a box that can store a value, and that using the assignment operator (=) stores a value into the variable. It notes that variables allow a value to be stored once and then reused across multiple lines of code, avoiding repetition. An example shows assigning the value "7" to the variable x, and then printing x multiple times to retrieve and reuse that stored value.

Uploaded by

Tarun Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Code Variables

In this section, I want to add the idea of variables in code.

A "variable" is like a box that holds a value

x = 7;

This stores the value 7 into the variable (i.e. box) x

Later x in the code retrieve the value from the box

x becomes a shorthand for 7 (whatever is in the box)

Using = in this way is called "variable assignment"

VARIABLES IN CS101

How we use variables in CS101:

-Assign a value into a variable once

-Then use that variable many times below

A convenient shorthand in the code


x = 7;
print(x);
print("lucky", x);
print("x is", x);

Experiments: try assigning (=) these values to x: 8,


"hi"

Store a value once, use it on several lines, saving


repetition

= in algebra is different, two things are equal forever

= in code is simple, just puts a value in a box when


run

Variables work as a shorthand -- we = assign a value into a variable, and


then use that variable on later lines to retrieve that value. In the simplest
case, this just works to avoid repeating a value: we store the value once,
and then can use it many times. All computer languages have some form
of variable like this -- storing and retrieving values.

CODE EXAMPLE - THE CURSE


Change the code below so it produces the following output. Use a variable
to store the string "Alice" in a variable on the first line like x = "Alice";,
then use the variable x on the later lines. In this way, changing just the
first line to use the value "Bob" or "Zoe" or whatever changes the output
of the whole program.

Alice Alice Alice

I had a crush on Alice


print(1, 2, "hi");

Show Solution
Next: that's code with variables, now you try it.

You might also like