Like other programming languages, the concept of variables in Python is the same. A variable is a holder or container.

Let’s consider an example from our everyday life. We drink water every day; how do we do it? Initially, we pour water from a large container into a glass and then drink from that glass. Here, the glass and the large container are holders of the water. In programming terms, both the large container and the glass are variables because they contain the water.

We not only drink water from the glass but also milk or juice. Similarly, a variable can be used for multiple purposes, meaning the content (water, milk, juice) can change.

So, we understand that a variable is something that holds something else.

Note: In real life, we can drink both water and milk from the same glass, but in programming, the same variable cannot hold multiple different items at once.

So far, we have seen examples from real life. Let’s see how it works in programming. Consider the following code:

print(10)

The result will be:

10

Here, 10 is a constant number that we directly provided to the print() function, and that is what is being displayed as the result. Now, we can store this number in a variable and then display it through the print() function:

a = 10
print(a)

Here, ‘a’ is a variable where we have stored the number 10, and we display this variable using the print() function.

In programming terms, the = symbol is called the assignment operator. The assignment operator is used to assign a value to a variable.

Let’s declare some more variables:

a = 10
b = "Hello SHAHINUR"
print(a)
print(b)

The output will be:

10
Hello SHAHINUR

This means a variable can be of any type, be it a number, a character, a string, etc.

In other programming languages, the type of a variable needs to be specified, but in Python, that is not necessary.

In Python, you can assign multiple variables at the same time using commas in the same statement:

a, b = 10, "Hello SHAHINUR"
print(a)
print(b)

The results will be as before.

Variable Naming

At first glance, the name of a variable can be anything, but there are some general rules. There are certain words or keywords that cannot be declared as variables. For example – break, for, if, etc. These are Python’s reserved keywords. Additionally:

  • A variable’s name cannot start with a number; it must start with a letter or an underscore.
  • A variable can only be composed of letters, numbers, and underscores.
  • Variable names are case-sensitive, meaning lowercase and uppercase letters indicate different variables. For example, ‘a’ and ‘A’ are two different variables.
0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x