A Variable is a placeholder that holds different data types as values. To assign a value to any variables, we use assigning operator (=) equal sign.

So, the text on the left of the equal sign is the variable, and the right side holds the variable’s value.

While creating any variables, please make sure to remember these

  1. While naming a variable, it can not have any numbers or special characters except (_) underscore as a first letter. such as 9variable = “Book”, or $Math = 500 (Incorrect).
  2. Also, the variable will never be wrapped with an inverted comma. Remembering this line will help distinguish between a string data type and a variable. Such as “Book” -> String data type while book_name is the variable.
  3. If a variable consists of two different words, it should not have space between them.

Incorrect Way to Declare Variable

# Incorrect way to select variable names

my_variable" = "This is a book"
2a = "AA"
a2@2 = 5
my Country = "Australia"
  Input In [11]
    my_variable" = "This is a book"
               ^
SyntaxError: invalid syntax

As we can see, while creating my_variable”, a syntax error occurred due to the (“) inverted comma.

Similarly, we will receive Systanx Error for variables 2a and a2@2.

Correct Way to Declare Variable

MyVariable = "Australia"
variable_2 = "Germany"
_country = "Nepal"

num1, num2 = 2 , 3 
# Declaring and assigning value to two variables at once

Variables are case-sensitive, so changes in any letter of the same word will be considered a different variable by the program.

name = "Ivan"
Name  = "Alan"
print("This name is", name , "and this is Name", Name)
This name is Ivan and this is Name Alan

Name and name are two different variables, and python is not replacing the variable with one another.

new_name = "Jane"
new_name = "Not Jane" 
print("This name is", new_name , "and this is name", new_name)
This new_namee is Not Jane and this is new_name Not Jane

In our answer, we can see that “Not Jane” has appeared twice even though our new_name variable got two values such as “Jane” and “Not Jane”.

The main reason for this behaviour is that both times we have used the same variable to assign a new value. As a result, python only refers to the latest one and replaces the other.

In this case, our new value for the “new_name” variable was “Not Jane”, which replaced the previous value “Jane”

Few more examples of variable

# Caeating a variables with string data type
Variable_1 = “Book”
_variable_2 = “Pen”

# Variable with numeric data type
year = 2006

# Varaible with two different words
my_profession  = student

Naming Convention

When writing the variable we could follow a different naming conventions.

For naming convention, we can follow 1)  UpperCamelCase or 2) snake_case.

# Camel Case
MyCountry = "UK"
CityName  = "Canberra"

# snake_case
my_country = "Australia"
city_name = "Darwin"
Word Cannot be used as a variable in python.

Python does have some reserved words/keywords which can not be used as variables.

ifelseelifandclasscontinue
TrueFalsewhilewithforbreak
orimportdefasassertlambda
Nonefromreturnglobalpasstry
exceptyieldassertnonlocalnotfinally

Checking Python Variable Type

To check the variable type, we can use the type() function.

Mostly, the variable type depends on its assigning value.

# String Variable 
variable_1 = "book"
print("variable_1 is ", type(variable_1)," variable")
variable_1 is  <class 'str'>  variable
# Integer Variable
my_num = 5
print("my_num is ", type(my_num)," variable")
my_num is  <class 'int'>  variable
# Float Variable
num_f = 5.0
print("num_f is ", type(num_f)," variable")
num_f is  <class 'float'>  variable
# Boolean Variable
logical_variable = True
print("logical_variable is ", type(logical_variable)," variable")
logical_variable is  <class 'bool'>  variable

Python Type Casting

While working in the real world, we may sometimes need to change the variable data type, and there is where python type casting comes into play.

# Before Type Casting
another_num = 10
print("another_num is ", type(another_num)," variable")
another_num is  <class 'int'>  variable
# After Type Casting
another_num = float(10)
print("another_num is ", type(another_num)," variable")
another_num is  <class 'float'>  variable

Variable type changed from ínt’ to float after applying type casting. We can also do the same to convert a float number into an integer or string.