Showing posts with label miscellaneous python function. Show all posts
Showing posts with label miscellaneous python function. Show all posts

Monday, June 1, 2020

Learn about the miscellaneous python function

Miscellaneous function in python:-

These all are the miscellaneous python function these functions are used some times or on the place of other functions. Some of them are removed from python 3 or some don't work on python 2 that's why I put them in the miscellaneous category.

Learn about the miscellaneous python function
Learn about the miscellaneous python function

reduce() Function : - 

reduce() function in python take the function and a sequence then returns a single value calculated as : - 

Step 1:- reduce function called for the two items from the sequence and gives the result.

Step 2: -  function called again for the two items from the sequence and the element will be the result which is obtained from step 1 and the next value in the sequence. This process keeps going until there are items in the sequence.

Syntax : - 

reduce(function, sequence[, initial]) -> value

NOTE: - if you want to use it, you have to first import it.

 strftime method in python : - 

strftime() function in python helps you to convert date, time, and datetime object to its equivalent string.

strftime() returns a string that represents the date and time using date, time, or datetime object. 

example : -

from datetime import datetime

now = datetime.now() # current date and time

year = now.strftime("%Y")
print("year:", year)

month = now.strftime("%m")
print("month:", month)

day = now.strftime("%d")
print("day:", day)

time = now.strftime("%H:%M:%S")
print("time:", time)

date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time) 

timeit() method in Python:-

timeit() function in python is used to measuring the execution time of your python code snippets.it is an in-built python library timeit and it is a simple method of the execution time of small bits of Python code.
# importing the required module 
import timeit 
  
# code snippet to be executed only once 
mysetup = "from math import sqrt"
  
# code snippet whose execution time is to be measured 
mycode = ''' 
def example(): 
    mylist = [] 
    for x in range(100): 
        mylist.append(sqrt(x)) 
'''
  
# timeit statement 
print timeit.timeit(setup = mysetup, 
                    stmt = mycode, 
                    number = 10000) 

Python - Map Function : - 

map() function in python is a build-in function and it called a function for a specified iterable and gives a list of results.

example:- 

def square(x):
    return x*x

Syntax : -
map(function, iterable [, iterable2, iterable3,...iterableN]) --> map object

Python main for function : -

main() function is an execution point for a program file some programming language has a function special function called main() which tells about the execution point. But Python runs each line serially from the top of the file and has no explicit main() function.
Python has another way to tell about the execution point that is __name__ property of in python file.

__name__variable shows the name of the current module. The value will depend on where we execute the Python file. 

example: -


import helloworld
def main():
    print("Hello World")

if __name__=="__main__":
    main()

mkdir() method in python : - 

If any user wants to interact with the operating system in python so python provides OS module. This module provides a portable way in python for operating system dependent functionality.

Function in os module raises OSError in the case of invalid or inaccessible file names and paths.

os.makedirs() function used to create the create a directory recursively or if any directory is missing then os.makedirs() function create them all.

# Python program to explain os.makedirs() method  
    
# importing os module  
import os 
  
# Leaf directory 
directory = "known"
  
# Parent Directories 
parent_dir = "/home/User/Documents/functioninprogramming/Authors"
  
# Path 
path = os.path.join(parent_dir, directory) 
  
# Create the directory 
# 'known' 
os.makedirs(path) 
print("Directory '%s' created" %directory) 
  
# Directory 'functioninprogramming' and 'Authors' will 
# be created too  
# if it does not exists 
  
  
  
# Leaf directory 
directory = "c"
  
# Parent Directories 
parent_dir = "/home/User/Documents/functioninprogramming/a/b"
  
# mode 
mode = 0o666
  
path = os.path.join(parent_dir, directory) 
  
# Create the directory 
# 'c' 
   
os.makedirs(path, mode) 
print("Directory '%s' created" %directory) 

Python raw_input method : -

raw_input function in python is used to read a string from standard input such as a keyboard and with the help of this method user can insert data into a program. 

Example: - 

print "What is your name?"
name = raw_input()
print "Hello %s!" % name 

factorial() function in Python : -

Python provides factorial() function that can be used to compute the factorial of a number without writing the whole code for factorials like in C or C++.

# Python code to demonstrate math.factorial() 
import math 
  
print ("The factorial of 17 is : ", end="") 
print (math.factorial(17)) 

ceil() function in Python : -

ceil method in python gives the ceiling value if x means the smallest integer not less than x.

# This will import math module
import math   
  
# prints the ceil using ceil() method
print "math.ceil(-23.11) : ", math.ceil(-23.11) 

  Split() function in python : - 

Well, all we know about joining two strings in Python by concatenation but split() function does the exact opposite of that.
split() function scans through a string and separates them whenever the separator called for it.

Syntax: -

str.split(separator, maxsplit)