BCA / B.Tech 8 min read

Variables

Variables in Python:


A variable is a fundamental concept in computer programming. It plays a significant role in all aspects of programming, regardless of the language. Understanding variables is necessary right from the initial stages of programming because no kind of data processing is possible without them.

What is a Variable?
In simple terms, a variable is a container that temporarily stores some kind of data or information. It is used to hold a value in a program and then use that value. We can define a variable with any name to address it and use the information inside it.

Importance and Definition of Variables:
Variables are primarily used to temporarily store data in computer programming. The advantage is that we can reuse the data, perform calculations, and get results. A variable is defined with a name and holds a value.

Types of Variables (based on data they hold):
Integer, Float, String, Boolean, List, etc.

Naming Rules for Variables:
  • Must start with a letter or an underscore.
  • Cannot start with a number.
  • Can only contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
  • Cannot contain spaces.
  • Is case-sensitive (`Name` and `name` are different).
  • Cannot be a reserved keyword.

Scope of a Variable:
The scope of a variable indicates the part of the program where it is valid. The two main types of scope are:
Local Variable: It is valid only within the specific function or block where it is defined.
Global Variable: It is valid anywhere in the entire program.