Python Keywords and Identifiers

Python Keywords and Identifiers, keywords (reserved words in Python), and identifiers (names given to variables, functions, etc.).

Introduction:

Python keywords are also known as reserved words. Keywords are particular words that act as a key to a code. These are predefined words by Python, so they cannot be used as identifiers.

Identifiers in Python are symbolic names used for identification. They can be a class name, variable name, method name, function name, and more.

Note: Python keywords or reserved words can not be used as an identifier.

Python Keywords:

Python keywords or reserved words are particular words that act as a key to a code. We have “keywords” in every programming language, Python keywords are small letters, and Python is a case-sensitive language.

Keyword
Description
andA logical operator
asTo create an alias
assertFor debugging
breakTo break out of a loop
classTo define a class
continueTo continue to the next iteration of a loop
defTo define a function
delTo delete an object
elifUsed in conditional statements, same as else if
elseUsed in conditional statements
exceptUsed with exceptions, what to do when an exception occurs
FalseBoolean value, result of comparison operations
finallyUsed with exceptions, a block of code that will be executed no matter if there is an exception or not
forTo create a for loop
fromTo import specific parts of a module
globalTo declare a global variable
ifTo make a conditional statement
importTo import a module
inTo check if a value is present in a list, tuple, etc.
isTo test if two variables are equal
lambdaTo create an anonymous function
NoneRepresents a null value
nonlocalTo declare a non-local variable
notA logical operator
orA logical operator
passA null statement, a statement that will do nothing
raiseTo raise an exception
returnTo exit a function and return a value
TrueBoolean value, result of comparison operations
tryTo make a try…except statement
whileTo create a while loop
withUsed to simplify exception handling
yieldTo end a function, returns a generator

Python Identifiers

An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.

Rules for writing identifiers

1. 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 myClass, var_1 and print_this_to_screen, all are valid example.

2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name.

3. Keywords cannot be used as identifiers.

global = 1

SyntaxError: invalid syntax

4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.

a@ = 0

SyntaxError: invalid syntax

5. An identifier can be of any length.


Python Syllabus

Python Step by Step Video Tutorial

Python Tutorial

Follow me on social media: