A Beginner’s Guide To Python Variables

A variable in Python is a storage object that stores any value (like numbers, or strings) in itself and makes changes in data. It is one of the fundamentals of the Python programming language. 

This article will help you to learn about Python variables, what they are,  how to use them to manipulate data, different data types variables, and rules to define the variable’s names in Python. We will also put light on some basic operations on numbers and strings.

Example: Define an ” x ” variable with a numerical value of 10.

				
					x = 10
				
			

In this example, the variable “x” holds the value of 10. Therefore the variable is x, and the data it holds is the value.

The data type of variable is the type of value or data it holds.

Note: Always variable is defined on the left side and value on the right side.

In Python there are three types of numerical values:

Integer:  It is a number without a decimal point like 1,2,3,4, etc.

Float: It is a number with a decimal point like 1.1,1.2,1.3, etc.

Complex number: It is a number that contains an alphabet with itself like an imaginary number. 

Its syntax is a+bj where a and b are any number and j is an alphabet such as 1+4i, 9+3k, etc.

Another type of data type that exists in Python is a string which is a collection of characters. It is defined within a single or double quote.

Example: Define a variable with an integer data type.

				
					x = 10
				
			

Example: Use the type() function to check the data type of variable “x”.

				
					x = 10
data_type = type(x)
print(data_type)
				
			
				
					Output:
<class 'int'>
				
			

Note: To display the output of the variable, use the print() function.

While storing an integer into a variable, Python allows us to assign an arithmetic sign to a value.

Example: Define a variable by assigning arithmetic operation to value.

				
					x = 10+10
print(x)
				
			
				
					Output:
20
				
			

It gives the sum of two numbers.

Moving ahead….. Let’s cover the floating integer and complex integer.

Example: Define an ” x ” variable with a floating value of 10.2.

				
					x = 10.2
				
			

Check the data type of the variable “x”.

				
					x = 10.2
data_type = type(x)
print(data_type)
				
			

Example: Define an ” x ” variable with a complex value.

				
					x = 3+2j
				
			

Check the data type of the variable “x”.

				
					x = 3+2j
data_type = type(x)
print(data_type)
				
			

Example: Define an ” x ” variable with a single quote string value of ‘Python’. 

				
					x = 'Python'
print(x)
				
			

Check the data type of the variable “x”.

				
					x = 'Python'
data_type = type(x)
print(data_type)
				
			

Example: Define an ” x ” variable with a double quote string value of ‘Python’. 

				
					x = "Python"
print(x)
				
			

Check the data type of the variable “x”.

				
					x = "Python"
data_type = type(x)
print(data_type)
				
			

Note: The value within a single or double quote is considered the same there is no difference because of different quotes.

These examples contain only a single value in a variable but Python has specific data types or objects that hold a collection of values, too. 

Python list is a type of data type that stores a collection of values in square brackets. 

Example: Define a variable with a data type of list.

				
					x = [10, 20, 30]
print(x)
				
			

Check the data type of the variable “x”.

				
					x = [10, 20, 30]
data_type = type(x)
print(data_type)
				
			

Python allows us to extract the values from the list. We can do this by using the index position method. In Python, the index number starts from zero. It means the first value is 10 at zero indexes, 20 is at one index, and 30 is at the second index, and so on.

Square brackets ( [] ) are used to extract the values from the list.

Example: Use the index position method to extract the value at index position zero.

				
					x = [10, 20, 30]
print(x[0])

				
			
				
					Output: 10 
				
			

Example: Use the index position method to extract the value at index position zero.

				
					x = [10, 20, 30]
print(x[2])
				
			
				
					Output: 30
				
			

Python List is a mutable object, which means we can change the value once they have been defined.

Example: Change the value of the second indexed position from 30 to 40.

				
					x = [10, 20, 30]
x[2] = 40
print(x)
				
			

Example: Change the value of the second indexed position from 30 to [40, 50, 60].

				
					x = [10, 20, 30]
x[2] = [40, 50, 60]
print(x)
				
			

Note: In above example, value at index position second will be considered as a single value.

Tuples are a type of data type that stores the collection of values in round brackets. It is ordered and immutable which means once defined the values are, they cannot be changed. 

Example: Define a variable with a data type of tuple.

				
					x = (10, 20, 30)
print(x)
				
			
				
					Output:
(10, 20, 30)
				
			

Example: Define a variable with a data type of tuple.

				
					x = (10, 20, 30)
data_type = type(x)
print(data_type)
				
			

As same as Python List, Tuples can also be extracted by the index position method.

Example: Use the index position method to extract the value at index position zero.

				
					x = (10, 20, 30)
print(x[2])
				
			
				
					Output: 30
				
			

Once the tuple is defined, the value cannot be changed and if we try, it gives us an error.

Example: Change the value of the second indexed position from 30 to 40.

				
					x = (10, 20, 30)
x[2] = 40
print(x)
				
			

As a result, it returns an error.

When we deal with file location, we need to point it to, called file pointers. The advantage of using file pointers is that when you need to perform various operations on a file, instead of providing the file’s path location, we can assign it to a particular variable and use that instead.

Example: Assign a variable to file’s path

				
					x=open('C:/Users/Downloads/Python_file','r')
print(x)
				
			

Check the data type of the variable “x”.

				
					x=open('C:/Users/Downloads/Python_file','r')
data_type = type(x)
print(data_type)
				
			

Let’s suppose that if we want to assign multiple values then instead of defining a new variable for each value we can assign multiple values in a single line. 

Example: Assign the multiple values in a single line

				
					x,y,z = 10,20,30
print(x,y,z)
				
			

As a result, it returns an error because an equal number of arguments did not pass to the variables. 

Note: Make sure that the number of values assigned to the variables is equal to the number of variables, else it will always return an error.

Example: Assign the same value to multiple variables.

				
					x,y,z = 10
print(x,y,z)
				
			

Let’s move ahead and look at naming rules for variables.

Starts with letter of the alphabet or an underscore(_)

Not Start with Number and Symbol 

Name = ‘Alex’

_name =   ‘Alex’

3name = ‘Alex’

!name = ‘Alex’

Rules:

  1. A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  2. Variable names are case-sensitive (number, Number and NUMBER are three different variables)
  3. A variable name cannot contain spaces.
  4. The variable name cannot start with reserved keywords.

Example: Variable names are case-sensitive.

				
					num = 1
Num = 2
NUM = 3

print(num)
print(Num)
print(NUM)
				
			

Example: Defined a variable with the reserved keyword.

				
					try = 1
print(try)
				
			

Python variables also allow us to play with arithmetic operations.

Example: Use + arithmetic operation to add the two values.

				
					x = 10
y = 20
print(x+y)
				
			

Similarly, we can perform multiplication as well and so on…

Example: Use * arithmetic operation to add the two values.

				
					x = 10
y = 20
print(x*y)
				
			

Now let’s know how to use arithmetic operations on string variables.

Example: Use + arithmetic operation to add the two values.

				
					x = 'coding'
y = 'streets'
print(x+y)
				
			

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, etc.

Example: Use the Index Position Method to extract the value.

				
					x = 'coding'
print(x[3])
				
			

Use the ( : ) to get the range of characters from the string variable. The first index is always included in the range but the last index is not, if you want to get the last index’s character, then plus one to the excluded index number.

Example: Use the ( : ) to get the characters from the range.

				
					x = 'coding'
print(x[0:5])
				
			

It returns the character from ‘c’ to ‘n’ instead of ‘c’ to ‘g’. As the range does not include the last index number therefore it returned only the character ‘g’.

Example: Use the ( : ) to get the characters from the range.

				
					x = 'coding'
print(x[0:6])
				
			

As a result, it returns the character from ‘c’ to ‘g’, as we added one to the last index (5 +1).

To get the length of a string, use the len() function.

				
					x = 'coding'
print(len(x))
				
			

Let’s see how you can extract characters from two strings and generate a new string.

				
					x = 'Holiday is today'
y = 'Have a great day'
				
			

The new string is, “Holiday is a great day” and be stored in variable z.

				
					z = x[0:10] + y[4:]
print(z)
				
			

Wrapping up!

I hope this article helped you to get started with the concepts of Python variables and cleared Python variables from basics. After reading this article, you may have learned more about what a variable is, the 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.

Happy Coding 🙂 

Leave a Comment

Stay in the loop

codingstreets