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

1 Cs

The document contains 11 Python programs. Program 1 takes a character as input and prints if it is an alphabet, digit or symbol. Program 2 contains a menu driven program to calculate factorial or check primality of a number. Program 3 counts lowercase and uppercase letters in a string. The remaining programs perform tasks like removing duplicates, sorting lists, swapping tuples, searching dictionaries, combining dictionaries, finding most common word, and handling exceptions in division.

Uploaded by

Shubh Chandegara
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)
106 views

1 Cs

The document contains 11 Python programs. Program 1 takes a character as input and prints if it is an alphabet, digit or symbol. Program 2 contains a menu driven program to calculate factorial or check primality of a number. Program 3 counts lowercase and uppercase letters in a string. The remaining programs perform tasks like removing duplicates, sorting lists, swapping tuples, searching dictionaries, combining dictionaries, finding most common word, and handling exceptions in division.

Uploaded by

Shubh Chandegara
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/ 12

Write a program to input a character and to print whether a

Program–1 :
given character is an alphabet, digit or any other character.

#Program to input a character and to print whether a given character is an


#alphabet, digit or any other character.

ch=input("Enter a character: ")


if ch.isalpha():
print(ch, "is an alphabet")
elif ch.isdigit():
print(ch, "is a digit")
elif ch.isalnum():
print(ch, "is alphabet and numeric")
else:
print(ch, "is a special symbol")

OUTPUT:

Enter a character: 7
7 is a digit
Enter a character: P
P is an alphabet
Write a menu driven program with options to calculate either
Program–2 : factorial of a number or check if the number is prime or
composite.

import math
while True:
print("1. To find factorial")
print("2. To check if prime or not")
print("3. To exit")
ch=int(input("Enter your choice:"))
if ch==1:
num = int(input("Enter any number :"))
fact = 1
n = num # storing num in n for printing
while num>1: # loop to iterate from n to 2
fact = fact * num
num-=1
print("Factorial of ", n , " is :",fact)
elif ch==2:
num = int(input("Enter any number :"))
for i in range(2,int(math.sqrt(num))+1):
if num % i == 0:
print("## Number is not Prime ##")
break
else:
print("## Number is Prime ##")
elif ch==3:
break

OUTPUT:

1. To find factorial
2. To check if prime or not
3. To exit
Enter your choice:1
Enter any number :5
Factorial of 5 is : 120
1. To find factorial
2. To check if prime or not
3. To exit
Enter your choice:2
Enter any number :64
## Number is not Prime ##
1. To find factorial
2. To check if prime or not
3. To exit
Enter your choice:2
Enter any number :23
## Number is Prime ##
1. To find factorial
2. To check if prime or not
3. To exit
Enter your choice:3
Write a program to count lowercase and uppercase letters in an
Program–3 :
inputted string.

#Program to count lowercase and uppercase letters in an inputted string

str1 = input("Enter the string :")


print(str1)
uprcase=0
lwrcase=0
i=0
while i < len (str1):
if str1[i].islower() == True:
lwrcase += 1
if str1[i].isupper() == True:
uprcase += 1
i += 1
print("No. of uppercase letters in the string = ",uprcase)
print("No. of lowercase letters in the string = ",lwrcase)

OUTPUT:

Enter the string :This is a String


This is a String
No. of uppercase letters in the string = 2
No. of lowercase letters in the string = 11
Write a program to remove duplicate characters of a given
Program–4 :
string.

#Program to remove duplicate characters of given string.


s=input("Enter string:")
news=""
for letter in s:
if letter not in news:
news=news+letter

print(news)

OUTPUT:

Enter string:PythonProgramming
Pythonrgami
Write a program to input a list and sort list elements in
Program–5 :
descending order.

# SORTING OF LIST IN DESCENDING ORDER


n= int(input("Enter How many Numbers? "))
mylist = []
i=0
while i<n:
num= int(input("Enter The Element of the List: "))
mylist.append(num)
i=i+1
print("Original List is")
print(mylist)
mylist.sort(reverse=True) # Sorting of List
print("List after sorting in Descending order")
print(mylist)

OUTPUT:

Enter How many Numbers? 5


Enter The Element of the List: 3
Enter The Element of the List: 4
Enter The Element of the List: 25
Enter The Element of the List :12
Enter The Element of the List: 89
Original List is
[3, 4, 25, 12, 89]
List after sorting in Descending order
[89, 25, 12, 4, 3]
Write a program to input two tuples and swap/interchange
Program–6 :
them.

#Program to input two tuples and swap/interchange them.

t1 = tuple()
n = int(input("Total no of values in first tuple: "))
for i in range(n):
a = input("Enter elements :")
t1 = t1+(a,)
t2 = tuple()
m = int(input("Total no of values in second tuple: "))
for i in range(m):
a = input("Enter elements :")
t2 = t2+(a,)
print("First tuple :")
print(t1)
print("Second tuple :")
print(t2)
t1,t2=t2,t1
print("After swapping :")
print("First tuple: ")
print(t1)
print("Second tuple: ")
print(t2)

OUTPUT:

Total no of values in first tuple: 4


Enter elements :1
Enter elements :5
Enter elements :3
Enter elements :7
Total no of values in second tuple: 3
Enter elements :5
Enter elements :9
Enter elements :8
First tuple :
('1', '5', '3', '7')
Second tuple :
('5', '9', '8')
After swapping :
First tuple:
('5', '9', '8')
Second tuple:
('1', '5', '3', '7')
Write a program to create a dictionary with product names as
Program–7 : keys, and price as values and search an entered product name
in dictionary and display its corresponding price.

#Program to create a dictionary and search a key in dictionary.

d={}
while True:
key=input("Enter Product Name:")
value=int(input("Enter Price:"))

d[key]=value
choice=input("Enter More.... (y/n)")
if choice=='n':
break
print(d)
while True:
p=input("Enter Product Name:")
if p in d:
print("Its price is",d[p])
else:
print("product not found")
choice=input("Search More.... (y/n)")
if choice=='n':
break

OUTPUT:

Enter Product Name:Book


Enter Price:200
Enter More.... (y/n)y
Enter Product Name:Watch
Enter Price:600
Enter More.... (y/n)y
Enter Product Name:Basket
Enter Price:150
Enter More.... (y/n)y
Enter Product Name:Mobile
Enter Price:6500
Enter More.... (y/n)n
{'Book': 200, 'Watch': 600, 'Basket': 150, 'Mobile': 6500}
Enter Product Name:Watch
Its price is 600
Search More.... (y/n)y
Enter Product Name:Bag
product not found
Search More.... (y/n)n
Write a program to count the frequency of an element in a given
Program–8 :
list.

#Program to count the frequency of each element in a list

lst=eval(input("Enter list elements:"))


d=dict()
for i in lst:
if i in d:
d[i] += 1
else:
d[i]=1
print("Element",'Frequency')
for key,value in d.items():
print("%10s"%key,"%9s"%value)

OUTPUT:

Enter list elements:[10,40,20,10,30,40,10,20,50]


Element Frequency
10 3
40 2
20 2
30 1
50 1
Write a program to combine two dictionaries adding values for
Program–9 :
common keys.

#Program to combine two dictionaries adding values for common keys.


d1={'A':1,'B':2,'C':3}
d2={'D':4,'B':4}
d3=dict()
for k in d1:
d3[k]=d1[k]

for k in d2:
if k not in d3:
d3[k]=d2[k]
else:
d3[k]=d1[k]+d2[k]
print("Original Dictionaries:")
print(d1)
print(d2)
print("Merged Dictionary:")
print(d3)

OUTPUT:

Original Dictionaries:
{'A': 1, 'B': 2, 'C': 3}
{'D': 4, 'B': 4}
Merged Dictionary:
{'A': 1, 'B': 6, 'C': 3, 'D': 4}
Write a program to take 10 sample phishing email, and find the
Program–10 :
most common word occurring.

#Program to take 10 sample phishing mail


#and count the most commonly occuring word
phishingemail=[
"jackpotwin@lottery.com",
"claimtheprize@mymoney.com",
"youarethewinner@lottery.com",
"luckywinner@mymoney.com",
"spinthewheel@flipkart.com",
"dealwinner@snapdeal.com"
"luckywinner@snapdeal.com"
"luckyjackpot@americanlottery.com"
"claimtheprize@lootolottery.com"
"youarelucky@mymoney.com"
]
myd={}
for e in phishingemail:
x=e.split('@')
for w in x:
if w not in myd:
myd[w]=1
else:
myd[w]+=1
key_max = max(myd,key=myd.get)
print("Most Common Occuring word is :",key_max)

OUTPUT:

Most Common Occuring word is : mymoney.com


Write a program to perform division of two numbers using
Program–11 :
python exception handling.

#Perform division of two numbers using python exception handling

try:
num1=eval(input("Enter dividend = "))
num2=eval(input("Enter divisor = "))
num3 = num1//num2
print("Division performed successfully")
except ZeroDivisionError:
print("Division by zero not allowed")
except NameError:
print("Variable not present")
except:
print("An error has occured")
else:
print ("The quotient is =",num3)
finally:
print("Done!!!!")

OUTPUT:

Enter dividend =
An error has occured
Done!!!!
Enter dividend = 60
Enter divisor = 5
Division performed successfully
The quotient is = 12
Done!!!!
Enter dividend = 60
Enter divisor = 0
Division by zero not allowed
Done!!!!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy