How to use Variables in Python 3?

Variables are data storage that stores provided data to specified variables. In python you can declare a variable without any symbol, for example in PHP you need a dollar sign ($) to declare a variable.

If you haven’t installed python yet, check out our article that will help you install python 3 on windows.

Create Variable

To create a variable, you just need to define the variable name and assign a value.

yourName = "John Smith"
print(yourName)
# Output: John Smith

In the first line of the above script, we have a variable yourName along with the assigned value “John Smith”. If you notice, you will find the first letter lower in the variable because of the naming convention. We used a camel case.

Variable Type

Python changes the type itself, you don’t need any function to do that. Suppose we have a variable yourPoints that has a numeric value 30

yourPoints = 30
print(yourPoints)
# Output: 30
print(type(yourPoints))
# Output: <class 'int'>

In the above script, python sets variable type integer because it finds a numeric value without any quotes. It is called implicit type casting. The type() function returns the type of object or variable.

Now let’s add quotes on 30 and then check the result.

yourPoints = "30"
print(yourPoints)
# Output: 30
print(type(yourPoints))
# Output: <class 'str'>

In this case, we have different results, the variable type changed to string as you can see in the output. It is because python found value with quotes.

Conclusion

This article helps to understand variables in the python programming languages with examples. If you would like to learn more about it then check out our Python page.