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.
It displays the list of python keywords as follows
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
Output
0
1 2 3 |
6.continue continue ends the current iteration of the loop, but not the whole loop.
#Program
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.
A python program that identifies which are the keywords
Output