CHAPTER TWO: VARIABLES

1 What is a Variable?

A variable is a simple data structure that has a name and a value. Now, let's figure out what is what.

Imagine a folder on your desktop. In this case, the folder is a variable. The folder has the label "film."; This is the name of the variable. The folder contains a video called "The."; This is the value of the variable.

Here is how this variable looks in Python language:



Let's break it down.

1) First, we give a name to our variable – film

2) Second, we put equal sign =

3) Third, we give a value to our variable "Super Cat."

A value in a variable always goes with quotes, like in our example. Otherwise, a variable will not work.

Now, as we know what a variable is and what it contains, let's make one!

2 How to Create a Variable?

Write our variable from the example above to the console:


film = "Super Cat"


And run the code. Nothing? That's right. There is no message to show on the screen as we did not use the print function. We will combine variables with print function in the following chapters. But, for now, hit the >_REPL button.



You should now see the input field that looks like this:



Input the name of our variable, which is the film, and hit enter.



In response to the variable name, the program returns its value, which in our case is "Super Cat."

Now, as you've created your first variable, experiment with it. Change name and value. See what happens.

3 Let's Print Variable Value

We've just learned how to create a variable. Now let's learn how to output its value.

Do you remember the print function we used earlier? Let's combine it with our variable and see what happens. Write this code in the console and hit run:


film = "Super Cat"

print(film)


Let's break it down:

1) First, we declared a variable and gave it a name – film.

2) Then we assigned a value to the variable – Super Cat.

3) Then, on a second line, we wrote a print function.

And finally, we passed the variable name to the print function, putting it in the function brackets.

Every time we create a variable and pass its name to the print function, it will output the variable's value, just like in our example.

Now you know how programmers display variables in Python. It doesn't seem too tricky. Let's have some practice. Below is the code that is missing some elements. You have to fix it so the program could create a variable and display its value.


= " "

()


By now, you should have sufficient knowledge and skills to accomplish this task. When done, iterate on it as long as you like. Change variable names and values. Hone on your new skills!

4 Wrapping Up Chapter Two

In chapter two, we have accomplished the following:

1) Learned what a variable is;

2) Created our first variable;

3) Learned how to display variable values.

5 Chapter Two Test

1. What is the purpose of variables?

1) Computers use variables to store information.

2) Computers use variables to change information.

3) Computers use variables to retrieve or delete information.

2. If a variable name consists of two or more words, you shall connect them using:

1) Underscore.

2) Dash line.

3) Write the words together, without space.

4) Write the words together with a capital letter without space.

3. We can display a variable on the screen by:

1) Using the print() function and passing the print command in its parentheses.

2) Using the print() function and passing the value of the variable in its parentheses.

3) Using the print() function and passing the name of the variable in its parentheses.

4. Arrange the parts of the code so that the program creates and displays the variable.


(name)

name

=

print

"John Doe"

Загрузка...