Skip to content

Python Variables

Python Variable

Python variable is a type of container in which a value is stored. When we create a variable, Python reserves memory depending on the type of the variable.

Why variable is required ?

When we take any input from the user, we do not know in advance what the user’s input will be. Also, if there is more than one user, each user may have a specific information. So, we use variables to solve these problems.

Suppose we want to take the age as input from a user. But we can’t say in advance how old the user will be. So we will create a variable and store the age of the user in it and later we will use that variable as the age of the user.

How to create a variable name in Python ?

Python has some rules for naming a variable, they are –

  • The variable name must always start with a letter or underscore (“_”) .
  • The name of a variable will not contain any letters other than a-z, A-Z, 0-9 and _ .
  • The name of a variable can never start with a number .
  • In Python, the name of the variable is always case-sensitive (e.g. – var, VAR, Var are three different variables) .
  • No keywords in Python should be used as variable name .

Example 1:

The following example mentions some legal variable names –

myVarName = "Rajkumar" 
my_var_name = "Rajkumar" 
_my_var_name = "Rajkumar" 
myVar1Name = "Rajkumar"

Example 2:

The following example mentions some illegal variable names –

1myVarName = "Rajkumar" 
# variable name can't start with number 
my$var$name = "Rajkumar" 
# variable name can only contain a-z,A-Z,0-9,_

How to declare a Python variable ?

In Python, whenever we assign a value to the name of a variable, a variable is created with that name.

Example 1:

# declaring a variable x with value 10 
x = 10 
print(x)
10

In the above code, when we assign 10 values to x, a variable named x is created. Then we print x which prints 10 at the output.

How to know type of a Python variable ?

Python is a dynamically typed language, so there is no need to specify the type of variable at the time of variable declaration . Type of variable depends on the type of value assigned to that variable .

In the above example we have declared a variable named x whose value is 10. Since the datatype of 10 is “int”, the datatype of the x variable will automatically become “int”.

In Python we can check the data type of any variable using type() function.

Example 1:

print(type(x))
<class 'int'>

How to redeclare a variable in Python ?

In Python, redeclaring a variable means changing the value of a variable already declared. In this case only the value of the variable can be changed or both the value and type can be changed.

Example 1:

x=15 
print(x) 
print(type(x))
15
<class 'int'>

In the above code we have assigned 15 with the variable x. Then we print the x variable and its type.

In this case, the value of the variable x changes but the type remains unchanged.

Example 2:

x="Rajkumar" 
print(x) 
print(type(x))
Rajkumar
<class 'str'>

In the above code we have assigned Rajkumar with the variable x. Then we print the x variable and its type.

In this case, both the value and type of the x variable have changed.

How to declare multiple variables with a single value ?

In Python we can declare multiple variables at the same time, each with the same value.

Example 1:

x=y=z=10 
print(x) 
print(y) 
print(z)
10
10
10

In the above code, we have declared three variables x, y and z simultaneously, each of which has a value of 10.

How to declare multiple variables with multiple values ?

In Python, we can declare multiple variables at the same time, each of which may have different values and types.

Example 1:

x , y , z = 10 , 10.1 , "Rajkumar" 
print(x) 
print(type(x)) 
print(y) 
print(type(y)) 
print(z) 
print(type(z))
10
<class 'int'>
10.1
<class 'float'>
Rajkumar
<class 'str'>

In the above code, we have declared three variables x, y and z at the same time, each of which has different values and types.

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

<< Previous

Python Comments

Leave a Reply