List of Keywords in Python and their uses

The list of keyword in Python and their uses are as follows:-

 

Falseassertdelforinorwhile
Nonebreakeliffromispasswith
Trueclasselsegloballambdaraiseyield
and continueexceptifnonlocalreturn 
asdeffinallyimportnottry 

 

Some of the extra keyword is added and some are removed you can get these keyword by in the current version of prompt by using this code:-

>>> import keyword
>>> print(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']

 

True, False :-  True and False are used as a comparsion operator in Python. It is also called a logical(boolean) operations in python.

print False == 12
print True == 1
 
print True + True + True
print True + False + True

Output
False
True
2
2

 

None :-  Null has his own datatype that is Nonetype. It represents the absence value that is null value. It is a special constant in python.

def a_void_function():
    x = 5
    y = 5
    z = x + y

a = a_void_function()
print(a)

Output
none

 

and, or , not :-  These are the logical operator in python and is resulting into true, or  is resulting true when operand is true, not  is used to convert the true value. The truth table of and,or,not are given below:-

Truth table of  and

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Truth table of  or

ABA and B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Truth table of  not

Anot A
TrueFalse
FalseTrue

 

# True, False, None, and, or , not
 
# prints False as its false.

print (None == 0)
print (None == 3)

# showing the example of None

a = None
b = None
print (a == b)
 
# showing logical operation 

print (True or False)
 
# showing logical operation 

print (False and True)
 
# showing logical operation 

print (not True)

Output
False
False
True
True
False
False

 

as :-  as is used to import the module while creating alias. It gives the different name to user define module while importing it.

assert :-  Assert statement  is a condition which is always true in python and if the condition is False then it gives an Assertionerorr. It is used for a debugging purpose.

break,continue :- break and continue are used in for and while loops to change the behaviour. Break is used to control the flow of statement and it end with smallest loop.Continue ends the current iteration loop, but not the whole loop.

#break
for i in range(1,30):
    if i == 8:
        break
    print(i)

Output
1
2
3
4
5
6
7

#continue
for x in range(1,10):
    if x == 8:
        continue
    print(x)

Output
1
2
3
4
5
6
7
9

class :-  Class is a collection of attribute and methods used describe the real world situation and add consisteneny in the program so that we can use in a proper way.

def :-  def functions are use apply the code more than one place with the reuse to code in a program and it is used to copy the code.

del :-  It is used to remove an item from the existing list. We can delete a variable reference by using del.

# del example
 
# initialising list 
x = [57, 22, 53]
print ("This is the number list with all the value")
print (x)
del x[2]
print ("This is the number list after deleting 2nd element")
print (x)

Output
This is the number list with all the value
[57, 22, 53]
This is the number list after deleting 2nd element
[57, 22]

 

if,else,elif :-  if,else,elif are the decision making statement.These statements are used to test some condition and execute in the block.  In python elif  is used in place of elseif  it is used to check that the expression is TRUE as the condition is TRUE it execute the block of code.  The elseis combine with if statement it contains the block of code it  resolve to 0 or FALSE.

def if_example(a):
    if a == 1:
        print('One')
    elif a == 2:
        print('Two')
    else:
        print('Something else')

if_example(2)
if_example(4)
if_example(1)

Output
This is even number
This is odd number
This is another number

except,raise,try :-  These statements are used to exception in python It disrupts the flow of the program.It is a python object which represents an error with some of the examples of exception in Python are IOError, ValueError, ZeroDivisionError, ImportError, TypeError  etc.

def reciprocal(num):
    try:
        x = 7/num
    except:
        print('Exception caught')
        return
    return x

print(reciprocal(20))
print(reciprocal(0))

Output
0.35
Exception caught
None
 

finally :-  finally is used to protect the block of code which is executed by with one or more except  blocks to catch specific exceptions.

for :-  for is used for looping to emphasize the collection of items. The foris used with any type of sequence like a list or a string.

cities = ['Lucknow','Delhi','Agra','Kanpur']
for i in cities:
    print('Welcome '+i)

Output
Welcome Lucknow
Welcome Delhi
Welcome Agra
Welcome Kanpur

from import :- The import is  made up of import keyword along by using the name of module.The from....import  used for special attributes and function forin current namespace.

global :- global function is used to declare that the variable which is inside a function is also exists oustide the function means we can use it globally.

def read1():
    print(globalvar)
def write1():
    global globalvar
    globalvar = 5
def write2():
    globalvar = 70

read1()
write1()
read1()
write2()
read1()

Output
50
5
5

in :-  in is used to test if an array(string, list) contains a value. If the value is present it return TRUE, otherwise it return False.

for x in 'devstudio':
    print(x)

Output
d
e
v
s
t
u
d
i
o

 

is :-  is used for the test variable which is assigned by the same object .It is for testing object identity.

lambda :-  It is used to create the function without a name and it combines with some of the functions such as  filter(), map() and reduce().

a = lambda x: x*7
for z in range(1,9):
    print(a(z))

Output
7
14
21
28
35
42
49
56

 

nonlocal :-  nonlocal is like a global keyword. It is used to declare a variable inside a function (function inside a function,or nested function). To adjust the value of nonlocal variable inside a nested function, we have to declare with nonlocal.

def outer_func():
    a = 17
    def inner_func():
        nonlocal a
        a = 6
        print("Inner function: ",a)
    inner_func()
    print("Outer function: ",a)

outer_func()

Output
Inner function:  6
Outer function:  6

 

pass :-  pass statement  is a null operation used as a placeholder no function is perform when the code is being executed.

return :- The return function exits your statement and return the result i.e the value of the expression following the return keyword, to the caller.

def function_return():
    x = 60
    return x

def no_return():
    x = 60

print(function_return())
print(no_return())

Output
60
None

 

while :-  while statement is executed until the condition is false  or a break statement is close.

with :-  The with statement assure that a cleanup is always used to cover the execution of block of a code within a method.

yield :-  yield is used with generator inside a functional work same as return statement.

def generator():
    for a in range(9):
        yield a*a

g = generator()
for a in g:
    print(a)

Output
0
1
4
9
16
25
36
49
64

 

 

Keywords: