1. WAP to input a welcome message and display it.
welcome_message=input("Enter welcome message : ")
print("Hello!, ",welcome_message)
Output:
Enter welcome message: Welcome to the Python World.
(Hello! Welcome to the Python World.)
>>>
2. WAP to input two numbers and display the larger / smaller number.
#input first number
num1=int(input("Enter First Number:"))
#input Second number
num2=int(input("Enter Second Number:"))
#Check if first number is greater than second
if (num1>num2):
print("The Larger number is:", num1)
else:
print ("The Larger number is:", num2)
Output:
Enter First Number:23
Enter Second Number:35
The Larger number is: 35
#input first number
num1=int(input("Enter First Number:"))
#input Second number
num2=int(input("Enter Second Number:"))
#Check if first number is lesser than second
if (num1<num2):
print("The Smaller number is:", num1)
else:
print ("The Smaller number is:", num2)
Output:
Enter First Number:23
Enter Second Number:35
The Smaller number is: 23
3. WAP to input three numbers and display the largest / smallest number.
#input first,Second and third number
num1=int(input("Enter First Number:"))
num2=int(input("Enter Second Number:"))
num3=int(input("Enter Third Number:"))
#Check if first number is greater than rest of the two numbers.
if (num1> num2 and num1> num3):
print("The Largest number is:", num1)
#Check if Second number is greater than rest of the two numbers.
elif (num2 > num1 and num2> num3):
print ("The Largest number is:", num2)
else:
print ("The Largest number is:", num3)
Enter First Number8
Enter Second Number6
Enter Third Number5
The Largest number is 8
>>>
4. WAP to generate the pyramid patterns using nested loop.
Pattern-1
for i in range(1,6):
for j in range(i):
print(" * ", end = "")
print( )
Output:
*
**
***
****
*****
Pattern-2
for i in range(5,0,-1):
for j in range(i):
print(" * ", end = "")
print( )
Output:
*****
****
***
**
*
Pattern-3
for i in range(1, 6):
for j in range(1, i+1):
print(i, end = " ")
print()
Output:
1
22
333
4444
55555
Pattern-4
for i in range(5, 0,-1):
for j in range(6,i,-1):
print(i, end = " ")
print()
Output:
5
44
333
2222
11111
5. WAP to determine whether a number is a perfect number, an armstrong number or a
palindrome.
# Palindrome (12321 is Palindrome)
number=int(input("Enter any number:"))
num=number
num1= number
rev=0
while num>0:
digit=num%10
rev=rev*10+digit
num=int(num/10)
if number==rev:
print(number ,'is a Palindrome')
else:
print(number , 'Not a Palindrome')
# Armstrong number is equal to sum of cubes of each digit
sum = 0
temp = num1
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num1 == sum:
print(num1," is an Armstrong number")
else:
print(num1," is not an Armstrong number")
# Perfect number, a positive integer that is equal to the sum of its proper divisors.
sum1 = 0
for i in range(1, number):
if(number % i == 0):
sum1 = sum1 + i
if (sum1 == number):
print(number, " The number is a Perfect number")
else:
print(number, " The number is not a Perfect number")
Output:
Enter any number:12321
(12321, 'is a Palindrome')
(12321, ' is not an Armstrong number')
(12321, ' The number is not a Perfect number')
Enter any number:2356
(2356, 'Not a Palindrome')
(2356, ' is not an Armstrong number')
(2356, ' The number is not a Perfect number')
>>>
Enter any number:153
(153, 'Not a Palindrome')
(153, ' is an Armstrong number')
(153, ' The number is not a Perfect number')
>>>
Enter any number:6
(6, 'is a Palindrome')
(6, ' is not an Armstrong number')
(6, ' The number is a Perfect number')
>>>
6. WAP to input a number and check if the number is a prime or composite number.
num = int(input("Enter any number : "))
if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, "is NOT a PRIME number, it is a COMPOSITE number")
break
else:
print(num, "is a PRIME number")
elif num == 0 or 1:
print(num, "is a neither Prime NOR Composite number")
else:
print()
Output:
Enter any number : 5
(5, 'is a PRIME number')
>>>
Enter any number : 123
(123, 'is NOT a PRIME number, it is a COMPOSITE number')
>>>
7. WAP to display the terms of a Fibonacci series.
# 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
Output:
How many terms ? 5
Fibonacci sequence:
0
1
1
2
3
8. WAP to compute the greatest common divisor and least common multiple of two integers.
# GCD PROGRAM
num1 = int(input("Enter 1st number: "))
num2 = int(input("Enter 2nd number: "))
i=1
while(i <= num1 and i <= num2):
if(num1 % i == 0 and num2 % i == 0):
gcd = i
i=i+1
print("Greatest Common Divisor (GCD) is ", gcd)
# LCM PROGRAM
if num1 > num2:
greater = num1
else:
greater = num2
while(True):
if((greater % num1 == 0) and (greater % num2 == 0)):
lcm = greater
break
greater += 1
print("The Least Common Multiple (LCM) is ", lcm)
Output:
Enter 1st number: 3
Enter 2nd number: 5
('Greatest Common Divisor (GCD) is ', 1)
('The Least Common Multiple (LCM) is ', 15)
>>>
9. WAP to Count and display the number of vowels, consonants, uppercase, lowercase
characters in string.
# Vowels & Consonants count
str = input("Type the string: ")
vowel_count=0
consonants_count=0
vowel = set("aeiouAEIOU")
for alphabet in str:
if alphabet in vowel:
vowel_count=vowel_count +1
elif alphabet == chr(32):
consonants_count=consonants_count
else:
consonants_count=consonants_count+1
print("Number of Vowels in ",str," is :",vowel_count)
print("Number of Consonants in ",str," is :",consonants_count)
# Upper and lower case count
uppercase_count=0
lowercase_count=0
for elem in str:
if elem.isupper():
uppercase_count += 1
elif elem.islower():
lowercase_count += 1
print("Number of UPPER Case in ",str,"' is :",uppercase_count)
print("Number of lower case in ",str,"' is :",lowercase_count)
Output:
Type the string: "Welcome to St. Michael’s School. Best school to study. "
('Number of Vowels in ', "Welcome to St. Michael’s School. Best school to study. ", ' is :', 20)
('Number of Consonants in ', "Welcome to St. Michael’s School. Best school to study. " ' is :',
37)
('Number of UPPER Case in ', "Welcome to St. Michael’s School. Best school to study. " "' is
:", 2)
('Number of lower case in ', "Welcome to St. Michael’s School. Best school to study. "," is :",
54)
>>>
10. WAP to input a string and determine whether it is a palindrome or not; convert the case of
characters in a string.
str=input("Enter a string:")
w=""
for element in str[::-1]:
w = w+element
if (str==w):
print(str, 'is a Palindrome string')
else:
print(str,' is NOT a Palindrome string')
Output:
Enter a string:"welcome"
('welcome', ' is NOT a Palindrome string')
>>>
Enter a string:"kanak"
('kanak', 'is a Palindrome string')
>>>
11. WAP to find the largest/smallest number in a list/tuple.
list1 = []
# Input number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
element= int(input("Enter elements: "))
list1.append(element)
# print Smallest element
print("Smallest element in List1 is:", min(list1))
Output:
Enter number of elements in list: 5
Enter elements: 36
Enter elements: 53
Enter elements: 49
Enter elements: 22
Enter elements: 29
('Smallest element in List1 is:', 22)
>>>
12. WAP to input a list of numbers and swap elements at the even location with the elements at
the odd location.
# Entering 5 element Lsit
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
# printing original list
print("The original list : " + str(mylist))
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(mylist)):
if i % 2:
even_i.append(mylist[i])
else :
odd_i.append(mylist[i])
result = odd_i + even_i
# print result
print("Separated odd and even index list: " + str(result))
Output:
Enter 5 elements for the list:
4
7
16
7
9
The original list : [4, 7, 16, 7, 9]
Separated odd and even index list: [4, 16, 9, 7, 7]
>>>
13. WAP to input a list/tuple of elements, search for a given element in the list/tuple.
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)
Output:
Enter 5 elements for the list:
45
34
37
34
55
Enter an element to be search:
34
('\nElement found at Index:', 1)
('Element found at Position:', 2)
('\nElement found at Index:', 3)
('Element found at Position:', 4)
>>>
14. WAP to input a list of numbers and find the smallest and largest number from the list.
#create empty list
mylist = []
number = int(input('How many elements to put in List: '))
for n in range(number):
element = int(input('Enter element '))
mylist.append(element)
print("Maximum element in the list is :", max(mylist))
print("Minimum element in the list is :", min(mylist))
Output:
How many elements to put in List: 5
Enter element 34
Enter element 23
Enter element 55
Enter element 11
Enter element 99
('Maximum element in the list is :', 99)
('Minimum element in the list is :', 11)
>>>
15. WAP to create a dictionary with the roll number, name and marks of n students in a class and
display the names of students who have scored marks above 75.
no_of_std = int(input("Enter number of students: "))
result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks is/are",(result[student][0]))
Output:
Enter number of students: 5
('Enter Details of student No.', 1)
Roll No: 1
Student Name: "Amit"
Marks: 78
('Enter Details of student No.', 2)
Roll No: 2
Student Name: "Abhay"
Marks: 78
('Enter Details of student No.', 3)
Roll No: 3
Student Name: "Pooja"
Marks: 76
('Enter Details of student No.', 4)
Roll No: 4
Student Name: "Aarti"
Marks: 60
('Enter Details of student No.', 5)
Roll No: 5
Student Name: "Harshit"
Marks: 55
{1: ['Amit', 78], 2: ['Abhay', 78], 3: ['Pooja', 76], 4: ['Aarti', 60], 5: ['Harshit', 55]}
("Student's name who get more than 75 marks is/are", 'Amit')
("Student's name who get more than 75 marks is/are", 'Abhay')
("Student's name who get more than 75 marks is/are", 'Pooja')
>>>