Python Variable Names

Written by Peter Steele

August 7, 2020

Categories: Python
Python Tutorial Variable Names

Introduction

 

Python is a powerful programming language, and here at Learn Code Today, it is also one of our favorite to work with! Over the years, Python has jumped to the top of the pack for programming languages over competitors because of its versatility and ease of use. This language can be used to create scripts, to create games, machine learning, AI, and so much more. It is a loosely typed language, meaning that you do not need to declare variable types as the interpreter figures that out for you, making it an excellent choice for beginners to pick up.

In today’s tutorial, we are going to talk about variables. A mentioned in some of the starting tutorials, variables are like little boxes that hold some information in them, with a human readable name, that we can call at anytime for use! How cool is that? So you want to store someone’s name and recall it late? Easy in Python!

 

How to Declare a variable

To declare a variable in Python, all you need to do, is to give the variable a name and set it equal to some content. See the example below:

name = "Peter"
Pretty easy right? Name anytime that we want to use this name, say to print it to the screen, we just call the variable up and it will output that for us!
print(name)
expected output:
Peter

You can also create multiple variables at once by separating them with commas:

x, y, z = 1, 2, 3

In this example, x has the value 1, y has the value 2, and z has the value 3.

So that’s easy enough to understand, but what else can variables hold in them? Everything just about! You can store numbers, or as they are called in programming terms, integers. You can also store floating point integers (decimals), references to objects, output from a method or function, boolean values, entire functions, etc.

 

Naming Variables – more important than you think

So with variables, comes great responsibility though. They can easily be overwritten and named poorly making code very hard to read and understand. It’s best to name your variables something meaningful so that just reading the name explains what it is holding. For example, while this is perfectly valid:

x = "Peter"
Trying to figure out what “x” is several hundred or thousands of codes lines down is going to be near impossible. It’s best to name it something more in line like:
name = "Peter"
user = "Peter"
username = "Peter"

As you can see, the variable name is much easier to understand what is contained inside of it.

 

Naming Gotchas

The last topic I will cover with naming variables is that capitalization does matter in Python. Peter, peter, PeTeR, PETER, are all valid different variables. Please be careful when naming your variables as to not cause confusion. Find a naming convention and stick to it. There are two main naming conventions that are used today in most programming languages. camelCase and PascalCase. Lets see what this looks like in practice.

ageInYears = 20
HeightInCentimeters = 177.8

As you can see, with the first variable name, we keep the first word lowercase and uppercase each word afterwards (This is called camelCase). For PascalCase, every word is capitalized from the get go. In Python, you can also use underscores and hyphens ( – ), but I would recommend one of the two that I have shown here since it is used in other languages more commonly.

While PEP 8, the Python guidelines for styling code, suggests using all lowercase names with underscores, if you ever plan to use another language such as C#, you will not find this convention very often and it can lead to confusion. If you are only going to be using Python, then stick to the PEP 8 guidelines and use all lowercase with underscores like so:

height_in_inches = 70

If you are only going to be using Python, then stick to the PEP 8 guidelines and use all lowercase variable names with underscores

Globally Reserved Names and other tips

I know I said this would be the last topic, but one more quick note. Some names are reserved for Global Variables and cannot and should not be used for your programs.

 

33 Python Reserved Words not allowed for variables

False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass

 

 

 

 

 

 

 

 

 

 

 

 

Also avoid using lowercase l (el) or uppercase O (oh) as single named variables as in some fonts, they are indistinguisable from 1 (One) and 0 (Zero).

 

Conclusion

In conclusion, Python variables are a fundamental concept in programming. They are used to store and manipulate data in a program. In Python, variables are created by assigning a value to a name. Good variable naming conventions and following PEP8 rules can make your code more readable and easier to understand. Remember to use descriptive variable names, avoid using globally reserved names, and use all lowercase letters for variable names to avoid confusion.

By understanding the basics of Python variables and following these best practices, you can write more efficient, readable, and maintainable code. Happy coding!

Related Articles

Install Python on Ubuntu 22.10

Install Python on Ubuntu 22.10

Ever wanted to know how to install Python on your ubuntu system? Well now you can learn the easy and simple way to install any version of Python along with how to remove any versions you may not be using, and how to run Python in the terminal. Check it out today!

Why Python?

Why Python?

Introduction Python is one of the most popular programming languages in the world, with a vibrant community of developers and a wide range of applications. It's also an excellent language for beginners who are just starting to learn programming. In this blog post,...

Installing Python on Windows 10/11

Installing Python on Windows 10/11

In this tutorial, we will cover how to properly install Python on Windows 10/11 and how to make sure Python is included in your path. We also cover how to use Python with it’s IDLE and inside of the command prompt.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Peter Steele

Peter Steele

Software Engineer/Site Owner

I am a father of 2, happily married and I also love to game! I started this site as a way to pass on what I have learned as a software engineer. Hopefully I can pass on some knowledge to those seeking it and make someone's life just a little better. Happy coding!

Pin It on Pinterest