×

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used".

You can learn about keywords in python, syntaxes, usages, and how to implement them while executing codes. Keywords are defined as words, which are pre-defined and reserved words that have a specific meaning and purpose of use. If you want to solve some basic python programs or learn the basics of python, you should go through this whole article.

These keywords cannot be used as variables. There are 35 keywords in python as of version 3.8.
We can run the below command to find the words, which are already reserved.
help(" keywords")

Below is a list of the Python keywords.

False        class               from                  or

None        continue         global              pass

true         def                   if                      raise

and          del                   import             return

as              elif                  in                     try

assert        else                 is                     while

async        except             lambda          with

await        finally             nonlocal        yield

break       for                   not       

And if we want to learn more about any keyword, we can specify the keyword in help () for which we need more information.

The command goes like this.

Example: help(” return”)

It will return the use and where to pass in the syntax to be syntactically correct.

There are two helpful members that will be dealing with the keywords.

1. Kwlist will provide the list of keywords that are in use in a particular version.

2. Iskeyword () will help to find if a string is defined as a keyword or not.

Below is given the details of keywords and their usage:

 THE KEYWORD “and”

"and" is used to find out if both the expressions before and after are true or false.

For example, <exp1> and <exp2>

if both exp1 and exp2 are true, the output will be true. If either of them is false, then the result will be false.

"and" will not return the Boolean value like operands. Rather it will return exp1 if it is false, or it will return exp2.

If we are particular to get the result and whether it is true or false, we need to check it by passing the result as bool (), or we can use a conditional if statement.

You can have a look at the following code to understand and keyword.

Program:

a=10
b=15
if (a==10 and a+b>20):
    print (“this is and keyword”)
else:
    print (“condition is not true”)

Output

 Keywords in python 

When you execute the above code, you can see the following output "this is and keyword" you get this output because both the conditions are given are true. 

Similarly, if you pass any false condition, then you will see the following output "condition is not true".

THE KEYWORDS if AND else

"if" is the control flow keyword used to execute a conditional statement. The "if" keyword will allow you to execute the block of code only if after the expression given in the "if" is true.

"else" will indicate that a block must be executed only if a condition mentioned in "if" is false. An "else" without "if" cannot exist.

Program:

a=10
b=15
if (a+b>20):
    print (“this is if block”)
else:
    print (“this is else block”)
 Keywords in python 

You can clearly understand the python if and else statement with the help of the above code.

You can clearly observe that if the condition in the if block is satisfied, you will see the output "this is if block".

If the condition in the "if" block is not satisfied, you can see the following output "this is else block".

 "if" can be used individually, or it can be used with an "if" followed by the "else" keyword.

The condition goes like this,

Example:

if<exp1>:
      <statements>

You can see the below code to understand the usage of the "if" statement without using the "else" statement.

Program:1

a= “Hello”
if(a==“Hello”):
     print(“welcome to programming world”)

Output

Keywords in python

Program:2

a= input()
if(a==“compiletimeinput”):
     print(“welcome to programming world”)
Keywords in python

You can also use "nested if" statements if you want to perform multiple actions.

Example:

if <exp1>:
                      if<exp2>:
                             <statements>
                 else:
                        <statements>

You can check the following code for the implementation of "nested if" statements.

If the condition in the first if statement is true, then only the second if statement will be executed. If not, the compiler will come out of the statement and executes the rest.

If the condition in the second if statement is true, then the compiler performs the task inside the second if statement.

Program:

a=int(input())
b=int(input())
if(a>b):
    if(a+b>0):
           print(“nested if”)
else:
    print(“try again’)
 Keywords in python 

The keyword elif

Elif can be used to verify if at least one condition mentioned in the set of conditions is true, and if any condition is true, then it will return the output of the block of code written in that elif block.

Else it can only be used after at least 1 if condition. An elif without it doesn't exist.

Multiple elif conditions can be used after the if condition.

Example:

if<exp1>:
                       <statements>
                elif<exp2>:
                        <statements>
                 ….
else:
                     <statements>

Refer to the below code to understand the working of "elif" statements. When there is multiple conditions or set of conditions, instead of using "if", you can use "elif".

This code prints different outputs based on the conditions provided in each block of statements.

Program:

a=int (input ())
b=int (input ())
if(a+b>0):
      print (“both are positive or at least one negative and the other is higher positive”)
elif (a+b<0):
      print (“both are negative or at least one has higher negative value”)
elif (a+b==0):
      print (“either both are 0 or both are off opposite sign”)
else:
      print (“nothing to output”)
 Keywords in python 

If the sum of values of "a" and "b" are positive, then you will see the following output "both are positive".

If the sum of values of "a" and "b" are negative, then you will see the following output "both are negative or at least one has a higher negative value".

If the sum of values of "a" and "b" are zero, then you will see the following output "either both are 0 or both are of opposite signs".

This is how "if elif" and "else" statements work when there are multiple sets of conditions.

These are the basic keywords which are required by each programmer or any person who wants to learn to program.

These are some of the basic example codes to learn the working of keywords and their functions in writing codes. If you are a beginner, you can learn about keywords and start writing some simpler codes using these keywords.

You can try to check whether a given number is even or odd, separating students of a class by grade-wise and checking whether a person is eligible for voting or not.

These are some of the basic programs to get familiar with python and some basic python keywords.


Related Topics

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python Statistical Module

Python Statistical Module The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The...

8 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

List Iteration in Python

In this tutorial, we will learn how to iterate list in Python. List in Python A list is an ordered group of values which includes several kinds of values.A list is a mutable...

3 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Python Bubble Sort

Bubble sort is one of the techniques used to sort the elements in lists in a certain order, either in ascending or descending order. It is called Bubble sort because...

4 minutes read.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

Python Logging Maxbytes

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.