Getting Started with Variables in Python

Are you a beginner in programming, you will probably hear about the Python programming language. In fact, Python is an easy to learn, powerful, and high-level programming language that uses instructions to tell a computer how to perform a specific task. This language was created by Van Rossum Guido in the late 1980s.

In this article you will find answers to the following questions:

  • What Is a Variable in Python?
  • What Are the Different Python Variable Types?
  • How to Declare and Use a Variable?
  • What about Re-Declaring a Variable?
  • How to Delete a Variable in Python?

What Is a Variable in Python?

Actually, a variable is common to all programming languages. It can be defined as a reserved memory location in which information can be stored. We can also take a variable as a component who provides to the computer the data needed for any operation.

What Are the Different Python Variable Types?

In Python, every variable belongs to a data type.  A data type can simply be defined as a peculiar family of variables. In other words, this means that they all share in this family or set of variables, the same characteristics.

As different variable types in Python, we can list:

  • Numbers
  • Lists
  • Tuples
  • Strings
  • Dictionaries

There are many other variable types used in Python and you can learn more about each of them here.

How to Declare and Use a Variable?

It is quite simple to declare or use a variable in Python. You can make this by using a name of your choice, like alphabets or a combination of letters or numbers. Nevertheless, there are some exceptions to name a variable.

Let’s Do an Example!

We will create a variable named “myVariable” to which we will assign the value 10. Then, we will use a function to display the value of that variable on the screen.

myVariable = 10
print (myVariable)

As the output of this small program, you should be able to see “10” displayed on your screen.

What about Re-Declaring a Variable?

Re-declaring a variable is an operation that can be performed on a variable once it has been declared. By doing this, the previous content of the variable is deleted. Let’s do one example.

# Declare a variable and assign to it the value 2
myVariable = 2
# Displaying the value of the variable on the screen
print (myVariable)

# Re-declaring the variable
myVariable = "Hello"
# Displaying the value of the variable on the screen
print (myVariable)
# Once the last line is executed, your displayed value will be "Hello".

How to Delete a Variable in Python?

Variables, once they are declared, can also be deleted to free the memory space. To delete a variable that already exists, you use the del command followed by the name of your variable, for instance:

a = 2021
print (a)
# This will display the value 2021.

del a
print (a)
# If you run the program, it will displayed an error because the variable a doesn't exist anymore.

Summary

Python is a powerful programming language used to build web and mobile applications. One of the important things in programming are variables and this article is about variables in Python. So, basically, we learn that a variable is an allocated space in the computer memory used to store information. On variables, we can perform many operations such as declaring, re-declaring, printing and deleting.

You take the time to read the whole article?

Yes? Then this is great because you now know what a variable is and what are some basic operations that can be performed on them. Here is a great resource that can help you to get started with programming in Python.

Hope this article will be helpful!

Stay tuned for more articles about Python programming concepts and Data Science!

3
0

Related Posts