×

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 Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Sentiment Analysis using NLTK

Introduction Data is being produced at an astounding rate and volume in the field of the internet and other digital services nowadays. Researchers, engineers, and data analysts often work with tabular...

7 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Cursor in Python

The cursor is an item that aids in query execution and records retrieval from databases. The cursor is crucial to the execution of the query. In-depth information on the execution...

7 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Creating Tables using Python MySQL

In this article, we are going to learn how to create tables in databases using Python MySQL. Introduction to Tables: Generally, databases are used in order to store the information in the...

9 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.