Python Tutorial — Variables And Keywords

codeforests
4 min readJul 11, 2020

This article serves as a tutorial for Python beginners to gain the essential knowledge to start coding in Python. By complete this tutorial, you shall be able to know how to correctly use Python variables as well as the Python keywords.

Python Variable

Variable is a name that refers to some value. Like any other programming languages, Python allows to define variables and manipulate it in your code logic.

Name convention

Python allows to use letter, number, or underscore [_] in a variable name, but it has to start with a letter or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

There is no limit on the length of your variable name, so you can choose anything meaningful to you in your code. but Python provided some guidelines to use lowercase as much as possible for the variables and function name.

Below are some examples of valid variable names:

a = "a" #Python variable name is case sensitive A = "a" module_name = "Python Tutorial for Variables & Keywords" speed_of_gravity = 299792458 pi = 3.14159265359 is_matched = True

And some invalid variable names as per below, if you use them in your code, Python throws “SyntaxError: invalid syntax” error.

1st_name = "John" #invalid as variable cannot start with digits first name = "John" right/wrong = True #invalid as variable cannot has special characters like /, whitespace, @, &, * etc., except _

Use of underscore

Take note of the _, although it is allowed to use in your variable name, it has some special meaning if you use it at the beginning. e.g. if you use _salary in your class, Python will protect it from accessing from outside of the class. This is out of scope for this topic, but do bear in mind on this.

Also if you use _ as your available name, there will be a conflict in the Python interactive mode, as in interactive mode, _ is interpreted as the result of the last executed expression, check more from this article.

You may also noticed that variables can hold different sorts of values, e.g. single character, multiple characters, numbers, and True or False etc. This is the different data type in Python, we will come to this topic in the later article.

Reserved Keywords

There are some other words we cannot directly use as variable, these words are so called Python reserved keywords, as Python uses these words to recognize the structure of the program.

Below are all the keywords reserved by Python3, and it is not allowed to use them directly as variable name.

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

For Python beginners, if you use some IDE like PyCharm or Jupyter Notebook, these keywords will be automatically highlighted in different color, so you don’t worry about you mistakenly used them as variable name.

Besides these reserved keywords, there are a few more words you shall try to avoid using them when defining your variable. For instance the below:

str int float list dict set tuple bytes

These are the Python built-in data types which will be covered in the next tutorial. And there won’t be any error prompted immediately when you assign a value to them, but you will face some issues when you want to call the default behavior of the built-in data type later. Below is an example:

The str() will throw error if you assigned “Test” to it, and it only works again if you delete the “str” as a variable. Hence the best practice is not to use these words as variable name in your code to prevent some unexpected errors and confusions.

Originally published at https://www.codeforests.com on July 11, 2020.

--

--

codeforests

Resources and tutorials for python, data science and automation solutions