3. Variables in Python

Spread the love

The most fundamental concept in programming is the concept of a variable.A variable is like a box in which you can store things like numbers and strings, for later use. We use variables to temporarily store data in a computer’s memory.

Here’s an example:

age = 24

when Python interpreter executes this code, it will allocate some memory, then it will store the number 24 in that memory, and finally it will attach this age label in that memory location. In other words, a variable is a name that refers to a value during program execution.

So, if I use the print function to print the value of the age variable, it will print 24, because age refers to 24.

age = 24

So, the output of this program is 24.

The equals symbol (=) is called the assignment operator. The assignment operator means “Put this value in this variable”.

Note that the variable name is an arbitrary name that you select. A variable can have a short name (like a and b) or a more descriptive name (age, price, account_id). But there are rules for naming variables.

  • Variable names are case-sensitive
  • Variable names should not start with a number
  • Python Keywords are not allowed as variable names

Rule number 1: Python is case-sensitive

Rule number 1 says that “Variable names are case-sensitive”. Python is sensitive to capitalization of letters. For example, “Age” and “age” are two different variables because the “A” is uppercase in the first variable and lowercase in the second variable:

Age = 50
age = 24
print (Age)

So, if we run this code, the print function will print 50.

Rule number 2: variable names should not start with a number

OK, rule number two, variable names should not start with a number. For example, if you declare a variable like this:

1age = 24

And run the program, you will get an error message says:

Rule number 3: Python Keywords are not allowed

Another rule is that Python Keywords are not allowed as variable names. Keywords are a series of reserved words in Python. They have special meaning for Python.

Here is a complete list of python keywords:


Updating a variable

And one last thing is about updating variables. You can update a variable after initializing it like this. For example, I can reassign the value of the age variable like this:

age = 24
age = 30

If I run the code, the result will be 30.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *