Python Keywords and Identifiers Tutorial

Ads
 

Python Keywords and Identifiers Tutorial

In this Python tutorial we are going to familiarize you with the Keywords and Identifiers in n the Python programming language.

In this Python tutorial we are going to familiarize you with the Keywords and Identifiers in n the Python programming language.

Python Tutorials: Learn Keywords and Identifiers in Python Programming Language

Learning Keywords and Identifiers of Python is the next thing that you should learn in Python programming language. In the previous section we have seen the steps to write and test "Hello World" program in Python. Now in this section we will learn keywords and identifiers of Python Programming language.

In this section we are covering:

  • Python keywords
  • Python identifiers
  • Rules of defining keywords and identifiers

Python Keywords

There are certain reserve words in Python programming which can't be used for creating classes, variable and functions. These words are known as keywords in Python programming language. If you use these keywords in writing Python program then programming language will give error and program will not run. So, you should learn about these Python Keywords before proceeding to next sections and learn Python programming language.

In Python programming language there are 33 keywords which is used in writing program. These keywords are used for defining the structure of the program; for example to write for loop in python you have to use the for keyword.

Here is the list of Python keywords:

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

help>

These keywords are reserved to the Python programming language and can't be used by developers to declare variables, functions or class names. You can to go help by typing help() on the Python console and then type keywords as shown below:

******

Here is an example of for keyword to define for loop in Python programming language:

>>> for x in range(1, 10):
...   print(x)
...
1
2
3
4
5
6
7
8
9
>>>

Above code is an example of for loop in python which prints 1 to 10 on the console.

Python identifiers

Identifiers are the name given to class, variables, functions and other objects in Python programming language. There are naming conversions in Python for identifiers. Any name which starts with letters (A to Z including lowercase) or with underscore (_) and it is followed by zero or more letters/number/underscores. Python does not allow punctuation characters such as @, $, and % within identifiers.

Python programming language is case sensitive programming language following two variables names are not same:

Country ="USA"

country ="ÜSA"

Here two variables Country and country are not same, they are two different variables.

In Python you can't use reserve words to define identifiers. Here are the list of reserve words that you should know:

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

It is noted that all the reserve words are in lower case.

Variables in Python

Variables are the memory location identified by a name whose values can be changed anytime during the execution of the program. Variables exists only during the execution of the program and vanishes when program competes its execution period.

Here is example of creating an integer variable in python:

counter = 10

You can change the value of variable any number of time during the execution of program.

If you have a variable and want to know the type of variable then you can use the type() function as shown below:

counter = 10
type(counter)

If you run above program then system will display the type of counter variable:

<class 'int'>

In this section we have understood python keywords and identifiers. You can check more tutorials at Python Tutorials section.

Ads

Ads