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

,

Program for perror function in c - library function

 perror is a c library function in c this function is used to print a descriptive error message in c.

Program for perror library function in c
Program for perror function

 

Syntax:-

void perror(const char *str)

  • str in c contain a custom message that is printed before the error message itself.
  • perror function in c doesn't return any value

Example Program for perror function in c

#include <stdio.h>

int main () {
   FILE *f;

 
   /* first rename the file */
   rename("file.txt", "updatefile.txt");

   /* now let's open same file */
   f = fopen("file.txt", "r");
   if( fp == NULL ) {
      perror("Error: ");
      return(-1);
   }
   fclose(fp);
      
   return(0);
}

Output

Error: : No such file or directory

Tuesday, October 27, 2020

,

Example program for tmpfile() function in c programming

  •  tmpfile() function in c programming is used to created the temporary file in program and this temporary file will be deleted after  termination of c program.
  • wb+ mode is used to opens file in binary update 
Example program for tmpfile() in c
Example program for tmpfile() function in c programming

 

Syntax :- 

FILE *tmpfile(void) 
  • this function return the file after creating the temporary file.

Program example for tmpfile() function in c


#include <stdio.h>
int main() 
{ 
    char str[] = "Hello function in c"; 
    int i = 0; 
  
    FILE* tmp = tmpfile(); 
    if (tmp == NULL) 
    { 
        puts("Unable to create temp file"); 
        return 0; 
    } 
  
    puts("Temporary file is created\n"); 
    while (str[i] != '\0') 
    { 
        fputc(str[i], tmp); 
        i++; 
    } 
  
    // rewind() function sets the file pointer 
    // at the beginning of the stream. 
    rewind(tmp); 
  
    while (!feof(tmp)) 
        putchar(fgetc(tmp)); 
} 
OUTPUT : -

Temporary file is created
Hello function in c