Skip to main content

Learning Python Part-4: Python Keywords

Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers
  • They are used to define the syntax and structure of the Python language.
  • In Python, keywords are case sensitive.
  • There are 33 keywords in Python 3.7. 
  • All the keywords (except True, False and None) are in lowercase.
  • Below is the list of keywords in Python 3.7

I will not be using just syntax to explain these keywords, rather I will use some simple yet enough code examples considering new plus experienced people. For those who are new to python, if you find it difficult  to understand the complete code, just try to see the context and usage of keyword.  False
This keyword is used to represent a boolean false. If a statement is false, “False” is printed. False in python equals to 0.
True: This keyword is used to represent a boolean true. If a statement is true, “True” is printed. True in python equals to 1.
None: This is a special constant used to denote a null value or a void. It is an object of its own datatype –NoneType. 

This is a logical operator in python. Widely used in decision making with all conditions to match as in example below. The truth table for “and” is depicted below. or: This is a logical operator in python just like and. However, unlike and, it is used to match any condition from given conditions set. The truth table for “or” is depicted below. not: This logical operator inverts the truth value to its opposite value as in example 1 below also can be used for checking membership as in example 2. The truth table for “not” is depicted below. assert: This function is used in debugging purposes to check the correctiveness of code. If a statement evaluated to true, nothing happens, but when it is false, “AssertionError” is raised. break:  This function is used to control the flow of loops. The break keyword is used to break out of current loop if condition is true. Else the next statement in loop is execute as shown below. continue: This function is also used to control the flow of code like break. However, this keyword skips the current iteration of the loop, but does not end the loop as in example below. class: This keyword is used to declare user defined classes. def: This keyword is used to declare user defined functions. if: This keyword is a control statement for decision making. True expression forces control to go in “if” statement block. False expression forces control to go in next (elif or else) block. elif: It is a control statement for decision making. It is short for “else if”. Used along with if statement as shown below. else: This keyword is a control statement for decision making. False expression forces control to go in “else” statement block. Used as if ... else or if ... elif ... else. del: "del" keyword is used to delete an object or it can be used to delete any value in any variable or list. try: This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in “try” block is checked, if there is any type of error, except block is executed. for example check the finally keyword section. except: This keyword works in conjunction with “try” to catch exceptions. finally: No matter what is result of the “try” block, block termed “finally” is always executed.  raise: This keyword is used for exception handling to explicitly raise exceptions. for: The for keyword is used to create a for loop. It can be used to iterate through a sequence, like a list, tuple, etc. while: This keyword is used to control flow and looping. With the while loop we can execute a set of statements as long as a condition is true. pass: It is the null statement in python. Nothing happens when this is encountered. This is also used to prevent indentation errors and used as a placeholder in empty classes or functions. import: This statement is used to import a particular module into current program. from: Generally used with import, from is used to import particular functionality from the module imported. as: This keyword is used to create the alias for the module imported. i.e giving a new name to the imported module.. E.g import math as mymath. lambda: It is also known as anonymous function. This keyword is used to make inline returning functions with no statements allowed internally. return: This keyword is used to return from the function. yield: This keyword is used like return statement but it returns a generator. The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. with: with statement in Python is used in exception handling to make the code cleaner and much more readable. in: This keyword is used to check if a container contains a value. This keyword is also used to loop through the container. is: This keyword is used to test object identity, i.e to check if both the objects take same memory location or not. global: This keyword is used to define a variable inside the function with a global scope. non-local: This keyword declares a variable to point to variable of outside enclosing function, in case of nested functions.


Popular posts from this blog

Learning Python Part-6: Python Comments

When it comes to writing a python program, based on the requirements, programs can simple programs of limited number of  lines or it can be really large program which might even span multiple python modules. Now the point here is that, even if we know how to write the program and even we follow correct concepts and syntaxes while writing program, we cannot keep it in our mind forever. We cannot remember each and everything about the program like variables, classes, concepts, functions and so on after some period of time. If the program written by us at some point in time back, once we open it after a period of time, it should not be a situation that we are blank, or guessing why particular code, function and so on. Even it may happen that you are working in a team and program code writing tasks might be shared. So when you put all pieces together written by multiple programmers in your team, you should be able to understand the code. This is where we bring in the concepts of

Learning Python - Part-5: Python Interfaces

When it comes to working with python, we can always use interactive mode by launching python  or Python3 command as shown in below screenshot.  Image: 1 Once launched we can run all sorts of python operations, however, this approach is not used for development work.  When it comes to writing programs, we generally prefer editors. In fact we can use basic editors like notepad(Windows)/vi editors(Linux). In these editors, once we are done with writing programs, we need to make sure files are stored as .py extension ( Image: 2 ). We simply run python command to execute the programs as shown in below screenshot ( Image: 3 ). Image: 2 Image: 3 Though using normal editors is fine, but when it comes to writing simple, large or complex programs, these basic editors do not offer help or suggestions or correction while writing code. That is where the need of tools that help during programming arises. Below are some of the code editors and IDEs that are widely used.