CodeTreat

Full Stack Developer Tutorials

  • About

Python Keywords

Python keywords are reserved words that are used for a specific purpose. These keywords cannot be used in any identifiers such as function names, variables, classes etc. The keywords convey a specific meaning to the compiler/interpreter to perform special functions. Keywords are case sensitive and must be written as it is. Following are the list of keywords that are available in the language as of version 3.3.
 

false def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass

To view the list of available keywords, the keywords command from python help() mode can be used. Follow the below steps to open the help section.

1. Open command prompt/terminal.

2. Enter the command python. The interactive shell will be initiated.

3. Enter the command help() to open the help section.

4. Now, enter the command keywords. This will display the list of keywords available in the installed version of python as shown below.

python keywords

To exit from the help section, press Ctrl+C and then type exit() to exit the interactive mode.
 

Python Keywords example


def sample(a, b):
    a = a;
    b = b;
    if a > b:
        print("a is greater than b")
    else:
        print("b is greater than a")

sample(3, 5)

 
In the above example, we have used three keywords def, if, else. Python functions are defined with the keyword def. The sample program contains a simple function with the name sample which gets two parameters with variable names a and b as input and checks the greater number of the two inputs and displays the message based on the condition.

In the final line of the program, we have called the function and passed two parameters 3 and 5. Since we have passed the second number as a bigger one, the output will be displayed as “b is greater than a”. You can save this code in a file named sample.py and execute it in the command prompt by calling the filename as shown below.


PreviousNext

Share this:

  • Facebook
  • Twitter
  • WhatsApp
  • Tumblr
  • Google
  • Pinterest
  • Spring Boot Introduction
  • Spring Tool Suite Setup
  • Spring Boot Initializr
  • Spring Boot Project Structure
  • Spring Boot Sample Application
  • Python Introduction
  • Python Environment Setup
  • Python Identifiers
  • Python Keywords
  • Python Lists
  • jQuery Introduction
  • jQuery Environment Setup
  • jQuery Syntax
  • jQuery Selector
  • jQuery Events
  • jQuery Effects