Python variables are core of python language. A variable in python hold some data of a specific type. In python, the data for a variable can change frequently, it is not fixed as in other languages.
In this article, you will learn about the rules to create python variables, variable scope and how to type variables names in python programming.
Re-declaration of Variables
In python programming, you can declare a variable with a specific type and then re-declare it with another data type. For example.
some_variable = 30 print(some_variable) some_variable = "Hello World!" print(some_variable)
In the above code, the python first prints the value of some_variable as number and then after re-declaration it prints the number as string. Therefore, the output will be as follows.
=============== RESTART: C:/Python_projects/Re-declaration.py =============== 30 Hello World!
Rules To Create Variable Names
Like other programming languages , python also follows some rules while you create a variable.
- Variable names cannot start with a number or special character other than “_” underscore.
- Variable names cannot contains white spaces.
- Variable names must not contain any special character other than “_” underscore.
Variable Must Not Start With Number
If you try to create a variable that start with a number. Here is the error you get.
>>> #amount = 66000 >>> #amount >>> #t = 44 >>> #t >>> 45ten = 45000 SyntaxError: invalid syntax >>>
In the example, about we tried to create variables with special character or numbers at the beginning. It is not working and does not print anything.
Even you receive a syntax error.
Variable Name Must Not Use Special Characters Other Than Underscore
>>> my$variable = 34 SyntaxError: invalid syntax >>>
Try to put a special character in the middle or end it will give you same – syntax error. However, if you choose to use “_” underscore, python will allow you to create a valid variable.Here is an example.
>>> my_variable = "Hello World" >>> my_variable 'Hello World' >>>
The above variable name is accepted with ‘underscore’ in the middle.
Variable Name Cannot Use a Whitespace
Inexperienced programmer, usually create a variable with white space in between, however, a white space is treated as a special character.
Tab or white space key on your keyboard will generate a space that is not acceptable in python as a part of variable name. See following example.
>>> my variable = 2100 SyntaxError: invalid syntax >>> first Name = "peter" SyntaxError: invalid syntax >>>
In the code above, we have tried to create two variables with white space in between that resulted in ‘syntax error’.
Variable Names In Python
A variable name in python must be self explanatory, meaning it must define the type of data it holds.
For example, the following name does not make sense.
n1 = "Harry" n2 = "potter" a = 34
Instead of typing above code you can simply type following that make sense.
firstName = "Harry" lastName = "Potter" age = 34
Another thing about variable is to choose the convention or style for variable names. Two styles are very popular
- camel case
- using underscore
The camel case for variable name is basically useful if you have variable names with more than two words.
superCatName = "Meow" myHero = "Spiderman"
Here in the names above, the first word starts with lowercase letters and other words in the variable name starts with capital letter.
Underscore as variable name separater
This is what most of the python programmer uses. They separate the words in variable names with underscore (“_”). For example.
super_cat_name = "teddy" my_hero = "superman"
Python Variable Scope
A python variable can exist in side a block or outside the block.
When it is outside of a block of statements or loops it has a global scope. It means that it will be visible throughout the program.
However, if the variable is created inside of a block then it cannot be seen outside unless the block is executed.
See the example below.
sum = 10 if sum == 10: # in this block we create a variable called result result = sum + 100 print(sum) print(result)
First the variable sum is evaluated and enter the if-block successfully. Then the 100 is added to the sum and assigned to a new variable result.
The sum is also printed inside the block.
When the block is exited, the variable result will display the final value.