Trip Back to Variables and Keywords in Python

Trip Back to Variables and Keywords in Python

If you're a python developer with months or years of experience, you can begin to imagine how it was when you started learning variables and keywords in this language.

You started with creating variables with the python rule in the documentation that gave a strong warning...Popular one - "Never use a keyword as a variable!!!"

So, if you're new to python and would love to know why this isn't allowed - then follow through.

What are Variables in Python?

Variables are used to store values in a memory location on your computer. These values are stored for future accessibility by the user.

In the C programming language, the variables are declared with their datatypes. In python, you don't need to declare the datatypes because the interpreter does that job for you.

Here's a difference below between C and Python variable declaration. Spot the difference in the code...

#variable declaration in C

int varNumber = 20;
printf("The variable number is %i", varNumber);

#variable declaration in python

varNumber = 20
print("The variable number is",varNumber)

Using the Assignment Operator to Declare Variables

Values are assigned to variables using the assignment operator "=". Numbers, strings and other datatypes can be assigned to the variables using the operator without declaring their datatypes.

e.g school = "Havard Business School", year = 2020, decimal = 20.45

Note: The assignment operator "=" is different from the relational or comparison operator "==". One is for assinging a value to a variable and the later is used to compare two values.

A value can be assigned to different variables.

var1 = var2 = var3 = 20  #a value assigned to different variables
print(var1)
print(var2)
print(var3
var1, var2, var3 = 20,40, "Peyer" #different values assigned to different variables on a line
print(var2)
print(var1)

#the above line of code is the same as;
var1 = 20
var2 = 40
print(var1)
print(var2)
print(var3)

Snippet

There are two known variables in Python which are the Global Variables and Local Variables. Global and Local variables will be discussed extensively in another article.

Rules for Writing Variables in Python

Here are some rules to guide you as a python developer...Remember, others will read your code and they'd love to understand what you did.

1) Variables are case sensitive in python. Check whether it's a lowercase, uppercase before calling the variable elsewhere in you program (e.g Python, python, PYTHON)

2) Use meaningful names to write your variables. This will help you and other developers understand what you did (e.g name = "Dara" and not n = "Dara"

3) A number cannot start a variable. (e.g 1Number) but numbers can be placed in between the letters.

4) A variable can start with an underscore and can also be placed in between the letters.(e.g _schools, number_1)

5) Keywords cannot be used as variables.

What are Keywords in Python?

Keywords are reserved words used in the syntax of the python language. Keywords can't be used as identifiers, or variables in Python. For easy identification in most IDEs, keywords are colored differently from variables.

Below are the keywords in Python...

In, True, finally, false, and, or, yield, except, elif, if, else, as, return, import etc

To find out more on the remaining keywords in Python...

#import the keyword module
import keyword
print(keyword.kwlist)

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

Conclusion

All datatypes and datastructures in Python can be assigned to variables. Variables continues to store the values for as long as you need them.

Remember the rule that variables can't be used as keywords!!! Don't break the rules so that errors won't appear in your code.