Variables and Constants In Python
 

Variables and Constants In Python

 

A variable in Python is used to store a value in the memory. The value of a variable can be changed later in the program. In Python programming, a variable is created when you first assign a value to it.

For example –
num = 10

place = "New Delhi"

Here num and place are two different variables one store a number which is 10 and the other one stores a string called New Delhi.

How a variable’s value is changed?

The value stored in a variable is not fixed it can be different for different stages of a program.

For example –

place="New Delhi"
print(place)
#Assigning a new value to the variable place
place="Mumbai"
print(place)

Now when you save and execute this program on your system this will display the output as given in the image below –

As you can see here earlier the place variable contains the value New Delhi and later it was replaced by Mumbai.

Rules and naming convention for variables in Python

There are certain rules in Python to use variable names –

  • A variable name must start with a letter or underscore, numbers or digits are not allowed at the starting
  • It should only contain the alphanumeric characters and underscore i.e. A-Z, a-z,0-9 and
  • The variable name is case sensitive that means NAME and name are two different variables in Python
  • It is recommended to use a descriptive name of a variable for example age make more sense than a
  • Using Python keywords as the variable name is not allowed

Examples of variables in Python

Following are the examples of some valid variables in Python.

_emp_id
name_of_employee
age
SALARY
Contact

The example of some illegal variable names that are not allowed in Python are –

1var
var-name
Var name

You can declare a multi-word variable name in the following ways, this will enhance the code readability.

You can separate each word with an underscore called Snake case.

name_of_emp = "Sumit"

Start the first letter of each word in capitals except the first one this is known as the Camel case

nameOfEmp = "Sumit"

Start each word in capitals this is known as the Pascal case

NameOfEmp = "Sumit"

Constant in Python

A constant is a variable in Python whose value is fixed during the program execution that means it can not be changed later in the program.

Example of declaring and assigning a value to constant in Python

Following is the example of declaring a constant in Python and assigning a value to it.

constant.py

PI = 3.14
GRAVITY=9.8

You can declare them in a separate python file let’s say constant.py and then import and use them in your main.py module

main.py 

import constant
print (constant.PI)
print (constant.GRAVITY)

When you execute main.py this will display the output as given in the image below.

Always declare a constant name in capital letters as we do in the above example.

Please note that in reality there is no way to declare a constant in Python I mean there is no keyword to declare constants like we do use const in C or C++. So it doesn’t actually prevent reassignment.

Now try these examples on your own system. If you find any problem then leave it in the comments below.

Previous Comments In Python
Next Data Types in Python
 

About author

You might also like

Python Programming 0 Comments

How to install Python pip on Linux?

Python pip is a package management tool used to find, install, remove and list the packages from the Python Package Index (PyPI). In this article, we will discuss how to

Python Programming 0 Comments

Writing Hello World! Program In Python

In the last few articles, we have installed python and set up VS code IDE on Linux and Windows for development using Python. Today in this tutorial we will write

Python Programming 0 Comments

Python Strings

A string is a sequence or a collection of characters enclosed within single or double-quotes. In Python, the print() function is used to display a string. Since a computer understands

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply