Python 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"
print(name)
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"
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!
Recent Comments