What is a Variable in Python Programming Language?

Shubham Hanmant Pawar
6 min readSep 13, 2023

--

Hello all,
Shubham Here again, In this article, we will learn what is ‘variable’ is.
We all know Python is one of the most simple and easy programming languages nowadays. The ease of access and readability makes Python ideal for beginners who want to learn the basics of coding. As a Python programmer, we need to understand what is variables.

To store values, a Python variable is a reserved memory location. In other words, for processing, a variable in a Python program gives data to the machine.

In Python’s Variables

As the name suggests, Python variables and data types are values that differ. In a programming language, where you store a value, a variable is a memory location. In the future, according to the requirements, the value that you have stored will change.

For example,

is one command that prints whatever is inside its parenthesis. When you want to write another print statement you have to begin with a new line.

The first two statements are written one after another without having spaces before the line begins. The third line however has one tab space before the line begins and the fourth has the space of one spacebar.
Only the first two statements will work.
Note: If you try to run all four codes in one file, it won’t run. Because even though the first two lines of code work (because those two have the correct indentation), because of the other two lines whole program will be stopped (They don’t have the correct indentation).
First run, first two lines of code alone. Then run the third and the fourth separately alone.
When we get to concepts like conditions and function please don’t forget to follow indentations in your code when you are writing, since I won’t be focusing on indentation there. Practicing will let you understand how the indentation works better in this language.

Okay, so variables huh?

It’s so easy, variable allows you to store values(or data) in the runtime. So that we could use those values again and again by calling the variable name within the program without having to type the value again and again whenever we want those values in different places. Once you assign a value to a variable you can use much time in many places.

Don’t worry I’m gonna explain.

So the runtime means until your program finishes running. When you run your program in the command line, all your codes written in that file will run from top to bottom and it will stop if that file doesn’t have any more codes to run. So that time your program is running is called the run time.

Calling a variable means calling.

It’s like this, you have a name, right? Why is that used? So someone can call you by that name. So if you used a variable it has a name and a value, And if you used that variable name anywhere in the program it means calling that variable. Why are we calling? We call that variable to get the value that has been assigned to it, update the value, or delete the value.

Okay, let’s see an example.

We know how to take input from the user right? using input command?

This is what we are going to do, We are going to save the user input into a variable. We did it in our first program. That’s okay if you are straight here. Let’s see it again.

If we run the input command alone in the program, it will just take the input and finish the program. Because that’s all we have said(written) in our program.

Run this command,

What’s use in that right? We want to get that input and do something with the input. The input command gives you the value that the user types in. You just have to catch it.

How do we do that? Well, let’s assign it to a variable so that we can call the variable to get its value right.

The variable has a name and a value. Name is written first, then we will write an equals sign to assign it a value. Which means, that this variable has this assigned value afterward.

For example,
apple=1
That means the apple variable has a value of 1 assigned to it. Giving a variable a value is called assigning a value to that particular variable.

Okay now, let’s run this command,

What we have written here is, that we tell the program to assign anything the user types into the variable user input. Here name is userInput and the value is whatever the user enters from the command line.

You can use anything for the name. And here simple and capital matters. If you wrote the “I” in word input in simple letters, you have to call by that exact format to call the variable you are trying to call. Otherwise, it would give you an error.

For example,
a=4 and A=5 are two different variables. If you called A, you get 4 and if you called A, you get 5.

Note the style I have written the variable name. It’s called a camel case. Following programming best practices makes you a cool programmer.
Indentation is also a good practice. In other languages, indentation doesn’t matter. It wouldn’t give you an error if you followed it or not. You can run thousands of codes in one line but would you be able to read it? Please search more on programming best practices and get familiar with them.

Hmmm, now you have a value for the variable userInput, now What can we do? Let’s just print the value for now

So to print something we use the print command. If we use a variable inside the parentheses of the print command it prints the value of the variable.

Do you see what happens there? It replaced the user input value insidethe parentheses. Pretty awesome huh?
Write again and again the print command again with the variable inside it, so you will know you can use the variable again and again without having to ask for the user input again and again whenever you want that value.

Okay, now you may have an idea about variables.

#vevcodelab

http://vevcodelab.com/

--

--

No responses yet