Thursday, November 12, 2020

, ,

Example for getattr() function in python

 getattr() function in python is sued to get the value of an object attribute and it return the default value if no attribute of that object is found.

getattr() function in python
Example for getattr() function in python

 

The reason of using getattr() function because it return the default value.Now let see the basic syntax : -

getattr(object_name, attribute_name[, default_value])

Example of getattr() function in python

this program will help you to teach about how to use the getattr() function in python

class Student:
    student_id=""
    student_name=""

    # initial constructor to set the values
    def __init__(self):
        self.student_id = "101"
        self.student_name = "Adam Lam"

student = Student()
# get attribute values by using getattr() function
print('\ngetattr : name of the student is =', getattr(student, "student_name"))

# but you could access this like this
print('traditional: name of the student is =', student.student_name)

Why use getattr() function

  • This function helps to get the object attribute value with the help of Name of that attr. or you can also manually set the input attribute name by console.
  • This function also give the freedom for set the default value according to your need.

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))