Variables in Python

In any program, you need to store and manipulate data to create a flow or some specific logic.

That’s what variables are for.

You can have a variable to store a name, another one to store the age of a person, or even use a more complex type to store all of this at once like a dictionary.

Creating also known as Declaring

Declaring a variable is a basic and straightforward operation in Python

Just pick a name and attribute a value to it use the = symbol.

name="Bob"
age=32

You can use the print() function to show the value of a variable.

print(name)
#Bob
print(age)
#32

Notice that in Python there is no special word to declare a variable.

The moment you assign a value, the variable is created in memory.

Python also has dynamic typing, which means you don’t have to tell it if your variable is a text or a number, for instance.

The interpreter infers the typing based on the value assigned.

If you need it, you can also re-declare a variable just by changing its value.

#declaring name as a string
name="Bob"
#re-declaring name as an int
name = 32

Keep in my mind though, this is not recommended, variables must have meaning and context.

If I have a variable called name I don’t expect it to have a number stored in it.

Naming Convention

Continuing from the last section when I talking about meaning and context.

Don’t use random variable names like x or y.

You want to store the party time, just call it party_time.

Oh, did you notice the underscore _?

By convention, if you want to use a variable name that is composed of two or more words, you separate them by underscores. This is called Snake Case.

Another option would be using CamelCase as in partyTime, this is very common in other languages, but not the convention in Python as stated before.

One last detail, variables are case sensitive, so party_time and Party_time are not the same thing, and the convention tells us to always use lower case.

Remember, use names that you can recall inside your program easily, bad naming can cost you a lot of time and cause a number of bugs.

In summary, variable names:

  • Are Case sensitive: time and TIME are not the same
  • Have to start with an underscore _ or a letter, DO NOT start with a number
  • Are allowed to have only numbers, letters and underscores. No special characters like: #, $, &, @, etc.

This, for instance, is not allowed: party#time, 10partytime.

Types

As I said before, Python has dynamic typing, but you can also state the variable type explicitly like this:

#name is a string
name=str("Bob")
#age is an int
age=int(32)

I have a specific and detailed post on Python Data Types, check it out.

Concatenate

You can concatenate variables to produce some meaningful output with the + sign.

name="Bob"
print("My name is " + name)
#My name is Bob

To concatenate a non-string, just convert it using the str() function.

In this example, age is being converted to a string.

name="Bob"
age = 32
print("My name is " + name + " and I am " + str(age))
#My name is Bob and I am 32

Delete a variable

It is also possible to delete a variable.

This is not something you should worry about in most cases.

If you are dealing with, say images or videos, it might be a good idea to free some memory if you are not planning on using that variable anymore.

name="Bob""

del name
print(name)

This should show you an error since we are trying to print a variable that doesn’t exist.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined