A variable is a fundamental concept in any programming language. It is a reserved memory location that stores and manipulates data. This tutorial on Python variables will help you learn more about what they are, the different data types of variables, the rules for naming variables in Python. You will also perform some basic operations on numbers and strings. We’ll use Jupyter Notebook to implement the Python codes.
Variables are entities of a program that holds a value. Here is an example of a variable:
In the below diagram, the box holds a value of 100 and is named as x. Therefore, the variable is x, and the data it holds is the value.
The data type for a variable is the type of data it holds.
In the above example, x is holding 100, which is a number, and the data type of x is a number.
In Python, there are three types of numbers: Integer, Float, and Complex.
Integers are numbers without decimal points. Floats are numbers with decimal points. Complex numbers have real parts and imaginary parts.
Another data type that is very different from a number is called a string, which is a collection of characters.
Let’s see a variable with an integer data type:
To check the data type of x, use the type() function:
Python allows you to assign variables while performing arithmetic operations.
To display the output of the variable, use the print() function.
print(x) #It gives the product of the two numbers
Now, let’s see an example of a floating-point number:
type(x) #Here the type the variable is float
Strings are declared within a single or double quote.
In all of the examples above, we only assigned a single value to the variables. Python has specific data types or objects that hold a collection of values, too. A Python List is one such example.
Here is an example of a list:
You can extract the values from the list using the index position method. In lists, the first element index position starts at zero, the second element at one, the third element at two, and so on.
To extract the first element from the list x:
To extract the third element from the list x:
Lists are mutable objects, which means you can change the values in a list once they are declared.
x=70 #Reassigning the third element in the list to 70
Earlier, the elements in the list had [14, 67, 9]. Now, they have [14, 67, 70].
Tuples are a type of Python object that holds a collection of value, which is ordered and immutable. Unlike a list that uses a square bracket, tuples use parentheses.
Similar to lists, tuples can also be extracted with the index position method.
print(x) #Give the element present at index 1, i.e. 8
If you want to change any value in a tuple, it will throw an error. Once you have stored the values in a variable for a tuple, it remains the same.
When we deal with files, we need a variable that points to it, called file pointers. The advantage of having file pointers is that when you need to perform various operations on a file, instead of providing the file’s entire path location or name every time, you can assign it to a particular variable and use that instead.
Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django with the Python Training course.
Here is how you can assign a variable to a file:
Suppose you want to assign values to multiple variables. Instead of having multiple lines of code for each variable, you can assign it in a single line of code.
(x, y, z)=5, 10, 5
The following line code results in an error because the number of values assigned doesn’t match with the number of variables declared.
If you want to assign the same value to multiple variables, use the following syntax:
Now, let's look at the various rules for naming a variable.
1. A variable name must begin with a letter of the alphabet or an underscore(_)
Example: abc=100 #valid syntax
_abc=100 #valid syntax
3a=10 #invalid syntax
@abc=10 #invalid syntax
2. The first character can be followed by letters, numbers or underscores.
Example: a100=100 #valid
3. Python variable names are case sensitive.
Example: a100 is different from A100.
4. Reserved words cannot be used as variable names.
Example: break, class, try, continue, while, if
Python is more effective and more comfortable to perform when you use arithmetic operations.
The following is an example of adding the values of two variables and storing them in a third variable:
Similarly, we can perform subtraction as well.
Additionally, to perform multiplication and division, try the following lines of code:
As you can see, in the case of division, the result is not an integer, but a float value. To get the result of the division in integers, use “//” — the integer division.
The division of two numbers gives you the quotient. To get the remainder, use the modulo (%) operator.
Now that we know how to perform arithmetic operations on numbers let us look at some operations that can be performed on string variables.
var = ‘Simplilearn’
You can extract each character from the variable using the index position. Similar to lists and tuples, the first element position starts at index zero, the second element index at one, and so on.
print(var) #Gives the character at index 0, i.e. S
print(var) #Gives the character at index 4, i.e. l
If you want to extract a range of characters from the string variable, you can use a colon (:) and provide the range between the ones you want to receive values from. The last index is always excluded. Therefore, you should always provide one plus the number of characters you want to fetch.
print(var[0:3]) #This will extract the first three characters from zero, first, and second index.
The same operation can be performed by excluding the starting index.
The following example prints the values from the fifth location until the end of the string.
Let’s see what happens when you try to print the following:
print(var[0:20]) #Prints the entire string, although the string does not have 20 characters.
To print the length of a string, use the len() function.
Let’s see how you can extract characters from two strings and generate a new string.
var1 = “It’s Sunday”
var2 = “Have a great day”
The new string should say, “It’s a great Sunday” and be stored in var3.
var3 = var1[:5] + var2[5:13] + var1[5:]
Get prepared for your next career as a professional Python programmer with the Python Certification Training Course. Click to enroll now!
I hope this blog helped you learn the concepts of Python variables. After reading this blog, you may have learned more about what a variable is, rules for declaring a variable, how to perform arithmetic operations on variables, and how to extract elements from numeric and string variables using the index position. To learn more about global variables, check out our next tutorial. To learn more about programming with Python, enroll in our Python Training Course today!