Skip to main content

Python #6-Keywords in Python

cont....
          of Python #5 - Variables and Strings in Python

What are Keywords?
Keywords are the reserved for special purpose.
It can't be used as variables name or identifiers.

Keywords in Python:

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

Brief of Keywords:
_________________________________________________________________________________

True , False : 
It is used for the comparison operations or logical operations.

Example:
     >>> 10 == 10
     True
     >>> 10 > 20
     False
_________________________________________________________________________________

Class :
Class keyword is used to define a new user defined class in python.

Syntax:
    class className:
             def functionName1(parameters):
                   ........
             def functionName2(parameters):

def:
def keyword is used to define a user defined function.
In python if you are going to create a function it should be started with the ' def ' Keyword.

Syntax: 
    def functionName(parameters):
           ........
_________________________________________________________________________________
del:
del keyword is used to delete the reference to an object in python.
we can able to delete the object easily in python once it is declared.

Example:
     a = 10
     print a                                         #Output will be 10.
     del a
     print a                                         #This will show error message. " a is not defined "
_________________________________________________________________________________
if, else, elif :
if, else, elif are used as an conditional operation or for decision making statements.

Example:
     a = 10
     if a < 0 :
        print "Greater than Zero"
     elif  a == 10:
        print " The number is Ten "
     else :
        print "Invalid data"

Output:
     The number is Ten

Note Worthy:  Change the a=10 value and see the difference of output changes.
_________________________________________________________________________________
break, continue :
break and continue used inside the looping statements to alter their normal behavior.

break will end the loop it is in and control flow to the next immediate loop statement.
continue will end the current iteration of the loop , but not the whole loop .

Example for break:
for x in range(1,10):
    if x == 5:
       break
    print x

Output :
    1
    2
    3
    4

#Here for loop will start its process .It will check for the If statement x == 5 once the value is detected the break inside the if will be executed. So once the break is identified it will come out from the for loop so the above program will print the output model so the output comes as 1,2,3,4.

Example for continue:
for x in range(1,10):
    if x == 5:
       continue
    print x

Output:
    1
    2
    3
    4
    6
    7
    8
    9

 #Here for loop will start its process .It will check for the If statement x == 5 once the value is detected the continue inside the if will be executed. So once the continue is identified it will come out " Skip the current iteration" from the for loop. and goes for the next iteration. so the above program will print the output model so the output comes as 1,2,3,4,6,7,8,9.
_________________________________________________________________________________
None:
None keyword is a special constant in python which is used to represent the absent of value .

Example:
    >>>a=None
    >>>b=None
    >>>a==b
   True
________________________________________________________________________________

Try and Learn....               Work space
_________________________________________________________________________________
and, or, not:
and , or, not are the logical operators. As these operators will follow their truth tables.

Example:
    >>>True and False
    False
    >>>True or False
    True
    >>>not False
    True
_________________________________________________________________________________
as :
as keyword is used as an Alias variable .
It means giving a different name to the module while importing it.

Example:
    import math as x
    print x.cos(0)
Output:
    1
_________________________________________________________________________________
assert :
assert keyword is used for debugging purposes.

Syntax:
assert condition , "Error statements"

Example:
    a=10
    assert a <9, "values is smaller than actual value"

Error:
AssertionError: value is smaller than actual value on line 2 in main.py

Note Worthy: This assert keyword is used for debugging where we can specify the error message . 
_________________________________________________________________________________
from, import :
import keyword is used to import the modules into the current namespace.
from is used to import specific attributes or functions.

Example:
Suppose we need only cos operation from the math function why should we call whole math function.Instead of calling we use this from keyword .

from math import cos

Now we can able to use this in our program as like math.cos(). it works in efficient way.
_________________________________________________________________________________
global :
Global keyword is used to declare that a variable inside the function is global .It is like global variable in c, c++ , Java we used.. As it works but here the global keyword is specified .

Example:
     globvar = 10
     def fun1():
         print globvar
     def fun2():
         print globvar

     fun1()
     fun2()
Ouput:
     10
     10
_________________________________________________________________________________
in :
in keyword is used to test the if a sequence contains those values.

Example:
    a=[1, 2, 3, 4, 5]
    print 5 in a
Output:
    True
_________________________________________________________________________________
is:

  • is keyword is used to testing object identity.
  • is is used to test if the two variables refer to the same object.
  • it returns True of the object are same(identical) and False if not same(not identical).

Example:
     a='A'
     print a is 'B'

Output:
     False

#This is keyword will return the Boolean values either true or false in output.
_________________________________________________________________________________
lambda :

  • lambda keyword is used to create an anonymous function .
  • it is also called as a function with no name.

Example:
    a = lambda x, y : x * y
    print a (2,2)

Output:
     4
Brief:
   Here lambda acts as an anonymous function to which we able to pass the values to perform the operation.
_________________________________________________________________________________
non local:
non local keyword is similar to that of the Global keyword.
_________________________________________________________________________________
pass:
pass keyword is used for null statement in python.
it is place holder. Nothing will happen because of the keyword.

Example:
    def fun1():
         pass
_________________________________________________________________________________
Yield:
yield keyword is used inside a function like a return statement.
It do some operation like generator.

Example:
    a=(2*x for x in range(1,6))
    print next(a)
Output:
    2
Note Worthy: It only returns only one values at a time.
_________________________________________________________________________________


Thanks and Regards,
Tech Bird

Comments