Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Python Practical Programs

Uploaded by

aayush270105
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Python Practical Programs

Uploaded by

aayush270105
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Practical Programs

Program 1:
Input three numbers and display the largest and smallest number
Code:
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
c=int(input("Enter a number: "))
if a>b and a>c:
print(a,"is the greatest number")
elif b>a and b>c:
print(b,"is the greatest number")
else:
print(c,"is the greatest number")
if a<b and a<c:
print(a,"is the smallest number")
elif b<a and b<c:
print(b,"is the smallest number")
else:
print(c,"is the smallest number")
Output:
Enter a number: 4
Enter a number: 3
Enter a number: 2
4 is the greatest number
2 is the smallest number
Program 2:
Write a program to input a value x and n and print the sum of the series:
a)1+x+x2+x3+…..xn
b)1-x+x2-x3+….xn
c)x+(x2/2)-(x3/3)+(x4/4)…(xn/n)
d)x+(x2/2!)-(x3/3!)+(x4/4!)…(xn/n!)
Codes:
a)
x=float(input("Enter a number: "))
n=int(input("Enter the power: "))
s=0
for i in range(n+1):
s+=x**i
print("The sum of first",n,"terms is",s)
Output:
Enter a number: 2
Enter the power: 3
The sum of first 3 terms is 15.0
b)
x=int(input("Enter a number: "))
n=int(input("Enter the power: "))
s=0
sign=+1
for i in range(n+1):
term=x**i*sign
s+=term
sign*=-1
print("The sum of first",n,"terms is",s)
Output:
Enter a number: 5
Enter the power: 4
The sum of first 4 terms is 521
C)
x=int(input("Enter a number: "))
n=int(input("Enter the power: "))
s=x
sign=+1
for a in range(2,n+1):
term=((x**a)*sign)/a
s+=term
sign*=-1
print("Sum of first",n,"terms is",s)
Output:
Enter a number; 2
Enter the power; 3
Sum of first 3 terms is 1.33333333
d)
x=int(input("Enter a number: "))
n=int(input("Enter the power: "))
s=x
sign=+1
for a in range(2,n+1):
f=1
for i in range(1,a+1):
f*=i
term=((x**a)*sign)/f
s+=term
sign*=-1
print("Sum of first",n,"terms is",s)
Output:
Enter a number: 2
Enter the power: 3
Sum of first 3 terms is 2.666666666666667

Program 3:
Determine whether a number is a perfect number,Armstrong number or a
palindrome number
Code:
n = int(input("Enter a number: "))
_sum = 0

# Perfect number check


for i in range(1, n):
if n % i == 0:
_sum += i

if _sum == n:
print(n, "is a perfect number")
else:
print(n, "is not a perfect number")

# Armstrong number check


_s = 0
temp = n
while temp > 0:
digit = temp % 10
_s += digit ** 3
temp //= 10

if n == _s:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")

# Palindrome number check


tem = n
rev_no = 0
while tem > 0:
digi = tem % 10
rev_no = rev_no * 10 + digi
tem //= 10

if n == rev_no:
print(n, "is a palindrome number")
else:
print(n, "is not a palindrome number")
Output:
Enter a number: 407
407 is not a perfect number
407 is an Armstrong number
407 is not a palindrome number
Program 4:
Input a number and check if it is a prime number or composite number
Code:
n = int(input("Enter a number:"))

if n > 1:
is_prime = True
for i in range(2, n):
if n % i == 0:
is_prime = False
break

if is_prime:
print(n, "is a prime number")
else:
print(n, "is a composite number")

elif n == 0 or n == 1:
print(n, "is neither prime nor composite")
else:
print("Please enter a positive integer greater than or equal to 0")
Output:
Enter a number:7
7 is a prime number
Program 5:
Program to display n terms of a Fibonacci series
Code:
n=int(input("Enter the number of terms:"))
first=0
second=1
print(first,second,end=' ')
for i in range(0,n-2):
third=first+second
print(third,end=' ')
first=second
second=third
Output:
Enter the number of terms:10
0 1 1 2 3 5 8 13 21 34
Program 6:
Compute the GCD and LCM of 2 integers
Code:
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

min_num = min(num1, num2)


gcd_result = 1
for i in range(1, min_num + 1):
if num1 % i == 0 and num2 % i == 0:
gcd_result = i

lcm_result = (num1 * num2) // gcd_result

print("GCD of", num1, "and", num2, "is", gcd_result)


print("LCM of", num1, "and", num2, "is", lcm_result)
Output:
Enter the first integer: 36
Enter the second integer: 48
GCD of 36 and 48 is 12
LCM of 36 and 48 is 144
Program 7:
Count and display the number of vowels,consonants,uppercase,lowercase
characters in a string
Code:
input_string = input("Enter a string: ")

vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0

vowels = "aeiouAEIOU"

for char in input_string:


if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1

if char.isupper():
uppercase_count += 1
else:
lowercase_count += 1

print("Number of vowels:", vowel_count)


print("Number of consonants:", consonant_count)
print("Number of uppercase characters:", uppercase_count)
print("Number of lowercase characters:", lowercase_count)
Output:
Enter a string: PyTHoN
Number of vowels: 1
Number of consonants: 5
Number of uppercase characters: 4
Number of lowercase characters: 2
Program 8:
Input a string and determine whether it is a palindrome string or not
Code:
input_string = input("Enter a string: ")

cleaned_string = ''
for c in input_string:
if c.isalnum() or c.isspace():
cleaned_string += c.lower()

if cleaned_string == cleaned_string[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Output:
Enter a string: level
The string is a palindrome.
Program 9:
Convert the case of the characters in a string. For example: if the input is
Good,then the output is gOOD
Code:
string = input("Enter a string: ")
output_string = ''

for char in string:


if char.isalpha():
if char.islower():
output_string += char.upper()
else:
output_string += char.lower()
else:
output_string += char
print("Converted string:", output_string)
Output:
Enter a string: Good
Converted string: gOOD
Program 10:
Pattern programs:
i) *
**
***
****
*****
Code:
n=int(input("Enter the number of rows"))
for i in range(n):
for j in range(i+1):
print("*",end=' ')
print()
ii) 1 2 3 4 5
1234
123
12
1
Code:
n = int(input("Enter the number of rows: "))
for i in range(n,0,-1):
for j in range(1, i + 1):
print(j, end=' ')
print()
iii) A
AB
ABC
ABCD
ABCDE
Code:
n = int(input("Enter the number of rows: "))
for i in range(n):
ch=65
for j in range(i+1):
print(chr(ch), end=' ')
ch+=1
print()

You might also like