Mark As Completed Discussion

In this lesson, we will discuss the use of variables and assignment operators in Python, with a focus on the following key points:

  1. What are variables and the assignment of variables in the Python programming language?
  2. Working with variables and assignment of variables in an introductory programming language.

This is a general purpose tutorial for multiple languages. For the Javascript-specific version of this lesson that focuses on fundamentals frontend engineers need to know, please click here.

Throughout the day, we often need to store a piece of information somewhere and refer to it later.

We encounter these situations while writing a computer program as well, where we often need to store some information or data temporarily. For this specific purpose, variables are used. These variables can store almost any type of data, whether they are numbers, strings, or lists. Data is stored in variables by assign-ing data values to them. This lesson will expand on this topic and discuss variables and their assignment in detail.

Variables

As discussed above, variables can store data. Data values can be assigned to a variable by using the assignment operator, which is the equal sign (=).

Once a value is assigned to the variable, it corresponds to that value unless it is re-assigned. Suppose we assign a value 5 to a variable a. This concept can be visualized as below.

Variables

This ensures that whenever we use the variable a in our program, we will get a value of 5. Let's understand this with code examples in Python.

Variables in Python

In Python, variables can be directly created by providing a variable name and assigning it some corresponding value. The code block below shows the creation of the variable number, which has the value 10.

If we execute this code in a Python interpreter, it will give no output. Why?

Because this is a statement, and this statement did not tell the programming language to display the value of variable number. For that, we'd need to either print() it, or log it out in some other way.

However, it did create a variable number in memory and assigned it the value 10. To check if the value was properly assigned, type either the variable name on the console, or use the print() function. It will display the variable name on the console.

The value that you stored earlier in number is successfully displayed, and we can see that it correctly assigned the value.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Important Rules while Creating Variables

Programming languages understand instructions only if they are given in a specified format (or syntax). When creating variables, we need to be careful of these rules, or the variables will not be created properly, and the program may give errors.

  • Variable names can be created using alphabetical letters (a-z and A-Z), digits (0-9), or underscore symbol (_). Special symbols are not allowed. For example, abc&def is an invalid variable name.
  • Variable names can begin with the underscore symbol or alphabetical letters only, not digits. For example, 123abc is an invalid variable name and will give you an error.
  • Variables in Python are case-sensitive. This means that if we declare two variables score and Score with different values assigned to them, then they are two different variables (not the same variable!).
  • Every programming language has certain keywords, that define in-built functionality in the language. These should not be used as variable names. Since it has a different meaning in the language, declaring variables with already existing keyword names will cause an error. These keywords will be highlighted with a different color whenever you type them, so it is easy for you to distinguish. For example, and is a keyword in Python, hence declaring a variable with that name would raise an error.
  • In Python, it is important to assign a value to a variable. If you only define the variable without assigning it a value, it will produce an error.

Now that we know how to create variables, let's see if we understood them properly.

Try this exercise. Is this statement true or false?

Is the following code snippet a valid way to declare a variable?

1let words = "Hello World!";

Press true if you believe the statement is correct, or false otherwise.

Let's test your knowledge. Is this statement true or false?

Is _name a valid variable name?

Press true if you believe the statement is correct, or false otherwise.

Reassigning variables

Values can be reassigned to variables in Python. When variables are reassigned, their value changes to that of the newer value specified, and the previous value is lost.

Reassignment of Variables

Let's look at an example. We can reassign variable values in Python as follows.

1var a = 10;
2a = 34;

The reassignment works similarly to how we create the variable. We only provide a different value than what was stored before. Here, the initial value of a was 10, which was reassigned to 20 from the second line in the above code block.

Operations on Variables

Using variables in the program makes it easy for us to perform various operations on data. Different types of variables support different types of operations. For simplicity, let's consider the type integer. On this type of variable, all mathematical operations are valid.

Let's understand this with a code example.

1var a = 5;
2var b = 10;
3var result = a + b;
4console.log(result);
5// prints 15 in console

This code block shows that if we have two integer variables, we can add them and get the result. Similarly, other mathematical operations such as subtraction, multiplication, and division are also supported.

Now let's consider if we have a variable of type string. In this case, the operations are defined differently. Here, a + symbol between two variables will join the two strings together.

1var prefix = 'un';
2var root = 'clear';
3var word = prefix + root;
4console.log(word);
5// prints the string 'unclear' on console

Here, the two strings are joined together to produce a new string. This operation is called concatenation. There are several other operations that we can perform on strings, integers, and other types of variables, but we will discuss them in later lessons.

Time for some practice questions!

Build your intuition. Fill in the missing part by typing it in.

What will be the final value of variable a below?

1let a = "Hello";
2let b = a;
3a = "World";

Write the missing line below.

Build your intuition. Click the correct answer from the options.

What will the following program output?

1var firstName = "Anna";
2var lastName = "Green"
3var name = firstName + lastName;
4console.log(name);

Click the option that best answers the question.

  • AnnaGreen
  • Anna Green
  • Anna
  • Will give an error

Summary

In this lesson, we talked about a basic concept in programming, about the creation of variables and assigning values to them. A useful tip for creating variables is to use meaningful names while creating them. If you have a lot of variables in your program and you don't name them properly, there is a high chance that you'll get confused about which variables were supposed to store what value!

One Pager Cheat Sheet

  • This lesson will discuss variables and assignment of data values in Python for storing data temporarily.
  • Variables are placeholders for data values that can be assigned using the assignment operator (=) and re-assigned when needed.
  • Python can create variables directly by providing a name and assigning it a value, as shown in the code blocks, and then use the print() or variable name to display the stored value.
  • We need to be careful to follow the specific syntax rules when creating variables, or they may not be created correctly and cause errors.
  • Creating a variable in Python is done by having a variable name followed by an assignment operator (=) and a value, as is the case with the code snippet words = "Hello World!", which is a valid way to declare the variable words.
  • Yes, using an underscore (_) is a valid way to name variables in Python.
  • Variables can be reassigned in Python, replacing their previous value with a newer one.
  • We can perform different operations on each type of variable, such as mathematical operations on integers or concatenation of strings.
  • The final value of a is "World".
  • The program prints the result of concatenating the values in the first_name and last_name variables, which is AnnaGreen.
  • Creating meaningful variable names is key to being able to keep track of values stored in a program.