Plc Additional Programs
Plc Additional Programs
#Write a Python program to get the largest number from a list of numbers entered by
user.
a=[]
n=int(input("Enter the number of elements: "))
for i in range(n):
b=int(input("Enter the number: "))
a.append(b)
largest=0
for i in a:
if i>largest:
largest=i
#Write a Python program to iterate through a string and check whether a particular
character is present or not. If present, print true otherwise print false.
str=input("Enter a string: ")
ch=input("Enter the character to be checked: ")
if ch in str:
print(ch,"is there in the string")
else:
print(ch,"is not in the string")
#Program to replace lower-case characters with upper-case and vice versa without
using built-in function.
str=input("Enter a string: ")
newstr=""
for i in range(0,len(str)):
if str[i].islower():
newstr+=str[i].upper()
elif str[i].isupper():
newstr+=str[i].lower()
else:
newstr+=str[i]
print("String after case conversion :" +newstr)
#Write a python program to find the union of 2 lists. Take the list as user input.
l1=eval(input("Enter the first list: "))
l2=eval(input("Enter the second list: "))
union=l1
for i in l2:
if i not in union:
union.append(i)
print("Union list: ", l1)
#Write a program to check if the given key is present in the list, if yes, print
its position.
l1=eval(input("Enter the list: "))
print(l1)
key=int(input("Enter the key: "))
if key in l1:
print(key, "is present in the list at", l1.index(key),"position.")
else:
print(key, "is not present in the list.")
# DICTIONARY
#In a class of 10 students, it is required to store the marks scored along with
their USNs. Use an appropriate data structure to store the marks. Also, retrieve
the students who have scored marks above average.
l1=[]
l2=[]
for i in range(n):
val=input("Enter the USN: ")
l1.append(val)
for i in range(n):
print("For",l1[i],",")
val=int(input("Enter the marks =\n"))
l2.append(val)
mydict=dict(zip(l1,l2))
print(mydict)
avg=(sum(mydict.values()))/len(mydict)
print("The marks greater than average are: ")
for i in mydict.values():
if i>avg:
print(i)
# TUPLES
#Write a Python program to get the 4th element and 4th element from last of a tuple
a=('h','e','l','l','o','w','o','r','l','d')
print(a)
ele1=a[3]
print(ele1)
ele2=a[-4]
print(ele2)
# FILES
count_letter()
# Python program to copy odd lines of one file to another file
def copy_odd_lines(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line_number, line in enumerate(infile, 1):
if line_number % 2 != 0:
outfile.write(line)
input_file_name = r'C:\Users\rowee\OneDrive\Desktop\files.txt'
output_file_name = r'C:\Users\rowee\OneDrive\Desktop\opfiles.txt'
copy_odd_lines(input_file_name, output_file_name)
def delete_file(filename):
os.remove(filename)
print(f"File '{filename}' deleted successfully.")
filename = r'C:\Users\rowee\OneDrive\Desktop\files.txt'
delete_file(filename)
filename = r'C:\Users\rowee\OneDrive\Desktop\files.txt'
count_upper_lower(filename)'''