What are python Variables?

Variables are containers for data that can change. They can be a string of text, a number, or even an object. A variable is defined by giving it a name and assigning it a value. Variables are considered data types in Python programming language that can hold different values and types of information as long as they have been assigned to the variable

Python Variables are used to store values or data. They can be assigned a value when they are created and can be changed later as well.

There are two types of variables in Python:

Global Variables

Global variables are variables that can be accessed from anywhere in the program. They are declared outside of any function and are available to all functions.

x = "awesome"

def myfunc():
  print("Python is " + x)


In this example, the variable x is declared as a global variable. This means that it can be accessed from anywhere in your program.

Local Variables

These variables are only available inside the function where they were created.

def myfunc():
  x = "fantastic"
  print("Python is " + x)


The computer stores these variables in memory. This means that if you have a variable with the name “a,” and you create another variable with the same name, it will overwrite the first one that was stored in memory.

Rules When naming a Variable

  • Python variable names should be descriptive, but they cannot start with a number.
  • Variable names can only contain letters, numbers, and underscores.
  • Variable names cannot start with a number.

How to Assign Values to Variables in Python?

Variables are used to store information and data in a program. Variables can be assigned values by using the = operator. The assignment operator is the equal sign, which assigns the value on its right side to the variable on its left side. For example:

x = 5
y = 10
z = x + y

# prints 15

How to Declare a Variable in Python?

In Python, you do not need to declare variables before using them. However, declaring a variable is useful for defining the scope of the variable.

In Python, variables are created when you assign a value to them. For example:

a = 10
b = 10
c = a+b
print (c)

The output will be 20.

This code will create the variable (a) and automatically set its type to int (integer).

Note: String variables can be declared using either single or double quotes.

x = “Hello Dolly”

is the same as

x = 'Hello Dolly'

Assign Multiple variables in python

In Python, you can assign multiple variables at the same time. This is useful when extracting multiple values from a data structure.

For example:

x, y = 10, 20

You can extract both items by assigning them to two variables:

x, y = y, x

In Python, we can use the type() function to find out the data type of any variable. It can be used on any object in Python including integers, floats, strings, and lists.

The following are some examples of how to use this function:

a = ('apple', 'orange', 'banana')
b = "Hello Dolly"
c = 11
x = type(a)
y = type(b)
z = type(c)

Delete Variable in Python

The del keyword is used to delete variables in Python.

The del keyword is used to delete variables in Python. It can be used with a variable name or an expression that evaluates a variable.

x = "hello dolly"
del x