Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

Python modules

 Before seeing some example about file Operators lets see what module is.....

     Simply module is a file consist of python code. A module may have function, classes and variable. A module can also include runable code. Fuctions are group of code and modules are group of function.

There are two types of modules:

1) user defined

2) built-in 

To create a module

1) create a file with function and variable then save it as .py extension

2) run the program and check it as no error

3) now create a new python file and import the previous file by using

Import filename

4) We can also access specific function from a module which can reduce the load speed. Use

From filename import function

def add(a,b):
   result=a+b
   print ("additional of two number is",result)
def sub(a,b):
   result=a+b
   print("subtraction of two number is",result)
def mul(a,b):
   result=a*b
   print("product of two number is", result)

Save the above program as module.py

Now create a new file as new.py and type

import module
 module.add(5,7)
 module.sub(6,3)
 module.mul(4,5)

Python modules


Using from:(here you can only use add function)

from module import add
module.add(7,3)

Python modules


Built-in :

Also like function there are many module in python which are used for specific purpose. Some main modules are

1) random

2) math

3) date and calendar

Random:

This module generates the random number and variable from list or sequence. Use randint() to print random integer number number. randint() consist of two parameters starting and ending value. We can also generate floating point numbers by using random() function. To generate elements in list we use choice() function. Lets see some example

import random
print(random.randint(0,5))
print(random.random())
list=["hello","hi","welcome","thankyou"]
print(random.choice(list))

Output will change whenever you run the program

Output:

Python modules


Math:

Using this module we can access for mathematical values like pi, factorials etc

import math
print(math.pi)
print(math.degrees(2))
print(math.radians(60))
Print(math.sin(3))
print(math.cos(2))
print(math.tan(0))
print(math.factorial(5))
print(math.sqrt(45))

Output:

3.141592653589793
114.59155902616465
1.0471975511965976
0.1411200080598672
-0.4161468365471424
0.0
120
6.708203932499369

Calendar and  date-time:

By using python calendar and date-time module we can track date from a year and time including current time

import calendar
cal = calendar.month(2021,5)
val = calendar.month(2021,6)
print(cal)
print(val)

Output:

Python modules


import time
local = time.localtime(time.time())
print(local)

Output:



Try the code with different variable and values to make it more clear and understandable .   feel free to comment!!!!!

Book for python:  https://amzn.to/3pCgIqy

ebook: https://amzn.to/3pHioiO

Comments

Popular Posts