Thursday, March 19, 2015

Python Keywords - Python Variables - Python Tutorial

Python KeywordsWhen we name our variables there are a few words we cannot use to name a variable. These words are known as Python Keywords. These thirty three words have some sort of purpose in Python. They are either built-in functions or methods or serve some other purpose. If we try to use one of the Python Keywords we will get an invalid syntax error.


Below you will find the full list of Python Keywords we will not explain what each one does in this tutorial but if one of the words is highlighted in blue you can either hover over the word or click on it for a better explanation.





































Python Keywords


andasassert
breakclasscontinue
defdelelif
elseexceptFalse
finallyforfrom
globalifimport
inislambda
Nonenonlocalnot
orpassraise
returnTruetry
while with yield

Trying To Assign A Variable a Keyword


Take a look at what happens when assign a variable a keyword. We will get an error. We will look at couple different examples here.


#Trying To Create A Variable using def
>>> def = 3
File "<stdin>", line 1
def = 3
^
SyntaxError: invalid syntax

#Trying To Create A Variable using else
>>> else = "Why doesn't this work"
File "<stdin>", line 1
else = "Why doesn't this work"
^
SyntaxError: invalid syntax

#Trying To Create A Variable using False
>>> False = 3
File "<stdin>", line 1
SyntaxError: can't assign to keyword

Access The Python Keywords


We can gain access to the keywords in Python by importing a module called keyword. We have not covered importing of modules yet, but we will in a later tutorial. For now just follow a long this module is easy to use and pretty straight forward.


First We Need To Import The Keyword Module


Open your Python interpreter and we are about import the keyword module. To import a module we simply type import and then the modules name. Follow the example below to import keyword module.


#Import Module

>>> import keyword
>>>
#note we do not get any response when we import a module

Get The Python Keyword List


Here we will use the keyword module to get a list of the Python Keywords in our interpreter. This command only works if you have imported the module.


#Get The Python Keyword List

>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', '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']

Check If A Word Is A Python Keyword Using The Keyword Module


In this example we will use the keyword module again this time we can check is a certain word is a keyword.


#Check If A Word Is A Python Keyword Using The Keyword Module

>>> keyword.iskeyword('else')
True
#note the word needs to be a string

#if we do not use a string we get an error
>>> keyword.iskeyword(else)
File "<stdin>", line 1
keyword.iskeyword(else)

You do not need to memorize the Python Keywords as we move on with our Python tutorials you will pick up on the keywords as you learn Python.  If you have any questions or comments leave them below.


 



No comments:

Post a Comment