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

Plc Additional Programs

Uploaded by

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

Plc Additional Programs

Uploaded by

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

#Write a python program to find the factorial of a given number.

n=int(input("enter a number: "))


factorial=1
if n>=1:
for i in range(1,1+n):
factorial=factorial*i
print("factorial of the given number is",factorial)

#Write a python program to find the sum of first n natural numbers


n=int(input("enter a number: "))
sum=0
for i in range(n+1):
sum+=i
print("sum of the first n natural numbers is",sum)

#Check if the given number is palindrome or not


n=(input("enter a number: "))
if n==n[::-1]:
print("yes, it is a palindrome")
else:
print("no, it is not a palindrome")

#Find the sum of digits of a given number


n=(input("enter a number: "))
sum=0
for i in n:
sum=sum+int(i)
print("sum of the digits= ",sum)

#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

print("Largest number is", largest)

#Write a Python Program to input a string to check whether it is a Palindrome


string or not.
str=input("Enter a string: ")
if str==str[::-1]:
print("Yes, it is a palindrome string.")
else:
print("No, it is not a palindrome string.")

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

#Write a program to remove duplicates from a list.


l=eval(input("Enter the list: "))
a=[]
for i in l:
if i not in a:
a.append(i)
print("The list after removing duplicates is", a)

#Consider the given scenario: In a bank, a cashier is serving a queue of customers


with token number starting from 101. There are 20 customers standing in the queue.
The cashier is a slow server and takes 5 mins to serve each customer. To avoid
waiting time, the manager appoints another cashier who serves only even token
numbers. Now the first cashier serves odd token numbers and second, the even ones.
Write a program to create 2 queues of customers and cashiers serve them in the
increasing order of token numbers.
for i in range(101,121):
if i%2!=0:
print("Cashier 1: ",i)
else:
print("Cashier 2: ",i)'''
#Write a program to print each line of a String in reverse order
strn=input ("Enter the string: ")
revstrn=""
for i in strn:
revstrn=i+revstrn
print ("The reversed form of", strn, "is" , revstrn)

# DICTIONARY

#Write a program to find the sum and average of dictionary values.


mydict={'a':1,'b':2,'c':3,'d':4,'e':5}
sum1=sum(mydict.values ())
avg=sum1/len(mydict)
print('sum =', sum1)
print('Average =', avg)

#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.

n=int(input("Enter the number of students:"))

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)

#Merge the contents of 2 dictionaries.


dict1={'a':1,'b':2,'c':3,'d':4}
dict2={'e':5,'f':6,'g' :7}
dict1.update(dict2)
print(dict1)

#Sort the elements in the dictionary based on the keys.


mydict={'rajesh':100,'khan':92,'nas':93,'tyla':94}
key=list(mydict. keys ())
key.sort()
sorted_dict={i:mydict[i] for i in key}
print(sorted_dict)

#Add a key value pair at the beginning of the given dictionary.


mydict={'key1':'my','key2':'name','key3':'is'}
print("The initial dictionary is:\n",mydict)
mydict['key4']='my'
mydict['key5']='name'
mydict['key6']='is'

print("After adding the key pair:\n", mydict)

#Dictionary- 3. Given a list of names, create a dictionary with key as a starting


letter and value as all names starting with that letter.
l1=['orlo','rowo','lolo']
for I in l1:
l2.append(i[0])
mydict=dict(zip(l2,l1))
print(mydict)

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

#Write a Python program to find the length of a tuple.


a=tuple("hello world")
print(a)
print(len(a))

#Write a Python program to convert a tuple to a string.


tup=('p','y','t','h','o','n')
str=''.join(tup)
print(str)

#Write a Python program to convert a list to a tuple


l=[5,10,7,4,15,3]
print(l)
t=tuple(l)
print(t)

# FILES

# Python program to count the uppercase character in a file


def count_letter():
file = open("C:/Users/rowee/OneDrive/Desktop/files.txt","r")
data = file.read()
count = 0
for letter in data:
if letter.isupper():
count+=1
print(count)
file.close()

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)

# Python program to read first N characters from each line


n=int(input("enter the number of characters you want to read: "))
def read_first_n_characters(filename, n):
try:
with open(filename, 'r') as f:
for line in f:
first_n_chars = line[:n]
print(first_n_chars)
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
filename = r'C:\Users\rowee\OneDrive\Desktop\files.txt'
read_first_n_characters(filename, n)

# Python program to delete a file


import os

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)

# Python program to count total number of uppercase and lowercase characters in


file
def count_upper_lower(filename):
with open(filename, 'r') as file:
upper_count = 0
lower_count = 0
text = file.read()
for char in text:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1

print("Number of uppercase characters:", upper_count)


print("Number of lowercase characters:", lower_count)

filename = r'C:\Users\rowee\OneDrive\Desktop\files.txt'
count_upper_lower(filename)'''

You might also like