Python Variables

In python, variables are the named locations in the memory to store the data values. The variables will act as a container to hold the data values that can be changed any time based on the defined conditions in the applications.

Python Variables Declaration

In python, to declare the variable assign value to it without mentioning any type, unlike other programming languages. As Python is a dynamically typed language, the python interpreter will automatically set the variable type based on the assigned value.

 

Following is the example of declaring and assigning value to the variables in python.

 

a = 100

b = "Welcome to Tutlane"

print(a) #100

print(b) #Welcome to Tutlane

If you observe the above example, we created two variables (a, b) without specifying the variable type. As python is a type-inferred language, it will automatically decide the type based on the assigned value.

Python Change Variables Type

In python, the variables are not bound to any specific type. So, you can change the type of variables even after they set.

 

Following is the example of changing the type of a variable in python by assigning the different values.

 

a = 100

print(type(a)) //#<class 'int'>

a = "Welcome to Tutlane"

print(type(a)) //#<class 'str'>

If you observe the above example first, we created a variable (a) by assigning an integer (100) value, and the data type of a variable is int. Afterward, we used the same variable to store string (Welcome to Tutlane) value, and the data type of variable is changed to the string.

 

As discussed, Python is a dynamically-typed language, and the type checking will happen at runtime. So, the type of variables will decide during the runtime based on the assigned value.

Rules to Define Variables in Python

While defining the variables in a python programming language, you need to follow specific naming convention rules.

 

  1. You can define the variables with a combination of alphabets (a-z, A-Z), numbers (0-9), and underscore (_). For example, the names abc, a2b, _abc, Abc are valid ways to define the variables.
  2. The variable must always start with either alphabet or underscore but not with the numbers. For example, the name a2b is valid, but 2ab is invalid.
  3. You should not use any reserved keywords in the variable name. For example, global = 20 is invalid.
  4. The white space or special characters such as @, !, #, %, -, etc. are not allowed to use in variable names. For example, a b c, a@b are invalid variables.
  5. In python, the variable names are case-sensitive, which means the role, Role, ROLE, and roLE are treated as different variable names.

The following are some valid ways to define the variable names in the python programming language.

 

# Valid variable names

abc = 100

a2b = "Hi"

_abc = 'Welcome'

Abc = 30.5

ABC = "Test"

The following are some invalid ways to define the variable names in the python programming language.

 

# Invalid variable names

2ab = 20

a b c = "Test"

a-bc = 30.5

global = 20

Python Assign Multiple Values to Multiple Variables

In python, you can assign multiples values to multiple variables in a single line like as shown below.

 

a, b, c = 10, 20.5, "Welcome"

print(a) #10

print(b) #20.5

print(c) #Welcome

If you observe the above example, we defined multiple variables (a, b, c) with various values in a single line.

 

In python, you can also assign a single value to multiple variables at once by defining it in a single line like as shown below.

 

a = b = c = 10

print(a) #10

print(b) #10

print(c) #10

If you observe the above example, we assigned the same value (10) to multiple variables (a, b, c) in a single line.

Python Variables Output

Generally, we will use a print statement with variables to print the required output. In print statement, the + character is useful to combine the required variables and text.

 

Following is the example of using + character in print statement to combine the required variables and text.

 

#using + character to combine variables

a = "welcome"

b = " to tutlane"

print(a + b) #welcome to tutlane

 

c = 10

d = 20

print(c + d) #30

If you observe the above example, we used + character in a print statement to add one variable to another. If two variables are string type, then the + character will combine the text, and if two variables are number type, then the + character will act as a mathematical operator.

 

If we use the + character to combine text and variables, it will work fine, but if we use the + symbol to combine the variables of different types (For example, string type and number type) we will get an error.

 

Following is the example of combining the text and variable.

 

#combine text and variable

name = "Tutlane"

print("Welcome to " + name) #Welcome to Tutlane

Following is the example of combining the string and number datatype variables.

#combine string and number type variables

a = 10

b = "welcome"

print (a + b) #TypeError: unsupported operand type(s) for +: 'int' and 'str'

This is how we can print the required output using + character in python print statement.

Python Variables Scope

In python, the access of variables inside the program will vary based on the defined scope of variables. In python, you can define two types of variables, i.e., local variables and global variables.

Python Local Variables

The scope of python local variables restricted to the function where we defined. We can't access the variable outside of the function.

 

Following is the example of defining the local variables in python.

 

def greeting():

    greet = "Welcome to Tutlane"

 

print(greet)

If you observe the above example, we defined the greet variable inside the greeting() function, but we are trying to access the greet variable outside of the defined function.

 

When you execute the above program, you will get an error like as shown below.

 

Python local variables scope example result

Python Global Variables

To access variables anywhere in the program, you need to create variables outside of the function, and those will be called global variables in python.

 

Following is the example of creating the global variables in python.

 

msg = "welcome to tutlane"

def greeting():

    print(msg)

           

greeting() #welcome to tutlane

If you observe the above example, we defined the global variable msg outside of the function and accessing the same variable inside the function.

 

In python, if you create a variable inside of function with the same global variable name, that variable scope is limited to that function only. The global variable will remain the same as its original value.

 

Following is the example of creating the variable inside of function with the same global variable name in python.

 

msg = "Welcome to Tutlane"

def greeting():

  msg = "Learn Python"

  print(msg)      

greeting()

print(msg)

If you observe the above example, we created a variable (msg) inside the greeting function with the same global variable name (msg).

 

When we execute the above program, we will get the result as shown below.

 

Learn Python

Welcome to Tutlane

If you observe the above result, the scope of the variable that we defined inside of function with the same global variable name is limited to within the function.

Python global Keyword

If you want to declare a variable inside the function (local variable), but want to use it outside of the function, you need to define the variable with the global keyword.

 

Following is the example of creating the global variable inside the function using the global keyword in python.

 

def greeting():

  global msg

  msg = "Learn Python"

greeting()

print("Welcome, "+ msg) #Welcome, Learn Python

If you observe the above example, we created a global variable (msg) inside the greeting() function using a global keyword and we are accessing the same variable outside of the function.

 

The global keyword is also useful to make the changes to global variables inside of the function.

 

Following is the example of changing the value of the global variable inside of function in python.

 

msg = "Welcome to Tutlane"

def greeting():

  global msg

  msg = "Learn Python"

greeting()

print(msg)

If you observe the above example, we modify the value of the global variable (msg) inside the function by using a global keyword in python.

 

When we execute the above program, we will get the result as shown below.

 

Learn Python

If you observe the above result, the global variable (msg) value has been updated with the changes we made in the greeting function.

 

This is how we can use variables in python to store the data values in memory and perform required operations based on our requirements.