A variable is a name which is used to reserve space in memory for storing values.
A variable in Python is created when we assign a value to it.
Here is an example where name, contact_number, amount, and status are variables in which we can store values.
name = "Danny" contact_number = 987654 amount = 150.60 status = True print(name) print(contact_number) print(amount) print(status)
The above example gives the following output:
Python is dynamically typed programming language, meaning we do not need to declare the data type of a variable. The type of a variable can also be changed by reassigning a new value of another type to already declared variable.
name = "Danny" name = 987654 print(name)
How to name a variable?
Use small letters and in case of multiple words, separate words with underscore to improve readability.
first_name = "Danny" last_name = "Lee" phone_number = "+1234567890" email = "[email protected]" designation = "Python Engineer" price = 107.45 is_active = True
Variables are Case-Sensitive
In Python, variables are case-sensitive. A variable name a and A are not same but two different variables.
a = "Hello" A = 17 print(a) print(A)
How to find the data type of a variable?
To find the data type of a variable, use type() built-in function.
first_name = "Danny" price = 107.45 is_active = True print(type(first_name)) print(type(price)) print(type(is_active))
To determine whether a variable is of a specific type, use isinstance() function:
x = "Python" y = 10 z = True print(isinstance(x,str)) print(isinstance(y,bool)) print(isinstance(z,bool))