Keywords

Keywords are reserved words in any programming language which can’t be used as variable names, function names or identifiers. These reserved words which make the basis of programming language and used for performing different build-in operations in programming language.

Keywords might get some minor changes in new versions of programming language, you can get list of all keywords by running these two lines below:

import keyword
print(keyword.kwlist)

Output:

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

Or you can use following single line:

help("keywords")

Output:

Here is a list of the Python keywords.  Enter any keyword to get more help.

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

Here is the list of all possible python keywords with their short description.

Note: Some keywords are added and decrypted in new versions of Python.

KeywordDescription
andLogical operator, performs conjunction
notLogical operator, performs negation
orLogical operator, performs disjunction
NoneRepresents a null value
nonlocalDeclare a non-local variable
pass‘Null statement’ means that no action will be taken
raiseRaise an exception
returnExit a function and return a value
TrueBoolean value, result of comparison operations
tryMake a try…except statement
whileCreate a while loop
withUsed to simplify exception handling
yieldEnd a function, returns a generator
forCreate a for loop
fromImport specific parts of a module
globalDeclare a global variable
ifMake a conditional statement
importImport a module/library
inDetermine if a value exists in a list, tuple, or other collection.
isTest if two variables are equal

Identifiers

Identifiers are names of variables, function, module, class, library or any other object we give any entity a name known as identifier.

Most people think variables and identifiers are the same, but actually they are defined in different ways.

Let’s take a look, according to the definition of variables, a variable in programming language is memory allocation which values can change over time, however we give that value a name which is an identifier.

Identifier itself is a board term which includes all the names of classes, functions, modules and variables.

Rules for naming Python Identifiers

1. An identifier can consist of uppercase letters and lowercase letters, digits, and underscores (_).

2. An Identifier can not start with a digit.

3. Identifiers cannot contain special characters such as #%&*@ etc.

4. Identifiers can be of any length.

5. Space is not allowed in writing identifiers hence use underscore.

6. Variable names are case-sensitive.

Valid Identifier Invalid Identifier
Count = 51count = 5
My_book = “python programming”My Book = “python programming”
email_address = “abc@zyz.com”email@address = “abc@xyz.com”

Statements

A statement is simply a set of instructions which a python interpreter can execute. In simple words any thing we write in python is a statement. Unlike other programming languages where a single line of code (single statement) ends with a semicolon (;) , a python statement ends with a newline.

print("hello world")

Above line is single python statement

Comments

Comments in Python are identified as hash (#) symbol, comments in Python are used to make the program understandable by other programmers.

# program below is used to add two digits

a = 5
b = 6
# sum of two variables
c = a+b
print (c,"sum of a & b")

Python doesn’t allow to add multiple comments in program, you have to add separate # in each line.

However you can use quotation marks to add long comments in the program, & don’t assign it to any variable, so python will not interfere with it.

"This long string is acting as a 
comment in a program, you can 
type as long as needed "

However, adding too many long string comments is not considered good practice.

Namespace

A namespace is a way to organize your variables and functions. Everything in Python is an object, including variables and functions. Most of the time, you want to keep your code organized by putting related variables and functions into namespaces.

Namespaces can be divided into three types in Python programs:

1. Built-In

2. Global

3. Local

1. Built-In

All of Python’s built-in objects’ names are listed in the built-in namespace. When Python is running, these are always accessible. The following command will list the items in the built-in namespace:

dir(__builtins__)

2. Global

All names defined at the level of the main programme are included in the global namespace. The global namespace is created when the main Python program begins and remains until the interpreter terminates.

3. Local

A class, function, loop, or any other type of code block has a declared local namespace. A function or a block of code’s defined names are specific to those areas. The defined function or code block is the only place where the variable names can be accessed.

def first_func():
    x = 20
    print('x =', x)
   

    def second_func():
        x = 30
        print('x =', x)

    second_func()

x = 10
first_func()
print('x =', x)

Output:
x = 20
x = 30
x = 10

There are two functions in the above program, first_func and second_func, you can’t call second_func outside the first function, because second_func is defined within the first function. Otherwise you will get an error message, if you call a function outside the scope.

NameError: name 'second_func' is not defined

Categorized in: