Skip to main content
  1. Courses/
  2. Python/

Variables

·548 words·3 mins· loading · loading ·
Russell Chubb
Author
Russell Chubb
Working at the intersection of Technology and Art.

Variables
#

Variables in Python are like containers that store data values. You can think of them as labels that you attach pieces of information to.

To create a variable in Python, choose a name for it, and use the equals sign to assign a value to it. In this example, I’ve created four variables: age, name, height, and is_lit. The variable age is assigned the integer value 23, name is assigned the string “Russell”, height is assigned the floating-point number 6.05, and is_lit is assigned the boolean value True.

Now, if you don’t understand the different data-types I’m talking about, watch my previous video on Data Types in Python.

The print() function allows us to output variables, you can do this one at a time, or multiple values at once, separated by a comma. You can also use the “+” operator to print multiple variables, however, when using the “+” symbol ensure all data-types are the same, or else, the code won’t run.

In Python, it’s possible to assign values to multiple variables in one line, you can also do the inverse, by assigning the same value to multiple variables in one line. Lastly, if you have a collection of values in a list, Python allows you to extract the values into variables, in a process called unpacking.

Python features strict variable naming conventions. Variable names can be a combination of letters, numbers, and underscores, but they must start with a letter or an underscore. For example, my_variable, variable1, and “_hidden_variable” are all valid variable names. However, names like 1st_variable or my-variable are not valid and your code won’t run.

Pop quiz… Can you tell me why each of these 3 variables are illegal? I’ll give you a second, but feel free to pause the video. The first variable name is illegal as it starts with a number. The second variable name is illegal as it has a hyphen in it. Finally, the third variable is perfectly legal… I’m just trying to keep you on your toes.

Variable names with more than one word can be difficult to read.This is where Camel, Pascal and Snake case are useful. Camel case is when each word, except the first, starts with a capital letter. Pascal case is when each word starts with a capital letter. Finally, snake case is where each word is separated by an underscore character.

It’s good practice to choose meaningful variable names that describe the data they hold. This makes your code more readable and easier to understand.

Commenting is a critical skill in Python. You begin a comment by typing the “#” symbol, which will comment out all code on the same line, after the symbol. You can also create multi-line comments, using triple quotation marks. Comments are useful for annotating what a specific piece of code does, or for use in “deactivating” code.

Casting is when you change, or specify the data-type of a variable in Python, and is useful when you need to adjust a variable type.

The type() function returns the type of the specified variable, and is critical in identifying a variable type.

That’s a brief introduction to variables in Python! We’ve covered what variables are, how to create them, and some best practices for naming them.

Thanks for watching!

Related