Python Variables
Variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
In Python, we don’t need to specify the type of variable because Python is a infer language and smart enough to get variable type.
Example: Creating Variables
x = 5 # x is of type int
y = "Vaibhav" # y is of type str
print(x)
print(y)
Naming Convention (Tradition) of Variable
- A variable name must start with a letter or the underscore character( _ ).
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- A variable name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
- Variable names are case-sensitive (age, Age and AGE are three different variables)
Legal variable names:
myvar = "Ram"
my_var = "Ram"
_my_var = "Ram"
myVar = "Ram"
MYVAR = "Ram"
myvar2 = "Ram"
Illegal variable names:
2myvar = "Ram"
my-var = "Ram"
my var = "Ram"
Multi Words Variable Names:
Variable names with more than one word can be difficult to read.
There are several techniques you can use to make them more readable:
Case | Example | Description |
---|---|---|
Camel Case | myVariableName = "John" | Each word, except the first, starts with a capital letter. |
Pascal Case | MyVariableName = "John" | Each word starts with a capital letter |
Snake Case | my_variable_name = "John" | Each word is separated by an underscore character: |
Assign Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line:
x, y, z = "Apple", "Samsung", "LG"
print(x)
print(y)
print(z)
Output:
Apple
Samsung
LG
Note: Make sure the number of variables matches the number of values, or else you will get an error.
Assign one Value to Multiple Variables
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output:
Orange
Orange
Orange
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.
fruits = ("apple", "banana", "cherry")
x, y, z = fruits
print(x)
print(y)
print(z)
Output:
apple
banana
cherry
Output Variables:
The Python print() function is often used to output variables.
x = "Python is awesome"
print(x)
In the print() function, you output multiple variables, separated by a comma:
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
You can also use the + operator to output multiple variables:
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Notice the space character after “Python ” and “is “, without them the result would be “Pythonisawesome”.
For numbers, the +
character works as a mathematical operator:
x = 5
y = 10
print(x + y)
In the print() function, when you try to combine a string and a number with the +
operator, Python will give you an error:
x = 5
y = "John"
print(x + y)
#TypeError: unsupported operand type(s) for +: 'int' and 'str'
The best way to output multiple variables in the print() function is to separate them with commas, which even support different data types:
x = 5
y = "John"
print(x, y)
Global Variables
Variables that are created outside of a function are known as global variables. Global variables can be used by everyone, both inside of functions and outside.
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
The global Keyword
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Also, use the global keyword if you want to change a global variable inside a function.
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Average Rating