Working with Variables in Python

Published on July 29, 2017 by

So far, we have only been telling Python explicitly what to print. When you code applications, games — or whatever the case might be — you need to have the ability to store and work with different types of data. We do so by using variables. Consider the following, for example.

greeting = "Hello World!"

The first line creates a new variable called greeting and assigns it with the text “Hello World!”. So the variable greeting now stores the value “Hello World!”. The second line instructs Python to print the value of the variable called greeting. Python checks in the memory of the computer and sees that the variable greeting corresponds to the text “Hello World!”, hence why Python prints out that text. Finally, the last line instructs Python to print the text “greeting”. Notice that the difference here is that Python prints the actual text instead of the variable since greeting has quotation marks around it, making it a string and not a variable. Note that you are free to choose between using double-quotes or single-quotes when adding strings; that’s just a matter of which you prefer, as the Python interpreter understands both just fine.

Running the above code above, you should see the following output.

Note that I added the code to a file named as discussed in the previous post. The output looks slightly different, because I am using the PyCharm IDE, which I will be using throughout this Python tutorial.

Reassigning Values to Variables

Now that you know how to use variables to store values, what if you want to change the value of variables? You can simply assign a new value to a variable, exactly as you would when initializing a variable for the first time, such as in this example:

greeting = "Hello World!"
greeting = "Hello there!"

What happens in the above code, is that the greeting variable is initialized with a value of “Hello World!”. The second line then reassigns the variable to a new value of “Hello there!”. This means that the original value of the variable has now been overwritten and lost. Below you can see the result of running the code.

Of course it doesn’t make sense to declare a variable only to reassign (or overwrite) it immediately, so the above code is just for the sake of providing a simple example.

Variable Rules and Naming Conventions

There are a couple of rules in regards to variable names. First of all, variables must begin with either a letter or an underscore (_) and may therefore not begin with numbers. The remainder of the variable name must either consist of numbers, letters or underscores (_). Spaces are not allowed. It’s also worth noting that variable names are case sensitive, so myvar is not the same as myVar.

If you are familiar with other programming languages, then you might have noticed that most languages use so-called camelCase for variable names by convention. This is not the convention in Python; instead, so-called snake case is used, meaning that instead of uppercasing characters, words are divided by underscores. A variable in camel case could be named myVar; the equivalent of this in snake case would be my_var, which is the preferred case used in Python.

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.