Hey folks! Welcome to an another interesting article on Python Programming Language.
Here, we will discuss about the variables of Python programming language.
So, let's dive in!
Variable in python are defined as a reserved location of memory in order to store values or data. In simple words, python variable is a program which is used to give data for processing to the computer. We can also say that the name given to memory location in python is known as Variable.
The name of variables are known as identifiers. In other words we can say that Variables are a example of identifiers as we uses identifiers to identify the literals which have been used in a particular program.
Rules for Naming Variables
The following are the rules that one must consider before naming a variable;
• The first alphabet of variable names must be a underscore character or a letter.
• With a number you can't start a variable name.
• Only alpha-numeric characters and underscores are included in a variable name.
• The names of variable are case-sensitive. For example- TWO, two and Two are three different variables.
• One can't use reserved words or keyword in the variable names.
• There must be no special characters and white-space in the name of variables.
Name = "Deepak"
Age = 22
CGPA = 7.6
Now, let's learn about how to declare the python variable and print it;
Number = 143
After declaring the variable, we can re-declare the variable in python;
Number = 143
print("Before declare: ", Number)
Number = 153.3print("After re-declare:", Number)
Before declare: 143
After re-declare: 153.3
Assigning value to Variable
Now, let's discuss about the assignment of value to variable.
• Single Value to multiple variables- In python, we can easily assign a single value to multiple variables simultaneously by using the "=" operator.
a = b = c = 101
• Different Values to multiple variables- In python, we can also assign different values to multiple variables in a single line by using the "," operator.
a, b, c = 101, 102, 103
Types of Python Variables
Now, let's discuss about the types of Python Variables. There are two types of Python variables which are discussed as follows;
1. Local Variable- Local Variables are those variables which have scope within the function and are also declared within the function.
a = 70
b = 30
c = a + b
print("The sum of two digits is:", c)add()
The sum of two digits is: 100
2. Global Variable- Global Variable are those variables which can be used everywhere in the program and the scope of global variable is in the entire program.
x = 6.3
x = 'Hey Folks, Welcome to Answersjet'
Hey Folks, Welcome to AnswersjetHey Folks, welcome to Answersjet
Above we have discussed the variables of Python programming language. Variable in python are defined as a reserved location of memory in order to store values or data. In simple words, python variable is a program which is used to give data for processing to the computer. There are two types of Python variables i.e., Local Variable and Global Variable.