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

Python Practical Program-Xi

The document contains a summary of Python programs written on various dates to demonstrate different programming concepts. There are 16 programs described that generate patterns, calculate mathematical series, check number properties, and perform string operations. The programs cover concepts like loops, conditionals, functions, input/output, and string/number manipulation.

Uploaded by

Tapaswini
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)
191 views10 pages

Python Practical Program-Xi

The document contains a summary of Python programs written on various dates to demonstrate different programming concepts. There are 16 programs described that generate patterns, calculate mathematical series, check number properties, and perform string operations. The programs cover concepts like loops, conditionals, functions, input/output, and string/number manipulation.

Uploaded by

Tapaswini
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

S.No.

Practical Program Date of Experiment

Write a python program to input a welcome message and display it. 21-09-22
1 message=input("Welcome to MBS PS, Bhubaneswar, Computer Science Lab. : ")
print("Hello, ",message)

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

Num1=int(input(“Enter the first number:”)


2 Num2=int(input(“Enter the first number”)
if(a>b):
print(num1, “ is greater”)
else:
print(num2, “is greater”)

Write a python program to input three numbers and display the largest / smallest 24-09-22
number.

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
3
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)

4 Generate patterns using a nested loop. 28-09-22


Pattern - 1: Simple pyramid pattern
1. # This is the example of print simple pyramid pattern  
2. n = int(input("Enter the number of rows"))  
3. # outer loop to handle number of rows  
4. for i in range(0, n):  
5.     # inner loop to handle number of columns  
6.     # values is changing according to outer loop  
7.         for j in range(0, i + 1):  
8.             # printing stars  
9.             print("* ", end="")       
10.   
11.         # ending line after each row  
12.         print()  
Output:-
*
**
***
****
*****

Pattern - 2: Reverse right angle pyramid 29-09-22


1. # This is the example of print simple reversed right angle pyramid pattern  
2. rows = int(input("Enter the number of rows:"))  
3. k = 2 * rows - 2  # It is used for number of spaces  
4. for i in range(0, rows):  
5.     for j in range(0, k):  
6.         print(end=" ")  
7.     k = k - 2   # decrement k value after each iteration  
5 8.     for j in range(0, i + 1):  
9.         print("* ", end="")  # printing star  
10.     print("")  
Output:-
*
**
***
****
*****

Pattern - 3: Printing Downward half - Pyramid 01-10-22


Code -
1. rows = int(input("Enter the number of rows: "))  
2. # the outer loop is executing in reversed order  
3. for i in range(rows + 1, 0, -1):    
4.     for j in range(0, i - 1):  
5.         print("*", end=' ')  
6 6.     print(" ")  
Output:
Enter the number of rows: 5
*****
****
***
**
*
Pattern - 1: Number Pattern 02-11-22
Code -
1. rows = int(input("Enter the number of rows: "))  
2. # Outer loop will print number of rows  
3. for i in range(rows+1):  
4.     # Inner loop will print the value of i after each iteration  
5.     for j in range(i):  
6.         print(i, end=" ")  # print number  
7.     # line after each row to display pattern correctly  
7
8.     print(" ")  
Output:
Enter the number of rows: 5
1
22
333
4444
55555

Pattern - 1: Right-angled pattern with characters 03-11-22


Code -
1. print("The character pattern ")  
2. asciiValue = 65     #ASCII value of A  
3. for i in range(0, 5):  
4.     for j in range(0, i + 1):  
5.         # It will convert the ASCII value to the character  
6.         alphabate = chr(asciiValue)  
7.         print(alphabate, end=' ')  
8.         asciiValue += 1  
8
9.     print()  
Output:
The character pattern
A
BC
DEF
GHIJ
KLMNO

9 Write a program to input the value of x and n and print the sum of the following 05-11-22
series:
1) 1 + x² + x³ + … + xⁿ
Output: Sum of series in python
The output shell python window will be appeared like below:-

10 09-11-22

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


n = int (input ("Enter value of n :"))
s=0

for a in range (n + 1) :
if a%2==0:
s += x**a
else:
s -= x**a
print ("Sum of series", s)
Output : Sum of series
11 10-11-22

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


n = int (input ("Enter value of n :"))
s=0

for a in range (n + 1) :
if a%2==0:
s += (x**a)/n
else:
s -= (x**a)/n
print ("Sum of series", s)
Output : Sum of Series Program :-
12 x – x2/2! + x3/3! – x4/4! + x5/5! – x6/6! 12-11-22

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


n = int (input ("Enter value of n :"))
s=0
a=1
fact=1
for a in range (1,n + 1) :
fact=fact*a
if a%2==0:
s += (x**a)/fact
else:
s -= (x**a)/fact
print ("Sum of series", s)

Output of Sum of series in python


13 Write a program to determine whether a number is a perfect number, an 16-11-22
Armstrong number or a palindrome.
PERFECT NUMBER
n= int(input(“enter a number”))
count = 0
for i in range(1, n):
if n % i == 0:
count = count + i
if count == n:
print(n, “The number is a Perfect number!”)
else:
print(n, “The number is not a Perfect number!”)

PALINDROME NUMBER
temp = int(input(“Enter a number”))
rev = 0 #reverse is 0
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!”)

ARMSTRONG NUMBER
count = 0
temp = int(input(“Enter a number”))
while temp > 0:
digit = temp % 10
count += digit ** 3
temp //= 10 # Armstrong number is a number that is equal to the sum of cubes of
its digits
if n == count:
print(n, “is an Armstrong number”)
else:
print(n, “is not an Armstrong number”)

Write a program to input a number and check if the number is a prime or 17-11-22
composite number.
# Number to be checked for prime

n=5

# Check if the number is greater than 1

if n > 1:
14
for i in range(2, int(n/2)+1):
if (n % i) == 0:
print(num, "is not a prime number")
break
else:
print(n, "is a prime number")
# If the number is less than 1, its also not a prime number.
else:
print(n, "is not a prime number")

15 Write a program to display the n terms of a Fibonacci series. 19-11-22

# Program to display the Fibonacci sequence up to n-th term


nterms = int(input("How many terms? "))
# 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)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

How many terms? 7


Fibonacci sequence:
0
1
1
2
3
5
8

Write a python program to compute the greatest common divisor and least 23-11-22
common multiple of two integers.
# python program to find LCM of two number using GCD
#input two numbers
n1 = int(input("Enter First number :"))
n2 = int(input("Enter Second number :"))
x = n1
y = n2
16 while(n2!=0):
    t = n2
    n2 = n1 % n2
    n1 = t
gcd = n1
print("GCD of {0} and {1} = {2}".format(x,y,gcd))
lcm = (x*y)/gcd
print("LCM of {0} and {1} = {2}".format(x,y,lcm))
Write a program to count and display the number of vowels, consonants, 24-11-22
uppercase, lowercase characters in the string.
s = input("Enter any string :")
vowel = consonent = uppercase = lowercase= 0
for i in s:
    if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'or i == 'A' or i == 'E' or i == 'I' or i
== 'O' or i == 'U'):
         vowel = vowel +1
    else:
17          consonent = consonent + 1
    if i.isupper() :
        uppercase = uppercase + 1
    if i.islower():
        lowercase = lowercase + 1 
print("Total number of vowel:",vowel)
print("Total number of consonent:",consonent)
print("Total number of uppercase letter:",uppercase)
print("Total number of lowercase letter:",lowercase)

Input a string and determine whether it is palindrome or not; convert the case of 26-11-22
characters in a string.
# Program to check if a string is palindrome or not
my_str = 'aIbohPhoBiA'
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
18
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

You might also like