0% found this document useful (0 votes)
89 views10 pages

Practical File Programs

The document contains 11 code snippets that demonstrate various Python programming concepts. The snippets include: 1) Taking user input and displaying output, 2) Comparing numbers and finding the largest/smallest, 3) Calculating series sums, 4) Generating patterns using loops, 5) Checking number properties like perfect/Armstrong/palindrome, 6) Determining if a number is prime/composite, 7) Displaying the Fibonacci series, 8) Calculating GCD and LCM, 9) Counting characters in a string, and 10) Creating a menu-driven program to perform CRUD operations on a customer database.

Uploaded by

Parthiv Mistry
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
89 views10 pages

Practical File Programs

The document contains 11 code snippets that demonstrate various Python programming concepts. The snippets include: 1) Taking user input and displaying output, 2) Comparing numbers and finding the largest/smallest, 3) Calculating series sums, 4) Generating patterns using loops, 5) Checking number properties like perfect/Armstrong/palindrome, 6) Determining if a number is prime/composite, 7) Displaying the Fibonacci series, 8) Calculating GCD and LCM, 9) Counting characters in a string, and 10) Creating a menu-driven program to perform CRUD operations on a customer database.

Uploaded by

Parthiv Mistry
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

1. Write a python program to input a welcome message and display it.

wl_msg = input(“Enter the welcome message:”)


print(wl_msg)

2. Write a python program to input two numbers and display the larger / smaller number.

n1=input("Enter the first number to check:")


n2=input("Enter the second number to check:")

#Checking Larger if
n1>n2:
print(n1," is larger.") elif
n2>n1:
print(n2," is larger") else:
print("You have entered equal number.")

#Checking Smaller if
n1<n2:
print(n1," is smaller.") elif
n2<n1:
print(n2," is smaller") else:
print("You have entered equal number.")
3. Write a python program to input three numbers and display the largest / smallest number.

n1=input("Enter the first number to check:")


n2=input("Enter the second number to check:")
n3=input("Enter the third number to check:")

#Checking Largest if n1>n2


and n1>n3:
print(n1," is largest.") elif
n2>n1 and n2>n3:
print(n2," is largest") elif
n3>n1 and n3>n2:
print(n3," is largest") else:
print("You have entered equal number.")

#Checking Smallest if n1<n2


and n1<n3:
print(n1," is smallest.")
elif n2<n1 and n2<n3:
print(n2," is smallest")
elif n3<n1 and n3<n2:
print(n3," is smallest")
else:
print("You have entered equal number.")
4. Generate the following patterns using nested loop.
Pattern-1 Pattern-2 Pattern-3
* 12345 A
** 1234 AB
*** 123 ABC
**** 12 ABCD
***** 1 ABCDE

#Code For Pattern1

for i in range(1,6):

for j in range(1,i+1):

print("*",end=" ")

print()

#Code for Pattern2

for i in range(6,1,-1): for j in

range(1,i):

print(j,end=" ")
print()

#code for pattern 3

for i in range (65,70):

for j in range(65,i+1):

print(chr(j),end="")

print()
5. Write a program to input the value of x and n and print the sum of the following series:
a. 1 + x² + x³ + ... + xⁿ
b. x - x2/2! + x3/3! - x4/4! + x5/5! - x6/6!

x = float (input ("Enter value of x :"))


n = int(input ("Enter value of n :")) s
= 0
#Series 1
for i in range (n+1):
s += x**i
if i<n:
print(x**i,"+",end=" ")
else:
print(x**i,end=" ")
print ("=", s)

#Series 2
sum = 0
m = 1
for i in range(1, 7) :
f = 1
for j in range(1, i+1) :
f *= j
t = x ** i / f
if i==1:
print(int(t),end=" ")
elif i%2==0:
print("-",int(t),end=" ")
else:
print("+",int(t),end=" ")
sum += t * m
m = m * -1
print(" =", sum)
Series 1:

Series 2:
6. Write a program to determine whether a number is a perfect number, an Armstrong number or a
palindrome.
n=int(input("Enter a number to check:"))
temp = n
cnt=0
rev=0

#Check for a perfect number


for i in range(1,n):
if n%i==0:
cnt+=i
if cnt==n:
print(n," is perfect number")
else:
print(n," is not a perfect number")

#Check for an armstrong number

while temp > 0:


digit = temp % 10
cnt += digit ** 3
temp //= 10 # Armstrong number is a number that is
equal to the sum of cubes of its digits
if n == cnt:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")

#Check for palindrome

while n > 0: #if n is greater than 0 this loop runs


dig = n % 10
rev = rev * 10 + dig #This loop calculates the
reverse and matches it with input
n = n // 10
if temp == rev:
print(temp, "The number is a palindrome!")
else:
print(temp, "The number isn’t a palindrome!")
Output:
7. Write a program to input a number and check if the number is a prime or composite number.
n=int(input("Enter number to check:"))
if n>1:
for i in range(2,n):
if n%i==0:
f=1
break
else:
f=0
elif n==0 or n==1:
print(n, " is not a prime number nor composite
number")
else:
print(n," is a composite number")

if f==1:
print(n, " is not a prime number")
else:
print(n," is a prime number")
8. Write a program to display the n term Fibonacci series.
nterms = int(input("Enter number of terms to display:
"))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1, end=” ”)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 +
n2
# update values
n1 = n2
n2 = nth
count += 1

9. Write a python program to compute the greatest common divisor and least common multiple of two
integers.
# Reading two numbers from user
fn = int(input('Enter first number: '))
sn = int(input('Enter second number: '))
gcd = 1
for i in range(1,fn +1):
if fn%i==0 and sn%i==0:
gcd = i
# Displaying GCD
print('HCF or GCD of %d and %d is %d' %(fn, sn,gcd))
# Calculating LCM
lcm = fn * sn/ gcd
print('LCM of %d and %d is %d' %(fn, sn, lcm))
10. Write a program to count and display the number of vowels, consonants, uppercase, lowercase
characters in string.
txt = input("Enter Your String : ")

v = c = uc = lc = 0

v_list = ['a','e','i','o','u']

for i in txt:

if i in v_list:

v += 1

if i not in v_list:

c += 1

if i.isupper():

uc += 1

if i.islower():

lc += 1

print("Number of Vowels in this text = ", v)

print("Number of Consonants in this text = ", c)

print("Number of Uppercase letters in this text=", uc)

print("Number of Lowercase letters in this text=", lc)


11. Write a menu-driven program to perform following operations:
1. Show record
2. Add new customer
3. Delete a customer
4. Search record
5. Update record
6. Sort record
7. Exit

n=int(input("How many customers:"))


cust={}
for i in range(n):
print("Enter details of customer:",i+1)
cname=input("Enter name of customer:")
pn=int(input("Enter Phone number:"))
cust[cname]=pn
c=0
while c!=7:
print('''
1. Show record
2. Add new customer
3. Delete a customer
4. Search record
5. Update record
6. Sort record
7. Exit
''')
c=int(input("Enter your choice:"))
if c==1:
for i in cust:
print(i,":",cust[i])
elif c==2:
print("Enter details of new customer:")
cname=input("Enter name:")
pn=int(input("Enter phone number:"))
cust[cname]=pn
elif c==3:
nm=input("Enter name to be deleted:")
f=cust.pop(nm,-1)
if f!=-1:
print(f, " is deleted successfully.")
else:
print(f, " not found.")
elif c==4:
name=input("Enter Customer Name:")
if name in cust:
print(name, " found in the record.")
else:
print(name, " not found in the record.")
elif c==5:
cname=input("Enter Customer Name:")
pn=int(input("Enter phone number to modify:"))
cust[cname]=pn
elif c==6:
l=sorted(cust)
for i in l:
print(i,":",cust[i])
elif c==7:
break

You might also like