Friday, 27 January 2017

Python Keywords and Identifiers

Keywords in Python

Keywords in python are the reserved words that cannot be used as variable name or function name or ordinary identifiers; they must be spelled exactly as they are written.

Explanation of Keywords

print – Used to print to console.

while – Used for controlling the flow of the program.

for – Used to iterate over times of a collection in order that they present.

break – Used to interrupt the loop or cycle when they are needed.

continue – Used to interrupt the current cycle without jumping out of the whole cycle.

if – Used to determine which statements are to be executed.

elif – This stands for else if. In this the first test evaluates to false then it continues with the next one.

else – It is an optional one, the else keyword is executed, unless the condition is true.

is – Used for testing the object identity.

not – This is used to negates the Boolean value.

and -  By using this all conditions in the Boolean expressions are met.

or – By using this atleast one condition must meet.

import – With this import the other python modules into a python script.

as – By using this give module in different alias.

from -  Used for importing a specific variable, class or a function from a module.

def – Used for creating an new user defined function.

return – Used for exits the function and returns a value.

lambda – Used to create a new anonymous function.

global – Used to define the outside functions and access the variables.

try – With the help of this find the exception handlers.

expect – Used for executing the code and then catches the exception.

finally – With this clean up resources and its always executed at the end.

raise – Used to create an user defined exception.

del – Used to delete objects.

pass – It does nothing.

assert – Its used for the debugging process.

class – This is used for creating the new user defined objects. 

exec – Used for executing the python code dynamically.

yield – This is used along with the generators.  

Python Identifiers

Identifier is the name given to the entities like class, functions, variables etc. which helps differentiating one entity from another.

Rules for Writing Identifiers

1) Identifiers is a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_1 and print_this_to_screen, are valid examples.

2)An identifier cannot start with a digit.1variable is invalid, but variable is perfectly fine.

3)Keywords cannot be used as identifiers.

>>>global = 1
File “<interactive input>”, line 1
global = 1
^

Syntax Error: invalid syntax

4)Does not use special symbols like !, @, #, $, % etc. in our identifier.

>>> a@ = $
File “<interactive input>”, line 1
a@ = $
^

Syntax Error: invalid syntax

5)Identifier can be of any length.


No comments:

Post a Comment