0% found this document useful (0 votes)
48 views

Computer Programmers

The program opens a CSV file, reads the data using a CSV reader object, and prints each row. It then allows the user to choose to either read the existing data or append a new row by writing to the file using a CSV writer object. If the user enters an invalid choice, it prints an error message.

Uploaded by

Prem Bathre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Computer Programmers

The program opens a CSV file, reads the data using a CSV reader object, and prints each row. It then allows the user to choose to either read the existing data or append a new row by writing to the file using a CSV writer object. If the user enters an invalid choice, it prints an error message.

Uploaded by

Prem Bathre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Program: -

num=int(input("Enter the number: "))


for i in range(2,num):
if num%i==0:
print(num, "is not prime number")
break;
else:
print(num,"is prime number")

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")

print("Do you want to continue? Y/N")


option=input()
if option=='y' or option=='Y':
var=True
else:
var=False
Output: -

Result: -
Program: -

ch=input("Enter a character: ")


ifch.isalpha():
print(ch, "is an alphabet")
elifch.isdigit():
print(ch, "is a digit")
elifch.isalnum():
print(ch, "is alphabet and numeric")
else:
print(ch, ”is alphabet and numeric”)

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: -

num=int(input("Enter a number : "))


n=num
res=0
while num>0:
rem=num%10
res=rem+res*10
num=num//10
if res==n:
print("Number is Palindrome")
else:
print("Number is not Palindrome")

Output: -

Result: -
Program: -

defisStringPalindrome(str):
iflen(str)<=1:
return True
else:
ifstr[0]==str[-1]:
returnisStringPalindrome(str[1:-1])
else:
return False
#__main__

s=input("Enter the string : ")


y=isStringPalindrome(s)

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)

num=int(input("enter the number: "))


ifnum< 0:
print("Sorry, factorial does not exist for negative numbers")
elifnum == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of ",num," is ", factorial(num))

Output: -

Result:-
Program: -

deffibonacci(n):
if n<=1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))

num=int(input("How many terms you want to display: "))


for i in range(num):
print(fibonacci(i)," ", end=" ")

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()

#Prepared by Latest Tech Updates--

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:

writer = csv.writer(file, delimiter=',', quotechar='"')

#write new record in file


writer.writerow(['4', 'Devansh', 'Arts', '404'])

print("Press-1 to Read Data and Press-2 to Write data: ")


a=int(input())
if a==1:
readcsv()
elif a==2:
writecsv()
else:
print("Invalid value")
Output: -

Result: -

You might also like