Monday, June 15, 2020

Python Keywords: An Introduction

Every programming language has special reserved words, or keywords, that have specific meanings and restrictions around how they should be used. Python is no different. Python keywords are the fundamental building blocks of any Python program.

In this article, you’ll find a basic introduction to all Python keywords along with other resources that will be helpful for learning more about each keyword.

By the end of this article, you’ll be able to:

  • Identify Python keywords
  • Understand what each keyword is used for
  • Work with keywords programmatically using the keyword module

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.

An example of something you can’t do with Python keywords is assign something to them. If you try, then you’ll get a SyntaxError. You won’t get a SyntaxError if you try to assign something to a built-in function or type, but it still isn’t a good idea. For a more in-depth explanation of ways keywords can be misused, check out Invalid Syntax in Python: Common Reasons for SyntaxError.

As of Python 3.8, there are thirty-five keywords in Python. Here they are with links to the relevant sections throughout the rest of this article:

You can use these links to jump to the keywords you’d like to read about, or you can continue reading for a guided tour.

How to Identify Python Keywords

The list of Python keywords has changed over time. For example, the await and async keywords weren’t added until Python 3.7. Also, both print and exec were keywords in Python 2.7 but have been turned into built-in functions in Python 3+ and no longer appear in the list of keywords.

In the sections below, you’ll learn several ways to know or find out which words are keywords in Python.

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.

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               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

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)

Read the full article at https://realpython.com/python-keywords/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...