What are Variables in Python?

Hello mates, In this section, We are going to discuss the Variables in Python. Like other programming languages python variables also have some rules and regulations which will we are going to discuss.

Lets start..

Table of Content:
Creating Variables in Python
Re- declaration of Variables:
Python Variable Names
Local Variable and Global Variable
How to delete a variable?
How to Assign Value to Multiple Variables in Python?
FAQs

Creating Variables in Python

As I am considering, you are familiar with variables. Variables are like a container which holds the values.

In Python, We don’t need to create or declare a variable with data types. You can simply create the variable at the moment you first assign a value to it. For example:

x = 6
y = "Harry"
print(x)
print(y)

Here, you can see that both variables have different values like x has int type and y have string type, but it clearly shows that we do not need to declare the variable with any specific data types.

Re- declaration of Variables:

In Python, a Variable can be re-declared, but one thing always you should remember that a variable name is case-sensitive. Also, we can re-declare the variable from one type to another type.

For instance, if I declared a variable and assigning the value in number type(Integer, Float, or complex number) when we are trying to re-declare it in string type.

# Declare a variable and initializing the value
A = 0
print(A)
# re-declaring the variable
A = 'technicalblog'
print(A)
variables in python
Output of the above mentioned code

Python Variable Names

The variable can have a short name (like K and J ) or a more descriptive name (Age, surname, total_volume). Rules for Python variables:

  1. The name of the variable should start with a letter or the underscore character.
  2. A Python variable name can not start with numeric values, and it can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  3. The Variable names are case-sensitive (Age, Age, and AGE are three different variables). Capital and small letter are matter here.
#Correct format:
myvar = "Harry"
my_var = "Harry"
_my_var = "Harry"
myVar = "Harry"
MYVAR = "Harry"
myvar2 = "Harry"

# wrong format
2myvar = "Harry"
my-var = "Harry"
my var = "Harry"
variables in python

Local Variable and Global Variable

In Python, there are two types of variables- Local and Global variables. When we want to use the same variable for the whole program or module that is a global variable, while if we use the variable in a specific function or method, that is a local.

For Example:

# Declare a variable 
x = 100
print(x)
# Global vs. local variables in functions
def MyFunction():
# global x
    x = 'Local and Gloabal variables in Python'
    print(x)
MyFunction()
print(x)
Output:

How to delete a variable?

We can delete a Python variable by using the command del “variable name”.

In the below-mentioned example, we deleted a variable X, and when we proceed to print it, we are getting an error “variable name is not defined” which means that the variable has been deleted the variable.

X = 15;
print(X)
del X
print(x)

Output:

variables in python

How to Assign Value to Multiple Variables in Python?

Python allow us to assign value to multiple variables in one line.

x, y, z = “Apple”, “Banana”, “Orange”
print(x)
print(y)
print(z)

Output:

FAQs:

An empty list could be created with a list() alone. True or False

True

Values in byte array should be integers between ______.

0 <= x < 256

Using Pop in the list will remove the popped up item.

POP

Also Read:


In the above, we have successfully presented the Variables in Python, How to delete, assign, and so many things with suitable examples and Screenshot of output.

I hope you like this article, for any query, please contact us or comment on the comment section.

Thank You 🙂

4 thoughts on “What are Variables in Python?

Leave a Reply

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