Keywords in Python

Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier.


Here's a list of all keywords in Python Programming


Keywords in Python programming language

 >>> import keyword

 >>> print(keyword.kwlist)

 ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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'] 

Description of Keywords in Python with examples

True, False

True and False are truth values in Python. They are the results of comparison operations or logical (Boolean) operations in Python. For example.

>>> 1 == 1 

True

 >>> 5 > 3 

True

 >>> True or False

 True 

>>> 10 <= 1

 False

 >>> 3 > 7

 False

 >>> True and False 

False

None

None is a special constant in Python that represents the absence of a value or a null value.

It is an object of its own datatype, the None Type. We cannot create multiple None objects but can assign it to variables. These variables will be equal to one another.

We must take special care that None does not imply False, 0 or any empty list, dictionary, string etc. For example:

>>> None == 0

 False

 >>> None == [] 

False 

>>> None == False 

False

 >>> x = None

 >>> y = None

 >>> x == y 

True 

and, or , not

and, or, not are the logical operators in Python. and will result into True only if both the operands are True. 

>>> True and False 

False 

>>> True or False 

True 

>>> not False 

True

as

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

As for example, Python has a standard module called math. Suppose we want to calculate what cosine pi is using an alias. We can do it as follows using as:

>>> import math as myAlias >>>myAlias.cos(myAlias.pi) -1.0

Here we imported the math module by giving it the name my Alias. Now we can refer to the math module with this name. Using this name we calculated cos(pi) and got -1.0 as the answer.

assert

assert is used for debugging purposes.

While programming, sometimes we wish to know the internal state or check if our assumptions are true. assert helps us do this and find bugs more conveniently. assert is followed by a condition

>>> a = 4 

>>> assert a < 5 

>>> assert a > 5

File "<string>", line 301, in run code File "<interactive input>", line 1, in <module> Assertion Error: 

async, await

The async and await keywords are provided by the async library in Python. They are used to write concurrent code in Python. For example,

import async 

async def main(): 

                  print('Hello')

                  await async.sleep(1) 

                  print('world')




Comments

Popular posts from this blog

insertion and deletion in array in c program

C programming Viva questions