• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Keywords in Python

Keywords in Python

Author: PFB Staff Writer
Last Updated: May 25, 2020

What are keywords?

Keywords in Python are reserved words that cannot be used as ordinary
identifiers. They must be spelled exactly as they are written.

List of keywords

The following is a list of keywords for the Python programming language.

anddelfromnot
whileaselifglobal
orwithassertelse
ifpassyieldbreak
exceptimportprintclass
execinraisecontinue
finallyisreturndef
forlambdatry

This prints the keyword list of Python.

$ python
>>> 
>>> import keyword

>>> print keyword.kwlist

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else',
'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with',
'yield']

Keywords Explained

print
print to console

while
controlling the flow of the program

for
iterate over items of a collection in order that they appear

break
interrupt the (loop) cycle, if needed

continue
used to interrupt the current cycle, without jumping out of the whole cycle.
New cycle will begin.

if
used to determine, which statements are going to be executed.

elif
stands for else if.If the first test evaluates to False,
then it continues with the next one

else
is optional. The statement after the else keyword is executed,
unless the condition is True

is
tests for object identity

not
negates a boolean value

and
all conditions in a boolean expression must be met

or
at least one condition must be met.

import
import other modules into a Python script

as
if we want to give a module a different alias

from
for importing a specific variable, class or a function from a module

def
used to create a new user defined function

return
exits the function and returns a value

lambda
creates a new anonymous function

global
access variables defined outside functions

try
specifies exception handlers

except
catches the exception and executes codes

finally
is always executed in the end. Used to clean up resources.

raise
create a user defined exception

del
deletes objects

pass
does nothing

assert
used for debugging purposes

class
used to create new user defined objects

exec
executes Python code dynamically

yield
is used with generators

More reading

For a more detailed explanation and examples of keywords, please see:
http://zetcode.com/lang/python/keywords/

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us