Home arrow Python arrow Page 3 - The Python Language

Keywords - Python

If you're already an experienced programmer and you're interested in adding Python to your list of languages, this nine-part series gives you a good start. It is excerpted from chapter four of the book Python in a Nutshell, Second Edition, written by Alex Martelli (O'Reilly; ISBN: 0596100469). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. The Python Language
  2. Character Sets
  3. Keywords
  4. Statements
By: O'Reilly Media
Rating: starstarstarstarstar / 1
September 11, 2008

print this article



Lexical Structure

Python has 30 keywords, which are identifiers that Python reserves for special syntactic uses. Keywords contain lowercase letters only. You cannot use keywords as regular identifiers. Some keywords begin simple statements or clauses of compound statements, while other keywords are operators. All the keywords are covered in detail in this book, either in this chapter, or in Chapters 5, 6, and 7. The keywords in Python are:











break class continue def

else except exec finally

global if import in

not or pass print

try while with (2.5) yield

The identifier with is a new keyword starting with Python 2.5 (up to Python 2.4, it is a completely normal identifier). The identifier as, which is not, strictly speaking, a keyword, is used as a pseudokeyword as part of some statements (specifically, the statements from, import, and, in Python 2.5, with). In Python 2.5, using with or as as normal identifiers produces warnings. To enable with usage as a keyword (and therefore to enable the new with statement) in Python 2.5, begin your source file with the statement:

  from __future__ import with_statement

This "import from the future" enables use of the with statement in this module.


Python uses nonalphanumeric characters and character combinations as operators. Python recognizes the following operators, which are covered in detail in "Expressions and Operators" on page 50:

Python uses nonalphanumeric characters and character combinations as operators. Python recognizes the following operators, which are covered in detail in Expressions and Operators on page 50:

  +   -   *   /   %   **  //   <<   >>   &
  |   ^   ~   <   <=  >   >=   <>   !=   ==


Python uses the following symbols and symbol combinations as delimiters in expressions, lists, dictionaries, various aspects of statements, and strings, among other purposes:

  (      )      [      ]      {      }
  ,      :      .      `      =      ;
  +=     -=     *=     /=     //=    %=
  &=     |=     ^=     >>=    <<=    **=

The period (.) can also appear in floating-point literals (e.g., 2.3) and imaginary literals (e.g., 2.3j). The last two rows list the augmented assignment operators, which lexically are delimiters but also perform an operation. I discuss the syntax for the various delimiters when I introduce the objects or statements with which they are used.

The following characters have special meanings as part of other tokens:

  '      "      #       \

The characters $ and ?, all control characters except whitespace, and all characters with ISO codes above 126 (i.e., non-ASCII characters, such as accented letters) can never be part of the text of a Python program, except in comments or string literals (to use non-ASCII characters in comments or string literals, you must start your Python source file with a "coding directive," as covered in "Character Sets" on page 34). This also applies to the character @ in Python 2.3; however, in Python 2.4, @ indicates decorators, as covered in "Decorators" on page 115.


A literal is a number or string that appears directly in a program. The following are all literals in Python: 

  42               # Integer literal
  3.14             # Floating-point literal
  1.0j             # Imaginary literal
  'hello'          # String literal
  "world"          # Another string literal
night"""         # Triple-quoted string literal

Using literals and delimiters, you can create data values of some other fundamental types:

  [ 42, 3.14, 'hello' ]     # List
  ( 100, 200, 300 )         # Tuple 
  { 'x':42, 'y':3.14 }      # Dictionary

The syntax for literals and other fundamental-type data values is covered in detail in "Data Types" on page 38, when I discuss the various data types Python supports.

>>> More Python Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: