Keywords in Python | Set 1

File handling | Loops | Python Methods and Functions | String Variables

This article aims to provide a detailed understanding of these keywords.

1. True : This keyword is used to represent the boolean true. If the statement is true, "True" is printed.

2. False : This keyword is used to represent the boolean value false. If the statement is false, "False" is printed. 
True and False in python are the same as 1 and 0. Example:

print False = = 0

print True = = 1

 

print True + True + True

print True + False + False

3. None : This is a special constant used to denote null or void It is important to remember, 0, any empty container (for example, an empty list) does not evaluate to None
This is an object of its own data type — NoneType. It is not possible to create multiple None objects and assign them to variables.

4. and : This is a boolean operator in python. "AND" Returns the first false value. If not found, return the last . The truth table for "and" is shown below.

3 and 0 returns 0
3 and 10 returns 10
10 or 20 or 30 or 10 or 70 returns 70

5. or : This is a boolean operator in python. " Or " Returns the first True value. If not found, returns the latest . The truth table for "or" is shown below.

3 or 0 returns 3
3 or 10 returns 3
0 or 0 or 3 or 10 or 0 returns 3

6. not : This boolean operator inverts the truth value . The truth table for “not” is shown below.

# Python code for demos
# True, False, Not and / or Not

  
# indicates that None is not equal to 0
# prints False as false.

print ( None = = 0 )

 
# shows target None
< code class = "comments"> # two None values ​​are equal to None
# here x and y are both equal to zero
# hence true

x = None

y = None

print (x = = y)

 
# shows a boolean operation
# or (returns True)

print ( True or False )

 
# pok Calls a boolean operation
# and (returns False)

print ( False and True )

 
# shows a boolean operation
# not (returns False)

print ( not True )

Output:

 False True True False False 

7. assert : This function is used for debugging purposes . It is usually used to check the correctness of the code. If the statement evaluates to true, nothing happens, but when it is false, " AssertionError " is thrown. You can also print a comma separated error message .

8. break : "break" is used to control the flow of a loop. The statement is used to exit the loop and transfer control to the statement immediately after the loop.

9. continue : “continue” is also used to control the flow of code. The keyword skips the current iteration of the loop , but does not end the loop .

Illustrations of the keywords break and continue can be seen in the article below.

here

11. def : This keyword is used to custom function declarations . For more information. click here

12. if : This is a control statement for making a decision.  Expression of truth causes control to flow into the if statement block.

13. rest : This is a control statement for making a decision.  A false expression forces control to go to the "else" statement block.

14. Elif : This is a decision control statement. This is short for " otherwise if "

if, else and elif conditional statements are explained in detail here in the article.

15. del : del is used to remove an object reference . Any variable or list value can be removed with del.

# Python code for demo
# del and assert

 
# initializing list

a = [ 1 , 2 , 3 ]

 
# print the list before deleting any value

print ( "The list before deleting any value" )

print (a)

 
# using del to remove the 2nd list item

del a [ 1 ]

 
# print the list after removing the 2nd element

print ( "The list after deleting 2nd element" )

print (a)

 
# assert demonstration
# prints AssertionError

assert 5 & lt;  3 , "5 is not smaller than 3"

Output:

 The list before deleting any value [ 1, 2, 3] The list after deleting 2nd element [1, 3] 

Runtime error:

 Traceback (most recent call last): File "9e957ae60b718765ec2376b8ab4225ab.py", line 19, in assert 5 & lt; 3, "5 is not smaller than 3" AssertionError: 5 is not smaller than 3 

Next article —  by Manjeet Singh (S. Nandini) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





Tutorials