Computer Programmers
Computer Programmers
Output: -
Result: -
Program: -
var=True
whilevar:
choice=int(input("Press-1 to find the ordinal value of a character \nPress-2 to
find a character of a value\n"))
if choice==1:
ch=input("Enter a character : ")
print(ord(ch))
elif choice==2:
val=int(input("Enter an integer value: "))
print(chr(val))
else:
print("You entered wrong choice")
Result: -
Program: -
Output: -
Result: -
Program: -
import random
n=random.randint(1,6)
guess=int(input("Enter a number between 1 to 6 :"))
if n==guess:
print("Congratulations, You won the lottery ")
else:
print("Sorry, Try again, The lucky number was : ", n)
Output: -
Result: -
Program: -
Output: -
Result: -
Program: -
defisStringPalindrome(str):
iflen(str)<=1:
return True
else:
ifstr[0]==str[-1]:
returnisStringPalindrome(str[1:-1])
else:
return False
#__main__
if y==True:
print("String is Palindrome")
else:
sprint("String is Not Palindrome")
Output: -
Result: -
Program: -
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
Output: -
Result:-
Program: -
deffibonacci(n):
if n<=1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))
Output: -
Result: -
Program: -
filein = open("mydoc.txt",'r')
line =" "
while line:
line = filein.readline()
#print(line)
for w in line:
if w == ' ':
print('#',end = '')
else:
print(w,end = '')
filein.close()
filein = open("Mydoc.txt",'r')
for line in filein:
word= line .split()
for w in word:
print(w + '#',end ='')
print()
filein.close()
Output: -
Result: -
Program: -
#Practical-prog Read a text file and display
#the number of vowels/consonants/uppercase/
#lowercase characters in the file.
def cnt():
f=open("D:\\test2.txt","r")
cont=f.read()
print(cnt)
v=0
cons=0
l_c_l=0
u_c_l=0
for ch in cont:
if (ch.islower()):
l_c_l+=1
elif(ch.isupper()):
u_c_l+=1
ch=ch.lower()
if( ch in ['a','e','i','o','u']):
v+=1
elif (ch in ['b','c','d','f','g',
'h','j','k','l','m',
'n','p','q','r','s',
't','v','w','x','y','z']):
cons+=1
f.close()
print("Vowels are : ",v)
print("consonants are : ",cons)
print("Lower case letters are : ",l_c_l)
print("Upper case letters are : ",u_c_l)
#main program
cnt()
Input: -
Output: -
Result: -
Program: -
import csv
def readcsv():
with open('C:\\Users\\Ram\\Downloads\\data.csv','rt')as f:
data = csv.reader(f) #reader function to generate a reader object
for row in data:
print(row)
def writecsv( ):
with open('C:\\Users\\Ram\\Downloads\\data.csv', mode='a', newline='')as file:
Result: -