DEV Community

Cover image for A Simple Guide to Python Variables
Syed Umair Ali
Syed Umair Ali

Posted on

A Simple Guide to Python Variables

Introduction to Variables

A variable is like a container. Every variable has a value that has been assigned to it and whenever you use the variable the value that was assigned to it will actually be used. A variable's value can be changed dynamically through code and it will hold the last value that was assigned to it.

name = "John Doe"
Enter fullscreen mode Exit fullscreen mode

Python allows you to assign more than one variable in a single line by separating their names and values with commas.

name, age, gender = "John", 12, "male"
Enter fullscreen mode Exit fullscreen mode

Naming Variables

In Python, there are a few rules that need to be followed when you're working with variables so that your code is easier to read and doesn't cause unnecessary errors.

The name of a variable can only contain numbers, letters, and underscores and must start with an underscore or a letter and not a number.

If you're variable name contains two words then join them together with a underscore because spaces shouldn't be used in variable names since adding a space makes them two different words.

Python comes with some restricted keywords that are used in programming and those words can't be used as the name of a variable since Python has assigned a special function to them already.

Constant Variables

In Python, and other programming languages, constant variables are variables whose values cannot be changed once they have been declared.

Any variable whose name is written in all capital letters will be treated as a constant variable in Python.

 VALUE_OF_PI = 3.14
Enter fullscreen mode Exit fullscreen mode

Did you find this post informational? Share your thoughts in the comments and let me know where are you on your Python Journey. Don't forget to like and share if you found this helpful!

Top comments (0)