Sunday, November 8, 2020

, ,

Example Program for delattr() function in python

Python delattr() function used to delete an attribute from an object.

Syntax For delattr :-

delattr(object, name)

delattr () Parameters in Pyhton

There two attribute which is used in Delattr () function in python:-

  • First one is object where name attribute is removed from that object

  • second one is name it can be string which is removed from the object.

  • delattr() function in python doesn't return any value.

Python getattr Example Program in python:-

class Coordinate:
  x = 10
  y = -5
  z = 0

point1 = Coordinate() 

print('x = ',point1.x)
print('y = ',point1.y)
print('z = ',point1.z)

delattr(Coordinate, 'z')

print('--After deleting z attribute--')
print('x = ',point1.x)
print('y = ',point1.y)

# Raises Error
print('z = ',point1.z)

OUTPUT : -

Program For delattr() function in python
Program For delattr() function


Saturday, November 7, 2020

, ,

Example Program for complie() function in python - Python Program

 

 The complie() function in python is used to return python code object from a source like (string,AST object,byte string).

Syntax For Complie(): -

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)


Well the return object from the compile function in python can be called by exec() and eval() method.

Parameter : -

  • A source that can be used in compile function that can be normal string, a byte string, or an AST object.

  • A code that can be read from a file. If it doesn't read form a file then you give a name according to you.
  • Either exec,eval or signal method can be used to call a function.
  • Well eval accept only a single expression.
  • exec take block of code with python statement like class function.
  • it contain single interactive statement.

 flags (optional) and dont_inherit : - if any problem stop to compile the source then it raise flag. default value : -0

 optimize (optional) - optimization level with Default value -1 for compiler.

Example Program for complie() function
Example Program for complie() function

 

Example program for compile() function in python : -

codeInString = 'a = 5\nb=6\nsum=a+b\nprint("sum =",sum)'
codeObejct = compile(codeInString, 'sumstring', 'exec')

exec(codeObejct)

OUTPUT

sum = 11

compile function in python convert the string into a python code object and then execute with exec() method

Wednesday, November 4, 2020

,

Example Program For enumerate() function in python

 enumerate() function in python adds counter to a iterable and returns it in the enumerate python program.

Syntax : -

enumerate(iterable, start=0)
  • Two parameters are used the first one is an object which is accept the iteration.
  • The second one is start parameters which is a starting point of counting but it's optional. If start is extinct the it's take 0 at start.
  •   enumerate() function return the object after adding the counter to an iterable.

Example Program for enumerate() in Python


grocery = ['bread', 'Tea', 'jam']
enumerateGrocery = enumerate(grocery)

print(type(enumerateGrocery))

# converting the gienvalue into a list
print(list(enumerateGrocery))

# changing the default counter
enumerateGrocery = enumerate(grocery, 10)
print(list(enumerateGrocery))

OUTPUT

Program for enumerate in Python
Program for enumerate() in Python



Tuesday, November 3, 2020

, , ,

Example Program For Complex Function In Python - Python Program

  •  Well Python not only handle the real number but also handle the complex numbers with the help of Complex function in python.
  • This complex function is so useful to manipulate mathematics problem's.
  • complex function in python return the real number from complex number or convert the string to a complex number. 
Example Program For Complex Function In Python
Example Program For Complex Function In Python

 

 Parameter : -

  •     real - real part. If real is extinct then it defaults to 0.
  •     imag - imaginary part. If imag is extinct then it defaults to 0.

Return the complex number.

Example of Complex function in Python To Create a complex number:-

z = complex(2, 3)
print(z)

z = complex(2)
print(z)

z = complex()
print(z)

z = complex('4-5j')
print(z)
(2+3j)
(2+0j)
0j
(4-5j)

Monday, November 2, 2020

, ,

Example Program for vars() function in python - Python Example Program

 vars() function in python return the dictionary attribute of an object.

Example Program for vars function in python
Example Program for vars() function in python

 

  • vars() can just take one object that can be module, class, instance, or any object which  having the __dict__ attribute in python program.
  • If the any object passed to vars() function doesn't  have the __dict__ attribute then it give an TypeError exception.
  • If any argument doesn't pass to vars() function then it act like an locals() function.

Example Program For vars function in python : -


class Foo:
  def __init__(self, a = 5, b = 10):
    self.a = a
    self.b = b
  
object = Foo()
print(vars(object))

OUTPUT

{'a': 5, 'b': 10}

Sunday, November 1, 2020

, ,

Example program for Any function in python - Python Example Program

 Any function in python programming number of iterable is used and return the true if any element of any iterable is true or return the false.

Example program for Any function in python
 Any function in python

 

Syntax of any function in python

any(iterable)


Any function in python take iterable (string, dictionary etc.) as a Parameters.

Return : -

it's Return the boolean value.

  •  True if any one element is true
  •  Return the false if all the element are false.

Example program for any() function in python : -


l = [1, 3, 4, 0]
print(any(l))

l = [0, False]
print(any(l))

l = [0, False, 5]
print(any(l))

l = []
print(any(l))

True
False
True
False

Example program for any function on Python Strings

s = "This is best"
print(any(s))

# 0 is False
# '0' is True since its a string character
s = '000'
print(any(s))

# False since empty iterable
s = ''
print(any(s))

Friday, October 30, 2020

,

Example program for slicing in python - python __slice__

  •  slice() function in python is used to sliced a given sequence or given object 
  • There are three parameters the first one is an starting point which is optional.
  • second parameter for slice function tells about the ending point.
  • Third one will tell the increment between each index.
Syntax:- 
slice(start, stop, step)
  • Create a slice object for slicing

result1 = slice(3)
print(result1)

)
result2 = slice(1, 5, 2)
print(slice(1, 5, 2))
slicing program in python
Example program for slicing


Program for slicing sub-string

py_string = 'Python'

slice_object = slice(3) 
print(py_string[slice_object])  # Pyt

slice_object = slice(1, 6, 2)
print(py_string[slice_object])   # yhn


  • Example program for negative slicing in python

py_str = 'Python'


slice_object = slice(-1, -4, -1)

print(py_str[slice_object])   # noh

In this program we use negative value for slicing the object
  • Example program for sublist and sub-tuple

py_l = ['P', 'y', 't', 'h', 'o', 'n']
py_t = ('P', 'y', 't', 'h', 'o', 'n')


slice_object = slice(3)
print(py_l[slice_obj]) # ['P', 'y', 't']

slice_object = slice(1, 5, 2)
print(py_t[slice_object]) # ('y', 'h')