Pt. 3 Python Variables
Python variables | Learning Python Pt. 3
Assigning, naming and working with variables in Python along with some best practices and naming conventions
In this part of the "Learning Python" series, we're going to be exploring and learning about variables.
Let's quickly define what a variable is and then move onto some examples.
What is a Python variable
Variables are used to store values and can almost be thought of as a placeholder or reference to another value.
We assign values to variables using the =
operator.
You'll often see a variable in Python defined like the following:
Where language
is the variable name and "Python"
is the value.
Let's create some variables and assign them different types of values:
Variable assignment
A variable can be assigned with different types of values.
Strings:
Whole numbers (Integers):
Floating point values (Floats):
Lists:
Dictionaries:
Booleans:
And much more.
We're going to be covering integers, floats, list, dictionaries, booleans & more in future parts of this series.
Reusing variables
Once a variable has been created, we can reuse it throughout our code, for example, let's create a variable with a string value and reuse it 5 times using the print
function:
If we run this code from the terminal, we get the following output:
Multiple assignment
We can create and assign multiple variables with values all on the same line:
If we then passed these 3 variables to the print function:
We get the following output:
Variable reassignment
In Python, you can reassign an existing variable with a new value, just as if you were creating a new variable:
You can also assign a variable with the value of another variable:
We can also swicth variable values in place!
Value unpacking
Although we're yet to cover lists, tuples or sets in this series (3 types of Python data containers), we can unpack values from containers into variables:
Variable naming
You can name a variable anything you like as long as you follow these rules.
Variable names must begin with a letter or underscore:
Variable names must only contain alpha-numeric characters and underscores (No spaces):
I.e ! " £ $ % ^ & * ( ) _ + =
etc.. are not allowed.
Variable names are case sensitive:
Variable names must not use Python reserved keywords (Special reserved Python words like print
list
, class
, def
, in
, not
etc). You'll learn more of these special names as you progress through the series:
Naming best practices
Use the following bext practices when it comes to naming variables.
Separate words with underscores (AKA Snake case):
Be descriptive (Explicit is better than implicit!):
Use all uppercase for constants (Values that should't be changed or reassigned) again with words separated with underscores:
Naming conventions
Snake case is widely accepted as the "de facto" way to name variables and I'd recommend you make a habit of it!
Having said that, there will be times when you come across the following naming conventions:
Camel case (Uppercase words apart from the first) - often found in older Python code.
Studly Caps / Pascal Case (Uppercase words) - Used for naming Python classes.
Studly Caps is the formal convention for naming Python classes (Which we'll cover soon), so be sure to stick to it when we get to creating classes.
A few more variable naming conventions include
Naming a variable with a single leading underscore - Used to hint that the variable is private:
Single underscore variables are typically found in Python classes so we won't go into detail on it now.
Naming a variable with a trailing underscore:
Typically used when one of Python's reserved keywords (described above) is the only suitable name for your variable or you have a second variable which needs a very similar name to one already defined.
And finally, a couple of things you should avoid
Capitalizing & underscores combined:
Using double underscores around variable names (Used for "Dunder" or "Magic" methods which you'll learn about soon):
Deleting variables
Variables can be deleted using the del
keyword, followed by the variable:
You probably won't be deleting variables very often, but it's there if you need it.
Wrapping up
Variables play a key part in Python and almost every other programming language.
Be sure to stick to the naming conventions and follow the best practices, you'll appreciate it later down the line!
As a challange, go ahead and create some variables of your own and use the print
function to print the output to your terminal!
Last modified · 14 Mar 2019 Reference : https://pythonise.com/series/learning-python/python-variables
Last updated