Variables
Variables are used to store data in memory which can be used later for doing any computation.
In this python tutorial, you are going to learn about variables which are one of the most fundamental concepts in programming. Variables are not only specifically used in python. They exist in every programming language.
Data Type of Variable
You do not need to specify the data type of variable in python. You just have to write the variable name and assign it a value.
Python interpreter allocates some memory to variable "age". 20 is stores at some place in the memory with the label age. Whenever we want to use this 20, we can use this using label age.
Printing Variable Values on Screen
When you need to print the value of a variable on screen, you need to write the name inside print(variable_name). Remember, when you print text, it is written in double quotation marks. But variable name is written without double quotation marks.
Rules for assigning variable names
- Variable name must start a with letter or an underscore.
- Variable name must contain only letters, alphabets or underscore. It must not start with a number.
- It can not be a reserved keyword.
- It should be a single word. No spaces are allowed e.g, name, myAge, my_name etc.
- For more information, you can follow Python Enhancement Proposal 8 (PEP 8) guidelines.
- Python is a case sensitive language. The variable named "name" and an other variable named "Name" are not same.
- Keep the names of variables descriptive.
Naming Conventions
We use the following naming conventions when our variable name consists of more than one word.
- Camel Case: Each word starts with capital letter but first word starts with small letter. For example, myName, myAge etc.
- Pascal Case: All words start with capital letter. For example, MyName, MyAge etc.
- Snake Case: Each word is separated by an underscore. For example, my_name, my_age etc.
Changing the value of a Variable
We can change the value of our variable or we can say, we can reassign the value of our variable.
Scope of a Variable
The scope of a variable refers to the area of a program where that variable can be accessed and modified. A variable can have local or global scope.
Local Scope
A variable declared within a specific function, method, or block is said to have local scope. It is only accessible within that function, method, or block and can not be accessed outside.
Global Scope
A variable declared within the main body and is not inside any block of code, function, or methos is said to have global scope. It can be accessed anywhere in the code.
Example Code
In this example;
- We can see that myName is a global variable because it is declared in the main body.
- On the other hand, hisName is declared inside the function printName().
- So, we know that we can access the global variables anywhere in the program and we are accessing myName in printName() function and after tht in main body.
- But we can not access local variables outside the block in which it is declared and you can see that we can not access hisName outside the printName() function.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP