Machine Learning in Cloud

Machine Learning in Cloud

Python Keywords and Identifiers

This sections explains keywords (reserved words in Python) and identifiers (names given to variables, functions, classes, etc.).

Python Keywords

There are 35 keywords in Python 3.8.2. The number of keywords varies based on the Python version.

  • Keywords are the reserved words in Python.
  • Keyword cannot be used as a variable name, function name or any other name identifiers.
  • Keywords are fundamental part of Python language, its syntax and structure.
  • Keywords, similar to variable or function names, are case-sensitive.

Only three keywords have the first letter as CAPITAL (True, False, None), the rest is written in lower case (remember, keywords are case-sensitive)

All the keywords except True, False, and None are in lowercase and they must be written as it is. The list of all the keywords is given below.

Keywords in Python version 3.8.2

FalseTrueNoneandas
assertasyncawaitbreakclass
continuedefdelelifelse
exceptfinallyforfromglobal
ifimportinislambda
nonlocalnotorpassraise
returntrywhilewithyield
Keywords

You can check the desired name against the list above easily to determine if it is a reserved keyword. This module allows a Python program to determine if a string or identifier is a keyword.

import keyword
keyword.iskeyword(variable_name)
  • Returns True if variable_name is reserved keyword
  • Returns False if variable_name is NOT reserved keyword
#if keyword
True

#if not keyword
False

Python Identifiers

An identifier is a name that the user gives to any user-created variables, functions, classes, decorators, generators etc.

Rule of thumb for identifiers nomenclature :

  • There is NO limit on the length of an identifier name. It can be a character or 1000.
  • Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _.Names like MyFunc, myFunc, my_variable_1 and my_awesome_coding_skills, all are valid examples.
  • Identifiers can start with _, for example; __variable is viable but it cannot start with a number, for example; 22variable or 22_variable is not viable identifiers and Python throws a syntax error
  • Python reserved keywords cannot be used as identifiers.
#code 
class = 2020

#Output
File \"<interactive input>\", line 1
    class = 2020
           ^
SyntaxError: invalid syntax
  • We cannot use special symbols like !, @, #, $, % etc. in our identifier.
#Code
group@ = 3

#Output
File \"<interactive input>\", line 1
    group@ = 3
     ^
SyntaxError: invalid syntax
Previous Article