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..
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)
![What are Variables in Python? 1 variables in python](https://technicalblog.in/wp-content/uploads/2020/11/variables-in-python.jpg.webp)
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:
- The name of the variable should start with a letter or the underscore character.
- 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 _ )
- 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"
![What are Variables in Python? 2 variables in python](https://technicalblog.in/wp-content/uploads/2020/11/variables-in-python-1.jpg.webp)
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:
![What are Variables in Python? 3 variables in python 3](https://technicalblog.in/wp-content/uploads/2020/11/variables-in-python-3.jpg.webp)
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:
![What are Variables in Python? 4 variables in python](https://technicalblog.in/wp-content/uploads/2020/11/variables-in-python-2.jpg.webp)
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:
![What are Variables in Python? 5 variables in python 4](https://technicalblog.in/wp-content/uploads/2020/11/variables-in-python-4.jpg.webp)
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 🙂