A Beginner’s Guide To Python Variables

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:

x=100 

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.

xvariable

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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:

x=100

To check the data type of x, use the type() function:

type(x)

type-x

Python allows you to assign variables while performing arithmetic operations.

x=654*6734

type(x)

x-int

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:

x=3.14

print(x)

type(x) #Here the type the variable is float

float

Strings are declared within a single or double quote.

x=’Simplilearn’

print(x)

x=” Simplilearn.”

print(x)

type(x)

x-simplilearn

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:

x=[14,67,9]

print(x)

type(x)

x-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:

print(x[0])

print-x

To extract the third element from the list x:

print(x[2])

Lists are mutable objects, which means you can change the values in a list once they are declared.

x[2]=70 #Reassigning the third element in the list to 70

print(x)

print-x-2

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.

x=(4,8,6)

print(x)

type(x)

print-x-3

Similar to lists, tuples can also be extracted with the index position method.

print(x[1]) #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.

tuple

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:

x=open(‘C:/Users/Simplilearn/Downloads/JupyterNotebook.ipynb’,’r’) 

type(x)

x-open

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

xyyz

The following line code results in an error because the number of values assigned doesn’t match with the number of variables declared.

value-error

If you want to assign the same value to multiple variables, use the following syntax:

x=y=z=1

xyz-1

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

    _a984_=100 #valid

    a9967$=100 #invalid

    xyz-2=100 #invalid

3. Python variable names are case sensitive.

Example: a100 is different from A100.

    a100=100

  A100=200

print-a

4. Reserved words cannot be used as variable names.

Example: break, class, try, continue, while, if

break=10

class=5

try=100

break-ten

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:

x=20

y=10

result=x+y

print(result)

x-20

Similarly, we can perform subtraction as well.

result=x-y

print(result)

result-x-y

Additionally, to perform multiplication and division, try the following lines of code:

result=x*y

print(result)

result=x/y

print(result)

result-print-result

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.

modulo

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[0]) #Gives the character at index 0, i.e. S

print(var[4]) #Gives the character at index 4, i.e. l

var-simplilearn

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.

print(var[:3])

print-sim

The following example prints the values from the fifth location until the end of the string.

print-ilearn

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.

var-simplilearn-print

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

len(var)

len-var

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:]

print(var3)

great-sunday

Don't miss out on the opportunity to become a Certified Professional with Simplilearn's Post Graduate Program in Full Stack Web Development. Enroll Today!

Conclusion

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 Post Graduate Program in Full Stack Web Development today!

About the Author

Avijeet BiswalAvijeet Biswal

Avijeet is a Senior Research Analyst at Simplilearn. Passionate about Data Analytics, Machine Learning, and Deep Learning, Avijeet is also interested in politics, cricket, and football.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.