Example HTML page

Keywords in Python

A “keyword”, called as “Reserve word” by the language which conveys a special meaning to an interpreter. A keyword may be a command or a parameter.

Note: Keywords cannot be used as a variable name in the source code.

To know the keywords in python type the following in IDLE.

>>> import keyword

>>> print(keyword.kwlist)

It displays the list of python keywords as follows

['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']

Let us discuss some of the keywords

1.True or False

In Python, whenever the logical (Boolean) operations are performed between the two operands then the resultant is either true or false.

Ex

>>> 1==1

True

>>> 1==3

False

>>> 5>3

True

>>> 10<=2

False

>>> 3>7

False

>>> True and False

False

>>> True or False

True

2. None

None is a special constant in Python that represents the absence of a value or a null value. None does not imply false, 0 or any empty list, dictionary, string etc.

>>> None==0

False

>>> None==False

False

>>> None==[ ]

False

>>> x=None

>>> y=None

>>> x==y

True

>>> x!=y

False

3. as

‘as’ helps to create an alias while importing a module. It means giving a different name (user-defined) to a module while importing it.

Ex:

>>> import math as myname

>>> myname.sin(myname.pi)

1.2246467991473532e-16

 4. assert assert is used for debugging purposes.

Ex:

>>> x=5

>>> assert x<7

>>> assert x>7

Traceback (most recent call last):

File “<pyshell#43>”, line 1, in <module>

assert x>7

AssertionError

 5.break

break will end the smallest loop it is in and control flows to the statement immediately below the loop.

#Program

for i in range(10):
    if i == 4:
        break
    print(i)

Output

0

1

2

3

6.continue continue ends the current iteration of the loop, but not the whole loop.

#Program

for i in range(10):
    if i == 4:
        continue
    print(i)

Output

0

1

2

3

5

6

7

8

9

7.class  Class is used to define a new user-defined class in python.

8. def

  • def is used to define a user-defined function.
  • Function is a block of related statements, which together does some specific task.
  • The usage of def is shown in below:

def function_name(parameters):

9. del

 del is used to delete the reference to an object. Everything is object in Python. We can delete a variable reference using del.

Ex:

>>> a=b=3

>>> del b

>>> b

Traceback (most recent call last):

File “<pyshell#47>”, line 1, in <module>

b

NameError: name ‘b’ is not defined

>>> a

3

10. except,raise,try

except, raise, try are used with exceptions in Python. We can raise an exception explicitly with the raise keyword.

11. finally

  • finally is used with try…except block to close up resources or file streams.
  • Using finally ensures that the block of code inside it gets executed even if there is an unhandled exception.

12. in

  • in is used to test if a sequence (list, tuple, string etc.) contains a value. It returns True if the value is present, else it returns False.
>>> for x in 'python':
	print(x)

	
p
y
t
h
o
n


A python program that identifies which are the keywords

#Python code to demonstrate working of iskeyword()
 
import keyword                # importing "keyword" for keyword operations
a = "for"                     # initializing strings for testing
b = "India"
c = "elif"
d = "elseif"
e = "sudhakar"
f = "assert"

# checking which are keywords
if keyword.iskeyword(a):
        print ( a + " is a python keyword")
else :
    print ( a + " is not a python keyword")
 
if keyword.iskeyword(b):
        print ( b + " is a python keyword")
else :
    print ( b + " is not a python keyword")
 
if keyword.iskeyword(c):
        print ( c + " is a python keyword")
else :
    print ( c + " is not a python keyword")
 
if keyword.iskeyword(d):
        print ( d + " is a python keyword")
else :
    print ( d + " is not a python keyword")
 
if keyword.iskeyword(e):
        print ( e + " is a python keyword")
else :
    print ( e + " is not a python keyword")
 
if keyword.iskeyword(f):
        print ( f + " is a python keyword")
else :
    print ( f + " is not a python keyword")
 


Output

for is a python keyword
India is not a python keyword
elif is a python keyword
elseif is not a python keyword
sudhakar is not a python keyword
assert is a python keyword
Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest