Showing posts with label function program in python. Show all posts
Showing posts with label function program in python. Show all posts

Friday, November 27, 2020

,

ord()Function in python with it's example

 ord function in python is used to return an integer representing of Unicode character.

Syntax:-

ord(ch)

Parameters For ord function in python

This function take just only one perameter and that known as ch (a Unicode character)

Return value

The ord() function in python returns an integer representing the Unicode character.

Example for ord()Function in python

print(ord('5'))    # 53
print(ord('A'))    # 65
print(ord('$'))    # 36

OUTPUT

ord()Function
ord()Function in python

Tuesday, November 24, 2020

, , ,

Use of Range Function In Python With Program

 python range function in python return the sequence of numbers between the start integer value to the stop integer.

Syntax : -

range(stop)
range(start, stop[, step])

Parameters For range() function in python:-

Range function takes the three arguments which is given below :-

  •  first one is a start point which define the starting point of range function.
  • second one is stop point integer which help to tell the end point for the sequence.
  • Step is a third one and it use to define the increment for the sequence of number.

Return value from range function in python: -

  •  range() function return the immutable sequence of number.
  • In this function sequence of number starts form 0 to Stop-1.
  • if the step argument is then it raise an Value Error.

How to use the range function in python with program

# empty range
print(list(range(0)))

# using range(stop)
print(list(range(10)))

# using range(start, stop)
print(list(range(1, 10)))

Output:

range function program in python
range function program in python


Wednesday, November 18, 2020

, , ,

frozenset() function in python With Example Code - Python Function

  •  The frozenset() function in python is an inbuilt function is Python.
  • That Function takes an iterable object as input and makes them immutable. 
  • it freezes the iterable objects and makes them unchangeable.
  • frozen sets can be helpful in  Dictionary it can be used as key because it remain the same after creation.

Parameter for the  frozenset() function in Python

it use a single parameter iterable which can be ( dictionary, tuple, etc.)

Return value : - 

  • The frozenset() function in python returns an immutable frozenset.
  • it return the empty frozenset only if no parameters are passed to this function.

Example code for frozenset function in python :- 


# tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')

fset = frozenset(vowels)
print('The frozen set is:', fset)
print('The empty frozen set is:', frozenset())

# frozensets are immutable
fSet.add('v')

OUTPUT

Example code for frozenset
Example code for frozenset

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