Python Variables


Python variables are bucket to save data.

Python variable name rules :-

1. Python variables, Must start with a letter or underscore. ( A-z, _ )
2. Python variables, Can contain letters, numbers, underscores. ( A-z, 0-9 _ )
3. Python variables are Case sensitive. Meaning variable A and a is different
4. Python variables, Can't be a reserved keywords.
5. Python variables shouldn't have spaces or special characters.

Example :-

_variable = 5
my_variable = "I love python"
variable = 10
Variable = 20

# SyntaxError: can't assign to keyword
#class = "Python"

# SyntaxError: invalid syntax
# my variable = 5  


print(_variable)
print(my_variable)
print(variable)
print(Variable)


Creating python variables :-

In python you can easily create variables, just shown like below program.

a = 10
print(a)

Here we used alphabetical letter a to store arithmetic value, 5


Python variable re-assign :-

when you declare a variable, you don't have to assign any Data types, its a interchangeable, lets see example

x = 10
x = False
print(x)

In this example, we declare a for numeric value, but as program progressed, we assigned another Boolean (True, False) value to variable x.

if you print the value you will see x value as False.


Python variable type :-

Since we have re-assigned value in last example lets use it to check how variable data type changes, in program.

x = 10
print(type(x))

x = False
print(type(x))

This will let you see how python internally changing type of variable.

Here we have used in-build method called type(argument), don't worry we have separate lesson to learn python built-in types.


What is Type?

Type mean stored value/data is of string, integer, boolean, decimal or any other.


Python variables are case sensitive :-

Python knows the difference between lower-case and UPPER-CASE letters,

x = 10
print(x)

X = False
print(x)

This time python will not re-assign variables. values will be unchanged.