In this article, we will discuss on variables of Python. The below contents are of this article:
Introduction
In order to store values and manipulate it as it required we need a variable. In Python variable plays an important role as well as in other programming languages.
The variable in Python is created the moment a value is assigned to it. The assignment operator is declared as an equal to sign (=) as shown below.
x=10
print(x)
Variables do not need to be declared with any particular type and can even change type after they have been set. The code has become easier after introducing comments in it. As we have already read the use of comments in the previous post. Click here
x= 123.45 #x is float type variable
x= 30 #x is now a type int
Here we have assigned the float type data x whose value is 123.45 and then modified the data type to an integer keeping the value 30 in it.
Variables can be accessed by using their identifier (name) as shown below.
String variables can be declared either by using single or double quotes:
a = "Itika"
a = 'Itika'
#both are the same
Variable Names
Any suitable identifier can be used as a name of a variable, based on the following rules:
- The first character of an identifier must be an underscore (‘_’) or a letter (upper or lowercase).
- Punctuation characters such as @, $, and % are not allowed.
- The rest of the identifier name can be underscore (‘_’) or a letter (upper or lowercase) or digits (0-9).
- Examples of valid identifier names are num1, sum, _total_val, Temp etc.
#legal variable names:
myname = "Itika"
my_name = "Itika"
_my_name = "Itika"
myName = "Itika"
MYNAME = "Itika"
myname0 = "Itika"
#Illegal variable names:
0myname = "Itika"
my-name = "Itika"
my name = "Itika"
You have to remember that Python is a case-sensitive language.
Dynamic Typing
One of the important features of Python is that it is a dynamically-typed language. A variable stores the value of the particular data type using memory locations.
A variable in Python is not bound permanently to a specific data type and so prior declarations is impossible. Instead, it only serves as a label to a value of a certain type. In Python, the data assigned to a variable decides its data type and not the other way around. The following is a statement where a string variable has been assigned. We find the data type by simply using the type() function. To know more you can refer to this.
Output Variables
Python access the output variables by using the “print” statement. We can combine both text and variable using “+” character.
a = "fun"
print("Coding is " + a)
“+” character can also be used to add a variable to another variable.
a = "Coding is "
b = "fun"
c = a+b
print(c)
But the thing is that if you want to add two different data type together it will show an error. Such as we add a number with the string as shown below :
Assign Value to Multiple Variables
Python allows a programmer to assign a single value to more than one variables simultaneously. for example :
sum = flag = x = y = 0
print(sum)
print(flag)
print(x)
print(y)
So, in the above statement, all the four integer variables are assigned a value 0.
Or, you can assign values to multiple variables in one line:
x, y, z = "Spyder", "PyCharm", "VSCode"
print(x)
print(y)
print(z)
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 = "fun"
def myfunc():
print("Life with 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.
x = "fun"
def myfunc():
x = "interesting"
print("Life with Python is " + x)
myfunc()
print("Life with Python is " + x)