SingleCharacter = input("Enter a character:",)
Character = ord(SingleCharacter)
# ord --> Convert a string to ASCII value
if Character > 0 and Character <= 31:
print("ASCII value", Character," is a control character")
elif Character >= 48 and Character <= 57:
print("ASCII value", Character," is a character is a digit")
elif Character >= 65 and Character <= 90:
print("ASCII value", Character," is a uppercase letter")
elif Character >= 97 and Character <= 122:
print("ASCII value", Character," is a lowercase letter")
else:
print("ASCII value", Character," is a Symbol")
Solve Problems by Coding Solutions - A Complete solution for python programming
To find the distance between two points, given by the coordinates (x1, y1) and (x2, y2) by Pythagorean theorem using functions
def pt(x1,y1,x2,y2):
x=x2-x1
y=y2-y1
x=x**2
y=y**2
d=(x+y)**(0.5)
return d
x1=int(raw_input('Enter the coordinate x1: '))
x2=int(raw_input('Enter the coordinate x2: '))
y1=int(raw_input('Enter the coordinate y1: '))
y2=int(raw_input('Enter the coordinate y2: '))
k=pt(x1,y1,x2,y2)
print 'Distance between two points is : ', k
x=x2-x1
y=y2-y1
x=x**2
y=y**2
d=(x+y)**(0.5)
return d
x1=int(raw_input('Enter the coordinate x1: '))
x2=int(raw_input('Enter the coordinate x2: '))
y1=int(raw_input('Enter the coordinate y1: '))
y2=int(raw_input('Enter the coordinate y2: '))
k=pt(x1,y1,x2,y2)
print 'Distance between two points is : ', k
Database version retrieving using Python
#username of MySQL: root
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
# prepare a cursor object using cursor() method
cursor = x.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
x.close()
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
# prepare a cursor object using cursor() method
cursor = x.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
x.close()
Database connection test to MySQL
#username of MySQL: root
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
x.close()
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
x.close()
Reversal of a string
startmsg = "hello"
endmsg = ""
for i in range(0,len(startmsg)):
endmsg = startmsg[i] + endmsg
print (endmsg)
endmsg = ""
for i in range(0,len(startmsg)):
endmsg = startmsg[i] + endmsg
print (endmsg)
String replacing with another string with occurrence count
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('red','brown')
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',1)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',2)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',3)
print (s1)
print (s)
s1=s.replace('red','brown')
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',1)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',2)
print (s1)
s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',3)
print (s1)
Searching the pattern in a String using find and index
try:
s="brown box red pencil brown pen red pen red box"
print s.find("red")
print s.find("red",13,len(s))
print s.find("red",34,len(s))
print s.find("crow")
print s.index("red")
print s.index("red",13,len(s))
print s.index("red",34,len(s))
print s.index("crow")
except ValueError:
print "crow not a pattern in string"
s="brown box red pencil brown pen red pen red box"
print s.find("red")
print s.find("red",13,len(s))
print s.find("red",34,len(s))
print s.find("crow")
print s.index("red")
print s.index("red",13,len(s))
print s.index("red",34,len(s))
print s.index("crow")
except ValueError:
print "crow not a pattern in string"
String operations to remove white spaces
s=" t ioi ii "
t1=s.rstrip() # removes trailing white space
t2=s.lstrip() # removes leading white space
t3=s.strip() # removes leading and trailing white space
print s
print t1
print t2
print t3
t1=s.rstrip() # removes trailing white space
t2=s.lstrip() # removes leading white space
t3=s.strip() # removes leading and trailing white space
print s
print t1
print t2
print t3
File operation for copying content of one file to another without for loop
# Implementation without for loop
# Copying content of 1.txt file to 2.txt after creation of file
# Please provide the 1.txt file in the current folder as input and
# check after runing the program in the same folder for 2.txt
I=open('1.txt',"r")
O=open('2.txt',"w")
C=I.readlines()
O.writelines(C)
I.close()
O.close()
# Copying content of 1.txt file to 2.txt after creation of file
# Please provide the 1.txt file in the current folder as input and
# check after runing the program in the same folder for 2.txt
I=open('1.txt',"r")
O=open('2.txt',"w")
C=I.readlines()
O.writelines(C)
I.close()
O.close()
File operation for copying content of one file to another
# Copying content of 1.txt file to 2.txt after creation of file
# Please provide the 1.txt file in the current folder as input and
# check after runing the program in the same folder for 2.txt
I=open('1.txt',"r")
O=open('2.txt',"w")
for l in I.readlines():
O.write(l)
I.close()
O.close()
# Please provide the 1.txt file in the current folder as input and
# check after runing the program in the same folder for 2.txt
I=open('1.txt',"r")
O=open('2.txt',"w")
for l in I.readlines():
O.write(l)
I.close()
O.close()
Subscribe to:
Posts (Atom)