Sunday, 17 September 2017

2)Python Keywords and Identifiers

Python Keywords and Identifier:

Python Keywords:

  • Keywords are the reserved words in Python.
  • keywords are case sensitive.
  • There are 33 keywords in Python 3.3.

Note:

  • We cannot use a keyword as variable name, function name or any other identifier.
  • All the keywords except True, False and None are in lowercase

Keywords in Python programming language:


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

break
except
in
raise

To list python keywords in CLI:
import keyword
print(keyword.kwlist)

Keywords in Python with examples:
i)True, False

They are the results of comparison operations or logical (Boolean) operations in Python. simply called Truth values

Example:


Note: 

True and False in python is same as ==> 1 and 0.
>>> True == 1True>>> False == 0True

ii)None

None is a special constant in Python that represents the absence of a value or a null value.

Example:

x=null
iii)and, or , not

and, or, not are the logical operators in Python

*and 
will result into True only if both the operands are True:


Truth Table for and:


A
B
A and B
True
True
True
True
False
False
False
True
False
False
False
False

*or 
will result into True if any of the operands is True.


Truth table for or:


A
B
A or B
True
True
True
True
False
True
False
True
True
False
False
False

*not operator 
is used to invert the truth value.


Truth table for not:


A
not A
True
False
False
True

Examples:
>>> True and FalseFalse>>> False or TrueTrue>>> not FalseTrue>>> not TrueFalse


iv)as
as is used to create an alias while importing a module. 

Note:
It means giving a different name (user-defined) to a module while importing it.

Example:
>>> import math as mathfunctions
>>> print(mathfunctions.pi)
3.141592653589793


v)assert
assert is used for debugging purposes.

while programming , check if our assumptions are true. assert helps us do this and find bugs more conveniently.

Syntax:
assert condition
or
assert condition , message
Note:
If the condition is true, nothing happens. But if the condition is false, AssertionError is raised.

Example:
>>> a = 6
>>> assert a > 10 , "A is small ,less than 10"
Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    assert a > 10 , "A is small ,less than 10"
AssertionError: A is small ,less than 10
>>> assert a > 5 , "A is small"
vi)break, continue

break and continue are used inside for and while loops to alter their normal behavior.

break - will end the whole loop and gets out.
continues - will end the current iteration of the loop
Example for break:
Once if the number is 5, then it gets out from the entire loop



Example for continue:
Once if the number is 5, then it gets out only the current iterations and continues further,



vii)class
class is used to define a new user-defined class in Python.
  • Class is a collection of related attributes and methods that try to represent a real world situation. 
  • This idea of putting data and functions together in a class is central to the concept of object-oriented programming (OOP).
Syntax:
class ExampleClass:
    def function1(parameters):
        …
    def function2(parameters):
        …
viii)def
def is used to define a user-defined function.

Syntax:
def function_name(parameters):
    …
ix)del
del is used to delete the reference to an object. Everything is object in Python. We can delete a variable reference using del



x)if, else, elif
if, else, elif are used for conditional branching or decision making.

Note:

Based on the condition to execute block of statement



xi)except, raise, try
except, raise, try are used with exceptions in Python.

xii)finally

finally is used with try…except block to close up resources or file streams.

xiii)for
for is used for looping(repetition of execution until particular condition)




xiv)from, import
import keyword is used to import modules into the current namespace. from…import is used to import specific attributes or functions into the current namespace.

Example:
import math
from math import cos
xv)global
global is used to declare that a variable inside the function is global (outside the function).




xvi)in
in is used to test if a sequence (list, tuple, string etc.) contains a value. It returns True if the value is present, else it returns False.



xvii)is
is is used in Python for testing object identity. While the == operator is used to test if two variables are equal or not, 

Note:
is used to test if the two variables refer to the same object.

Example:
>>> True is True
True
>>> False is False
True
>>> None is None
True
xviii)lambda
lambda is used to create an anonymous function (function with no name). It is an inline function that does not contain a return statement. It consists of an expression that is evaluated and returned.

Syntax:
lambda arguments: expression


xix)nonlocal
The use of nonlocal keyword is very much similar to the global keyword. nonlocal is used to declare that a variable inside a nested function (function inside a function) is not local to it, meaning it lies in the outer inclosing function.

Note:

  • If we need to modify the value of a non-local variable inside a nested function, then we must declare it with nonlocal. 
  • Otherwise a local variable with that name is created inside the nested function.  


xx)pass
pass is a null statement in Python. Nothing happens when it is executed. It is used as a placeholder.

Suppose we have a function that is not implemented yet, but we want to implement it in the future. Simply writing,
def function(args):


in the middle of a program will give us IndentationError. Instead of this, we construct a blank body with the pass statement.

def function(args):
    pass
can do the same thing in an empty class as well.
class example:
    pass
xxi)return
return statement is used inside a function to exit it and return a value.

Note:
If we do not return a value explicitly, None is returned automatically.



xxii)while
The statements inside a while loop continue to execute until the condition for the while loop evaluates to False or a break statement is encountered.


Note:
Note that 0 is equal to False.

xxiii)with
with statement is used to wrap the execution of a block of code within methods defined by the context manager.

xxiv)yield
yield is used inside a function like a return statement. But yield returns a generator.

Note:

  • Generator is an iterator that generates one item at a time. A large list of value will take up a lot of memory. 
  • Generators are useful in this situation as it generates only one value at a time instead of storing all the values in memory. 

Python Identifiers:
Identifier is the name given to entities like class, functions, variables etc. in Python.

Rules for writing identifiers:

  1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). 
  2. Names like myClass, var_1 and print_this_to_screen, all are valid example.
  3. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
  4. Keywords cannot be used as identifiers.
  5. Identifier can be of any length.

Previous page                                                                                                                  Next page

4 comments:

  1. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info
    วิธีแทงบอล

    ReplyDelete

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...