Python – Variables
How to create a variable in Python ?
Variables are containers for storing data values. To compare with different programming languages, Python has no command for declaring a variable.
Example :
x = 7 y = "steve" print(x) print(y)
Output :
7 steve
Types of Variables in Python
1. Values type variable.
2. Pointer or Address type variable.
3. Reference type variable.
The variables doesn’t need to be declared with any particular type and can even change the type after they have been set.
Example :
x = 10 # x is of type int x = "Shaun" # x is now of type str print(x)
Output :
Shaun
String variables can be declared either by using single quotes or double quotes.
Example :
x = "steve" # is the same as x = 'steve'
What are the Variable Names in Python ?
A variable can have a short name ( like a and b or x and y ) or a more descriptive name ( like name, age, company name, country name etc ).
Rules for python variables :
- A variable name can only start with a Letter or the Underscore ( _ ) character.
- A variable name cannot start with a number or digit.
- A variable name can only contains the Alpha-Numeric characters and Underscore character( like A-z , 0-9 and _ ).
- The Variable names are case – sensitive ( car, Car and CAR are three different variables ).
Note : Remember that variables names are case-sensitive.
How to assign value to Multiple variables in Python ?
Python allows you to assign values to multiple variables in a single or one line.
Example :
x, y, z = "Black", "White", "Grey" print(x) print(y) print(z)
Output :
Black White Grey
You can also type the same value to multiple variables in one line.
Example :
x = y = z = "Black" print(x) print(y) print(z)
Output :
Black Black Black
What is output variable in Python ?
The print function is used to print the output variables on the screen.
To combine both text and a variable, python uses the + character.
Example :
x = "Excellent" print("python is " + x)
Output :
python is Excellent
You can also use the + character to add a variable to another variable :
Example :
x = "python is " y = "Excellent " z = x + y print(z)
Output :
python is Excellent
For numbers, the + character works as a mathematics operator :
Example :
x = 2 y = 5 z = x + y print(z)
Output :
7
If you try to type a string and a number, python will give you an error :
Example :
x = 5 y = "steve" z = x + y print(z)
Output :
TypeError: unsupported operand type(s) for +: 'int' and 'str'
What is Global Variable in Python ?
Variables that are created outside of a function ( see the above examples ) are known as Global variable. It can be used by everyone , both inside of functions and outside of functions.
Example : You have to create a variable outside of a function, and use it inside the function.
x = "Excellent" def myfunc(): print("python is " + x) myfunc()
Output :
python is Excellent
If you create a variable with the same name inside a function, then this variable will be local, and can only be used inside the function. The global variable with the same name will remain same as it was, global and with the original value.
Example : You have to create a variable inside a function, with the same name as the global variable.
x = "Excellent" def myfunc(): x = "Awesome" print("python is " + x) myfunc() print("python is " + x)
Output :
python is Awesome python is Excellent
What is Global Keyword in Python ?
Normally, when you create a variable inside a function, that variable is local variable, and can only be used inside that function.
To create a global variable inside a function, then you can use the global keyword.
Example : If you use the global keyword, then the variable belongs to the global space :
def myfunc(): global x x = "Awesome" myfunc() print("python is " + x)
Output :
python is Awesome
Also, you have to use the global keyword if you want to change a global variable inside a function.
If you want to change the value of a global variable inside a function, then refer to the variable by using the global keyword :
Example :
x = "Excellent" def myfunc(): global x x = "Awesome" myfunc() print("python is " + x)
Output :
python is Awesome
« Previous Next »