Python Delete Variable

python-delete-variable

What is a Variable in Python?

A Python variable is a held memory area to store esteems. As such, a variable in a python program offers data to the PC for preparing.

Python Variable Types

Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.

In this tutorial, we will learn,

  • Instructions to Declare and utilize a Variable
  • Re-proclaim a Variable
  • Link Variables
  • Neighborhood and Global Variables
  • Erase a variable

 

How to Declare and use a Variable

Let see an example. We will define variable in Python and declare it as “a” and print it.

a=100 
print (a)

Re-declare a Variable

You can re-proclaim Python factors even after you have announced once

Here we have Python pronounce variable instated to f=0.

Afterward, we re-dole out the variable f to esteem “guru99”

Variables in Python

Python 2 Example

# Declare a variable and initialize it
f = 0
print f
# re-declaring the variable works
f = 'guru99'
print f

Python 3 Example

# Declare a variable and initialize it
f = 0
print(f)
# re-declaring the variable works
f = 'guru99'
print(f)

Python String Concatenation and Variable

We should see whether you can link distinctive data types like string and number together. For instance, we will connect “Master” with the number “99”.

In contrast to Java, which links number with string without pronouncing number as string, while at the same time announcing factors in Python requires proclaiming the number as string else it will show a TypeError

Variables in Python

For the following code, you will get undefined output –

a="Guru"
b = 99
print a+b

Once the integer is declared as string, it can concatenate both “Guru” + str(“99”)= “Guru99” in the output.

a="Guru"
b = 99
print(a+str(b))

Python Variable Types: Local & Global

There are two kinds of factors in Python, Global variable and Local variable. At the point when you need to utilize a similar variable for rest of your program or module you announce it as a global variable, while on the off chance that you need to utilize the variable in a particular capacity or technique, you utilize a nearby factor while Python variable affirmation.

How about we comprehend this Python variable sorts with the contrast among nearby and global factors in the underneath program.

  1. Allow us to characterize variable in Python where the variable “f” is global in scope and is appointed worth 101 which is imprinted in yield
  2. Variable f is again announced in capacity and assumes local scope. It is allocated esteem “I am learning Python.” which is printed out as a yield. This Python proclaim variable is not quite the same as the global variable “f” characterized prior
  3. When the capacity bring is finished, the neighborhood variable f is devastated. At line 12, when we once more, print the estimation of “f” is it shows the estimation of global variable f=101

 

Python 2 Example

# Declare a variable and initialize it
f = 101
print f
# Global vs. local variables in functions
def someFunction():
# global f
    f = 'I am learning Python'
    print f
someFunction()
print f

Python 3 Example

# Declare a variable and initialize it
f = 101
print(f)
# Global vs. local variables in functions
def someFunction():
# global f
    f = 'I am learning Python'
    print(f)
someFunction()
print(f)

While Python variable assertion utilizing the keyword global, you can reference the global variable inside a capacity.

  1. Variable “f” is global in scope and is appointed worth 101 which is imprinted in yield
  2. Variable f is proclaimed utilizing the keyword global. This is NOT a local variable, yet a similar global variable announced before. Subsequently when we print its worth, the yield is 101
  3. We changed the estimation of “f” inside the capacity. When the capacity bring is finished, the changed estimation of the variable “f” perseveres. At line 12, when we once more, print the estimation of “f” is it shows the worth “changing global variable”

Variables in Python

Python 2 Example

f = 101;
print f
# Global vs.local variables in functions
def someFunction():
  global f
  print f
  f = "changing global variable"
someFunction()
print f

Python 3 Example

f = 101;
print(f)
# Global vs.local variables in functions
def someFunction():
  global f
  print(f)
  f = "changing global variable"
someFunction()
print(f)

Delete a variable

You can likewise erase Python factors utilizing the command del “variable name”.

In the beneath illustration of Python erase variable, we erased variable f, and when we continue to print it, we get mistake “variable name isn’t characterized” which implies you have erased the variable.

Variables in Python

Example of Python delete variable or Python clear variable :

f = 11;
print(f)
del f
print(f)

Summary:

  • Factors are alluded to “encompass” or “containers” where information can be kept up and referred to. Like some other programming language Python likewise utilizes a variable to store the information.
  • Factors can be announced by any name or even letter sets like a, aa, abc, and so on
  • Factors can be re-announced even after you have proclaimed them for once
  • In Python you can’t link string with number straightforwardly, you need to pronounce them as a different variable, and from that point onward, you can connect number with string
  • Python constants can be perceived as sorts of factors that hold the worth which can not be changed. Normally Python constants are referred to from different records. Python characterize steady is proclaimed in another or separate document which contains capacities, modules, and so forth
  • Kinds of factors in Python or Python variable sorts : Local and Global
  • Pronounce neighborhood variable when you need to utilize it for current capacity
  • Announce Global variable when you need to utilize a similar variable for rest of the program
  • To erase a variable, it utilizes watchword “del”.