BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Keywords and Identifiers with examples

Last Updated: March 29, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

In this article, we will discuss Keywords and Identifiers in Python with the help of examples.

What is a Python Keyword?

Python keywords
A python keyword is a reserved word which you can’t use as a name of your variable, class, function etc. These keywords have a special meaning and they are used for special purposes in Python programming language. For example – Python keyword “while” is used for while loop thus you can’t name a variable with the name “while” else it may cause compilation error.

There are total 33 keywords in Python 3.6. To get the keywords list on your operating system, open command prompt (terminal on Mac OS) and type “Python” and hit enter. After that type help() and hit enter. Type keywords to get the list of the keywords for the current python version running on your operating system.

Chaitanyas-MacBook-Pro:~ chaitanyasingh$ Python 3.6
...
>>> help()
Welcome to Python 3.6's help utility!
...
help> keywords

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

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

Example of Python keyword

In the following example we are using while keyword to create a loop for displaying the values of variables num as long as they are greater than 5.

num = 10
while num>5:
    print(num)
    num -= 1

Output:
Python keyword example

Python Identifiers

In the last article, we discussed about variables in Python.

Variable name is known as identifier. There are few rules that you have to follow while naming the variables in Python.

For example here the variable is of integer type that holds the value 10. The name of the variable, which is num is called identifier.

num = 10

1. The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all valid name for the variables.
2. The name of the variable cannot start with a number. For example: 9num is not a valid variable name.
3. The name of the variable cannot have special characters such as %, $, # etc, they can only have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _ ).
4. Variable name is case sensitive in Python which means num and NUM are two different variables in python.

Python identifier example

In the following example we have three variables. The name of the variables num, _x and a_b are the identifiers.

# few examples of identifiers

num = 10
print(num)

_x = 100
print(_x)

a_b = 99
print(a_b)

Output:
Python Identifiers Example

❮ PreviousNext ❯

Top Related Articles:

  1. Python Recursion
  2. Python Constructors – default and parameterized
  3. Python User defined Functions
  4. Python Function Arguments – Default, Keyword and Arbitrary
  5. Python Data Types

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. edwin gabula says

    May 7, 2020 at 9:24 PM

    examples of other keywords

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap