Python-Keywords

Python Keywords

Last updated on 25th Sep 2020, Artciles, Blog

About author

Surendhar (Sr Technical Director - Python )

He is a TOP-Rated Domain Expert with 11+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 5 Years & Share's this Informative Articles For Freshers

(5.0) | 14677 Ratings 900

Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive.

Python Keywords

Python keywords are special reserved words that have specific meanings and purposes and can’t be used for anything but those specific purposes. These keywords are always available—you’ll never have to import them into your code.

Python keywords are different from Python’s built-in functions and types. The built-in functions and types are also always available, but they aren’t as restrictive as the keywords in their usage.

Subscribe For Free Demo

Error: Contact form not found.

Use an IDE With Syntax Highlighting

There are a lot of good Python IDEs out there. All of them will highlight keywords to differentiate them from other words in your code. This will help you quickly identify Python keywords while you’re programming so you don’t use them incorrectly.

Use Code in a REPL to Check Keywords

In the Python REPL, there are a number of ways you can identify valid Python keywords and learn more about them.

Note: Code examples in this article use Python 3.8 unless otherwise indicated.

You can get a list of available keywords by using help():

  • >>>
  • >>> help(“keywords”)

Here is a list of the Python keywords.  Enter any keyword to get more help.

  • False from or
  • None continue global pass
  • True def if raise
  • and del impor return
  • as elif in try
  • assert else is while
  • async except lambda with
  • await finally nonlocal yield
  • break for not

Next, as indicated in the output above, you can use help() again by passing in the specific keyword that you need more information about. You can do this, for example, with the pass keyword:

  • >>>
  • >>> help(“pass”)
  • The “pass” statement
  • ********************
  • pass_stmt ::= “pass”

“pass” is a null operation — when it is executed, nothing happens. It

is useful as a placeholder when a statement is required syntactically,

but no code needs to be executed, for example:

  • def f(arg): pass    # a function that does nothing (yet)
  • class C: pass       # a class with no methods (yet)

Python also provides a keyword module for working with Python keywords in a programmatic way. The keyword module in Python provides two helpful members for dealing with keywords:

    1. 1.kwlist provides a list of all the Python keywords for the version of Python you’re running.
    2. 2.iskeyword() provides a handy way to determine if a string is also a keyword.

To get a list of all the keywords in the version of Python you’re running, and to quickly determine how many keywords are defined, use keyword.kwlist:

  • >>>
  • >>> import keyword
  • >>> keyword.kwlist
  • [‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘async’, …
  • >>> len(keyword.kwlist)
  • 35

If you need to know more about a keyword or need to work with keywords in a programmatic way, then Python provides this documentation and tooling for you

Look for a SyntaxError

Finally, another indicator that a word you’re using is actually a keyword is if you get a SyntaxError while trying to assign to it, name a function with it, or do something else that isn’t allowed with it. This one is a little harder to spot, but it’s a way that Python will let you know you’re using a keyword incorrectly.

Python Keywords and Their Usage

The sections below organize the Python keywords into groups based on their usage. For example, the first group is all the keywords that are used as values, and the second group is the keywords that are used as operators. These groupings will help you better understand how keywords are used and provide a nice way to organize the long list of Python keywords.

There are a few terms used in the sections below that may be new to you. They’re defined here, and you should be aware of their meaning before proceeding:

Course Curriculum

Get Practical Oriented Python Training to UPGRADE Your Technical Skills

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum
  • Truthiness refers to the Boolean evaluation of a value. The truthiness of a value indicates whether the value is truthy or falsy.
  • Truthy means any value that evaluates to true in the Boolean context. To determine if a value is truthy, pass it as the argument to bool(). If it returns True, then the value is truthy. Examples of truthy values are non-empty strings, any numbers that aren’t 0, non-empty lists, and many more.
  • Falsy means any value that evaluates to false in the Boolean context. To determine if a value is falsy, pass it as the argument to bool(). If it returns False, then the value is falsy. Examples of falsy values are “”, 0, [], {}, and set().

For more on these terms and concepts, check out Operators and Expressions in Python.

Value Keywords: True, False, None

There are three Python keywords that are used as values. These values are singleton values that can be used over and over again and always reference the exact same object. You’ll most likely see and use these values a lot.

The True and False Keywords

The True keyword is used as the Boolean true value in Python code. The Python keyword False is similar to the True keyword, but with the opposite Boolean value of false. In other programming languages, you’ll see these keywords written in lowercase (true and false), but in Python they are always written in uppercase.

The Python keywords True and False can be assigned to variables and compared to directly:

  • >>>
  • >>> x = True
  • >>> x is True
  • True
  • >>> y = False
  • >>> y is False
  • True

Most values in Python will evaluate to True when passed to bool(). There are only a few values in Python that will evaluate to False when passed to bool(): 0, “”, [], and {} to name a few. Passing a value to bool() indicates the value’s truthiness, or the equivalent Boolean value. You can compare a value’s truthiness to True or False by passing the value to bool():

  • >>>
  • >>> x = “this is a truthy value”
  • >>> x is True
  • False
  • >>> bool(x) is True
  • True
  • >>> y = “”  # This is falsy
  • >>> y is False
  • False
  • >>> bool(y) is False
  • True

Notice that comparing a truthy value directly to True or False using is doesn’t work. You should directly compare a value to True or False only if you want to know whether it is actually the values True or False.

When writing conditional statements that are based on the truthiness of a value, you should not compare directly to True or False. You can rely on Python to do the truthiness check in conditionals for you:

  • >>>
  • >>> x = “this is a truthy value”
  • >>> if x is True:  # Don’t do this
  • …    print(“x is True”)
  • >>> if x:  # Do this
  • …    print(“x is truthy”)
  • x is truthy

In Python, you generally don’t need to convert values to be explicitly True or False. Python will implicitly determine the truthiness of the value for you.

The None Keyword

The Python keyword None represents no value. In other programming languages, None is represented as null, nil, none, undef, or undefined.

None is also the default value returned by a function if it doesn’t have a return statement:

Python Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download
  • >>>
  • >>> def func():
  • …    print(“hello”)
  • >>> x = func()
  • hello
  • >>> print(x)
  • None

Conclusion

Python keywords are the fundamental building blocks of any Python program. Understanding their proper use is key to improving your skills and knowledge of Python.

Throughout this article, you’ve seen a few things to solidify your understanding Python keywords and to help you write more efficient and readable code.

In this article, you’ve learned:

  • The Python keywords in version 3.8 and their basic usage
  • Several resources to help deepen your understanding of many of the keywords
  • How to use Python’s keywords module to work with keywords in a programmatic way

If you understand most of these keywords and feel comfortable using them, then you might be interested to learn more about Python’s grammar and how the statements that use these keywords are specified and constructed.

Are you looking training with Right Jobs?

Contact Us

Popular Courses