Python Keywords and Identifiers (Updated)

Filed Under: Python

Let’s talk about Python keywords and identifiers. We recently also covered a complete tutorial on installing and setting up Python for beginners in this Python tutorial.

Python Keywords

Well simply, Python keywords are the words that are reserved. That means you can’t use them as name of any entities like variables, classes and functions.

So you might be thinking what are these keywords for. They are for defining the syntax and structures of Python language.

You should know there are 33 keywords in Python programming language as of writing this tutorial. Although the number can vary in course of time. Also keywords in Python is case sensitive. So they are to be written as it is. Here is a list of all keywords in python programming.

python keywords, keywords in python, python 3 keywords

If you look at all the keywords and try to figure out all at once, you will be overwhelmed. So for now just know these are the keywords. We will learn their uses respectively. You can get the list of python keywords through python shell help.

List of All Python Keywords

andLogical operator
asAlias
assertFor debugging
breakBreak out of Python loops
classUsed for defining Classes in Python
continueKeyword used to continue with the Python loop by skipping the existing
defKeyword used for defining a function
delUsed for deleting objects in Python
elifPart of the if-elif-else conditional statement in Python
elseSame as above
exceptA Python keyword used to catch exceptions
FALSEBoolean value
finallyThis keyword is used to run a code snippet when no exceptions occur
forDefine a Python for loop
fromUsed when you need to import only a specific section of a module
globalSpecify a variable scope as global
ifUsed for defining an “if” condition
importPython keyword used to import modules
inChecks if specified values are present in an iterable object
isThis keyword is used to test for equality.
lambdaCreate anonymous functions
NoneThe None keyword represents a Null value in PYthon
nonlocalDeclare a variable with non-local scope
notLogical operator to negate a condition
orA logical operator used when either one of the conditions needs to be true
passThis Python keyword passes and lets the function continue further
raiseRaises an exception when called with the specified value
returnExits a running function and returns the value specified
TRUEBoolean value
tryPart of the try…except statement
whileUsed for defining a Python while loop
withCreates a block to make exception handling and file operations easy
yieldEnds a function and returns a generator object

Below is a simple example showing usage of if-else in python program.

var = 1;

if(var==1):
    print("odd")
else:
    print("even")

When we run the above program, Python understands the if-else block because of fixed keywords and syntax and then do the further processing.

What are Python Identifiers?

Python Identifier is the name we give to identify a variable, function, class, module or other object. That means whenever we want to give an entity a name, that’s called identifier.

Sometimes variable and identifier are often misunderstood as same but they are not. Well for clarity, let’s see what is a variable?

What is a Variable in Python?

A variable, as the name indicates is something whose value is changeable over time. In fact a variable is a memory location where a value can be stored. Later we can retrieve the value to use. But for doing it we need to give a nickname to that memory location so that we can refer to it. That’s identifier, the nickname.

Rules for Writing Identifiers

There are some rules for writing Identifiers. But first you must know Python is case sensitive. That means Name and name are two different identifiers in Python. Here are some rules for writing Identifiers in python.

  1. Identifiers can be combination of uppercase and lowercase letters, digits or an underscore(_). So myVariable, variable_1, variable_for_print all are valid python identifiers.
  2. An Identifier can not start with digit. So while variable1 is valid, 1variable is not valid.
  3. We can’t use special symbols like !,#,@,%,$ etc in our Identifier.
  4. Identifier can be of any length.

Though these are hard rules for writing identifiers, also there are some naming conventions which are not mandatory but rather good practices to follow.

  1. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
  2. Starting an identifier with a single leading underscore indicates the identifier is private.
  3. If the identifier starts and ends with two underscores, than means the identifier is language-defined special name.
  4. While c = 10 is valid, writing count = 10 would make more sense and it would be easier to figure out what it does even when you look at your code after a long time.
  5. Multiple words can be separated using an underscore, for example this_is_a_variable.

Here’s a sample program for python variables.

myVariable="hello world"
print(myVariable)

var1=1
print(var1)

var2=2
print(var2)

If you run the program, the output will be like below image.

python identifier example output

Conclusion

So, that’s it for today. In the next tutorial, we will learn about Python Statements and Comments. Till then #happy_coding 馃檪

Comments

  1. JULIO says:

    Well done Pankaj! I read and watched a lot of blogs and tutorials and it is the best! Clear, concise and full of information in a simple way. Thanks for your help! I’m 64, next to retirement and learning Python to keep my neurons awake!!!!

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content