Python Variables

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's 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 output of the above code is as follows:

Danny
987654
150.6
True

Dynamically Typed Programming Language

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.

Here's an example to demonstrate this:

name = "Danny"
name = 123456
print(name)

The output of the above code is as follows:

123456

How to name a variable?

Use small letters and in case of multiple words, separate words with underscore to improve readability.

Here are some good examples of variable names:

first_name = "Danny"
last_name = "Lee"
phone_number = "+1234567890"
email = "danny@tutorialsbuddy.com"
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.

Here's an example to demonstrate this:

a = "Hello"
A = 17

print(a)
print(A)

The output of the above code is as follows:

Hello
17

How to find the data type of a variable?

To find the data type of a variable, use type() built-in function of Python.

Here's an example to demonstrate this:

first_name = "Danny"
price = 107.45
is_active = True

print(type(first_name))
print(type(price))
print(type(is_active))

The output of the above code is as follows:

<class 'str'>
<class 'float'>
<class 'bool'>

To determine whether a variable is of a specific type, use isinstance() function:

Here's an example to demonstrate this:

x = "Python"
y = 10
z = True

print(isinstance(x,str))
print(isinstance(y,bool))
print(isinstance(z,bool))

The output of the above code is as follows:

True
False
True