Python Variables

python-variables-ipcisco-1

As in other programming languages, there are variables in Python programming. Python variables are the containers that we store data values in them. We can assign a python number, a list, a tuple etc. to Python variables.

 

So, how can we create variables in Python? We can do this easily by assigning a value to a variable. When we assing this value, the variable is automatically created. This value can be a number a string, a list a tuple, a set, a dictionary etc.

 

The names of the variables are case sensitive. So, the variable that you create with lower case is not the same variable that is created with upper case. As an example, a and A are two different variables in python programming.

 


You can learn also Python Lambda Function


 

 

Now, to see how to use variables in Python, let’s do a simple example in which we will assign different values to different variables.

 

a = 5
b = "John"
abc = {1,2,3,4,5}
mylist = ["x","yy","zzz"]

print(a)
print(b)
print(abc)
print(mylist)

 

The output will be:

 

5
John
{1, 2, 3, 4, 5}
['x', 'yy', 'zzz']

 

You can also watch the video of this lesson!

python-variables-ipcisco-1

 

We can change the value of a variable multiple times in our code. We can assign a value in one type and then we can change it also to another data type. For example, firstly we can assign a string value to a variable and then we can change it as list type by assigning a list.

 


You can also Download Python Cheat Sheet!


 

While we assign a value to the variables, if it is the same value, we can do this in one line. Below, you can find an example of this usage.

 

x = y = z = 100
print(x)
print(y)
print(z)

 

The output will be:

 

 

100
100
100

 

variables-of-python-ipcisco-2

 

We can also assign the values of one more variable by using comma (,).

 

a, b, list1 = 10, 20, [1,2,3]
print(a)
print(b)
print(list1)

 

10
20
[1, 2, 3]

 


 

Table of Contents

Variable Names

 

We can use different variable names in python programming. This can be one letter like a, b, x etc., one more letter or a combination of letters, digits and underscore character (_).

 

A variable name can start with a lower case, upper case or underscore character. But a python variable name can not start with a digit. Digits can only used in the other characters.

 

a = 5
Count = 200
_Nodes = ["Cisco 4000 Series", "Cisco 9000 Series"]
Device15 = "Catalyst Switch 9300"

print(a)
print(Count)
print(_Nodes)
print(Device15)

 

The output of this code will be like below:

 

5
200
['Cisco 4000 Series', 'Cisco 9000 Series']
Catalyst Switch 9300

 

python-variable-names-ipcisco-3

 


 

Type() Function

 

In Python programming, we can see the type of a variable with type() function. In other words, type() function gives us the data type. Let’s show this with an example.

 

a = 5
b = "Cisco"
abc = {1,2,3,4,5}
mylist = ["router","switch","firewall"]

print(type(a))
print(type(b))
print(type(abc))
print(type(mylist))

 

As you can see, the class of each data type is given below:

 

<class 'int'>
<class 'str'>
<class 'set'>
<class 'list'>

 

python-variable-types-ipcisco-4

 

Here, we have talked about one of the beginning lessons of python programming, Python variables. You will use variables always in your codes in different formats.

 

Back to: Python Programming Course > Python Basics

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course

Collapse
Expand