Monday, June 22, 2015
Thursday, June 18, 2015
Wednesday, June 17, 2015
Remove punctation from the text - Python
import re
import string
def removePunctuation(text):
#converts to lowercase
text1 = text.lower()
#removes punctuation
#punctation = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
text2 = re.sub('[%s]' % string.punctuation, ' ' ,text1)
#removes leading and trailing spaces
text3 = text2.strip()
return text3
print removePunctuation("'The best investments today,'")
print removePunctuation("Misery in Paradise;")
Output:
the best investments today misery in paradise
Monday, June 15, 2015
Program to find two largest numbers using Python
nums = [10, 90, 80, 60, 50]
maxone = 0
maxtwo = 0
for x in nums:
if(maxone < x):
maxtwo = maxone
maxone = x
elif (maxtwo < x):
maxtwo = x
print "First two largest numbers"
print "First largest number:", maxone
print "First largest number:", maxtwo
output:
maxone = 0
maxtwo = 0
for x in nums:
if(maxone < x):
maxtwo = maxone
maxone = x
elif (maxtwo < x):
maxtwo = x
print "First two largest numbers"
print "First largest number:", maxone
print "First largest number:", maxtwo
output:
First two largest numbers First largest number: 90 First largest number: 80
Friday, June 12, 2015
Conversion of decimal to hexadecimal, binary and octal in Python
#num= int(input("Enter a decimal number:))
num = 15
print "Decimal value of given number:", num
print "Octal value of", num, ":",oct(num)
print "Binary value of", num, ":",bin(num)
print "Hexadecimal value of", num, ":",hex(num)
num = 15
print "Decimal value of given number:", num
print "Octal value of", num, ":",oct(num)
print "Binary value of", num, ":",bin(num)
print "Hexadecimal value of", num, ":",hex(num)
Output:
Decimal value of given number: 15 Octal value of 15 : 017 Binary value of 15 : 0b1111 Hexadecimal value of 15 : 0xf
Subscribe to:
Posts (Atom)