Python Keywords

Python · Getting Started

Theory

Keywords are words that Python reserves for its own grammar, such as if, else, for, while, def, class, return, import, True, False and None. The parser recognises them before anything else, so you cannot use a keyword as a variable, function or class name: writing class = 5 is a SyntaxError. The standard library module keyword holds the full list in keyword.kwlist, and keyword.iskeyword('for') checks a single word.

A few words are soft keywords, for example match and case. They act as keywords only in specific positions (inside a match statement) and remain usable as ordinary names elsewhere, which is why keyword.iskeyword('match') returns False. Also note that common names like print, len, sum, list and str are not keywords at all; they are built-in functions and types, so Python lets you reuse them, but doing so hides the original.

Why we use it

Keywords are the fixed vocabulary that gives Python code its structure. Knowing them helps you read any program, understand SyntaxError messages, and avoid choosing a variable name the language already owns.

When to use it

Check keyword.kwlist or keyword.iskeyword() when a name gives a strange SyntaxError. Avoid reusing built-in names like list, sum or str as variables too; they are not keywords, but shadowing them causes confusing bugs later.

How it works

  1. You type a name, e.g. class = 5
  2. The parser checks the word against the keyword list
  3. Is it a hard keyword? Yes: SyntaxError: the word is reserved. No: Treat it as an ordinary name.
  4. Is it a built-in like list or sum? Yes: Allowed, but it hides the built-in from now on. No: Safe to use.
  5. Name is bound to the value

Syntax

import keyword
keyword.kwlist

Example

import keyword
print(keyword.kwlist)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic