How To Create A Variable In Python

In this tutorial, you will learn how to create a variable in Python programming language. Variables are used in programming to store information, and they represent the reserved memory locations that store values.

This means that when you create a variable, you reserve some memory space in which to store a value. In Python, variables can store different types of data, such as integers, floats, strings, lists, dictionaries, and more.

Step 1: Assign a Value to a Variable

To create a variable in Python, you simply need to assign a value to a variable name using the assignment operator (=). The variable name should be lowercase and must not begin with a number. You can use letters, numbers, or underscores, but the first character must be a letter or underscore. Here’s a sample code to create a variable:

In this code, we have created a variable named “my_variable” and assigned the value 5 to it.

Python is a dynamically-typed language, which means that it automatically detects the data type of a variable based on the assigned value. No need to explicitly declare the data type of a variable like in some other programming languages.

Step 2: Use the Variable in Your Code

After creating a variable, you can use it in your code. Here’s an example of using the variable we created earlier:

In this code, we are multiplying the value of “my_variable” by 10 and storing the result in a new variable named “result”. Then, we are printing the value of the “result” variable.

The output of the code would be:


Step 3: Update the Value Stored in a Variable

You can also update the value stored in a variable by reassigning a new value to it. Here’s an example on how to update the value of a variable:

In this code, we are updating the value of “my_variable” from 5 to 7 and then printing the new value.

The output of the code would be:


Remember that the assignment operator (=) replaces the previous value stored in the variable with the new one.

Full code

Here is the full code of the tutorial for reference:

The output of the full code would be:



In this tutorial, you have learned how to create a variable in Python. We covered assigning a value to a variable, using the variable in your code, and updating the value of a variable. Now you can create and work with variables to store data in your Python programs.