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

Monday, February 13, 2017

Date Conversion from One Format To Another Python

Following Code will convert date entered in one format to another:

Code::

def converter(date):
    day,month,year=date.split('/')
    monthnames=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    return monthnames[int(month)-1]+" "+day+","+year

date=input("Enter Date in DD/MM/YYYY format : ")
print ("Entered Date: "+converter(date))

Output::


Palindrome Testing Recursively Python

Entered string is Palindrome or not can be tested using Recursion as Shown by Following Code::

Code::

def isletter(c):     #function to check that character c is a letter or not
    return ('a'<=c<='z')or ('A'<=c<='Z')

def paltest(line):
    if len(line)<=1:return True
    elif not isletter(line[0]):return paltest(line[1:])      #ignoring first not letter
    elif not isletter(line[-1]):return paltest(line[:-1])    #ignoring last not letter
    elif line[0].lower()!=line[-1].lower():return False      
    else:return paltest(line[1:len(line)-1])                 #palindrome testing recursive call


string=input("Enter any String: ")
if(paltest(string)) : print("Yes,Palindrome")
else: print("Not,Palindrome")

Output::

Saturday, February 11, 2017

Case Conversion Python

Following Code Will Convert The Case of Entered String 
i.e lower to UPPER and UPPER to lower



Code::

def conversion(l):
    li=list(l)
    for i in range(0,len(l)):
        if 'a'<=li[i]<='z': li[i]=chr(ord(li[i])-32)
        elif 'A'<=li[i]<='Z': li[i]=chr(ord(li[i])+32)
    stri=''
    return stri.join(li)

s=input("Enter String: ")
print(conversion(s))


Output::


Standard Deviation Calculator Python

The Standard Deviation is a measure of how spread out numbers are.

Standard Deviation is a quantity expressing by how much the members of a group differ from the mean value for the group.
Its symbol is σ (the greek letter sigma)
Following Code will calculate the Standard Deviation:

Code::
import math
def average(l):             #function to calculate average(mean)
    total=0.0
    for i in l:
        total=total+int(i)
    if(len(l)==0): return 0
    else: return(total/len(l))
    
def standardeviation(l,ave):       #function to calculate standard deviation
    diff=0.0
    for i in l:
        x=int(i)-ave
        diff=diff+x*x
    if(len(l)==0): return 0
    else :return(math.sqrt(diff/len(l)))    


lst=[]
num=input("Enter Values ,enter -1 to end:")
while num!='-1':
    lst.append(num)
    num=input()
ave=average(lst)
print("Average is:",ave)
sd=standardeviation(lst, ave)
print("Standard Deviation :",sd)
        
Output::

Wednesday, February 1, 2017

Lower Function User Defined Python

Following code will define a lower function to convert all letters of a string to lower case:

Code::

def lower(a):
    x=""
    for i in a:
        if(i>='A' and i<'a'):
            x=x+((chr((ord(i)+32))))     #ord to convert character to ascii value
        else:
            x=x+i
    return x

i=input("Enter any String :")
print(lower(i))

Output::


User Defined String Upper Function Python

Following code defines a function upper which changes the entered string to Upper Case:

Code::

def upper(a):
    x=""                                                 #for returning Upper Case
    for i in a:
        if(i>='a'):                                     #for Not Changing Already upper Case Letters
            x=x+((chr((ord(i)-32))))
        else:
            x=x+i
    return x

i=input("Enter any String :")
print(upper(i))

Output::


Number To String Python

Following Code will convert a number entered by user to the Same Number in Words

Code::

def numtostring(n):
    if (n==0):return ""
    elif(n==1):return "one"
    elif(n==2):return "two"
    elif(n==3):return "three"
    elif(n==4):return "four"
    elif(n==5):return "five"
    elif(n==6):return "six"
    elif(n==7):return "seven"
    elif(n==8):return "eight"
    elif(n==9):return "nine"
    elif(n==10):return "ten"
    elif(n==11):return "eleven"
    elif(n==12):return "twelve"
    elif(n==13):return "thirteen"
    elif(n==14):return "fourteen"
    elif(n==15):return "fifteen"
    elif(n==16):return "sixteen"
    elif(n==17):return "seventeen"
    elif(n==18):return "eighteen"
    elif(n==19):return "nineteen"
    elif(n<=29):return "twenty "+numtostring(n%10)
    elif(n<=39):return "thirty "+numtostring(n%10)
    elif(n<=49):return "forty "+numtostring(n%10)
    elif(n<=59):return "fifty "+numtostring(n%10)
    elif(n<=69):return "sixty "+numtostring(n%10)
    elif(n<=79):return "seventy "+numtostring(n%10)
    elif(n<=89):return "eighty "+numtostring(n%10)
    elif(n<=99):return "ninety "+numtostring(n%10)

def convert(n):
    if(n<=99):
        return numtostring(n)
    elif(n<=999):
        return numtostring(n//100)+' hundred '+numtostring(n%100)
    elif(n<=9999):
        return numtostring(n//1000)+' thousand '+numtostring((n//100)%10)+' hundred '+numtostring((n%100))


i=int(input("Enter Any Number Upto 9999: "))
print(convert(i))
     
Output: