0% found this document useful (0 votes)
5 views11 pages

Q - 1) WAP To Check Whether Ad Number Is Armstrong or Not: Code

The document contains a series of Python programming tasks and their corresponding code snippets. These tasks include checking for Armstrong numbers, reversing numbers, drawing patterns, checking for palindromes, string manipulations, list operations, and tuple operations. Each task is presented with a brief description followed by the relevant code implementation.

Uploaded by

ayushloveshaurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Q - 1) WAP To Check Whether Ad Number Is Armstrong or Not: Code

The document contains a series of Python programming tasks and their corresponding code snippets. These tasks include checking for Armstrong numbers, reversing numbers, drawing patterns, checking for palindromes, string manipulations, list operations, and tuple operations. Each task is presented with a brief description followed by the relevant code implementation.

Uploaded by

ayushloveshaurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Q - 1) WAP to check whether ad number is Armstrong or not:

CODE :
n = int(input("Enter the number: "))
a=0
org = n
while n!= 0:
r = n%10
a = a + r**3
n//= 10
if org == a:
print("yes its an Armstrong")
else :
print("not an Armstrong")

Q- 2) WAP to reverse a number:


CODE:
n= int(input("Enter the number: "))
c=0
while n!=0:
r = n%10
n//=10
c = c*10 +r
print(c)
Q-3) WAP to draw pattern:

CODE:
n=5
for i in range(1,n+1):
for j in range(n-i):
print(" ",end=" ")
for j in range(i,1,-1):
print(j,end=" ")
for j in range(1,i+1):
print(j, end=" ")
print()

Q-4) WAP to draw pattern:

CODE:
n=5
for i in range(n,0,-1):
for j in range(n-i):
print(" ",end=" ")
for j in range(i,1,-1):
print(j,end=" ")
for j in range(1,i+1):
print(j, end=" ")
print()

Q – 5) WAP to check whether the program is palindrome:


CODE:
n= int(input("Enter the number: "))
c=0
org = n
while n!=0:
r = n%10
n//=10
c = c*10 + r
print(c)
if org == c:
print(org,"is a palindrome")
else:
print(org,"is not a palindrome")

Q-6) Spliting a string:


CODE:
s = "Class XI - Computer Science"
print(s[:4])
print(s[10:1:-1])
print(s[::3])
print(s[-7:-4:2])
print(s[9:0:-3])
print(s[15:1:-5])
Q-7) Progamm to read a string and a character from the user and count the
number of times the character occurred in a string:

CODE:

s=input("Enter a String: ")


ch=input("Enter a character: ")
c=0
for i in s:
if i==ch:
c=c+1
print(ch," occures ",c,'times in the string')

Q-8) upper and lower case in string:

CODE:

s = "Hello World"
print(s)
print("Lower = ",s.lower())
print("Upper = ",s.upper())
Q-9) replace all vowels by *:
CODE:
s=input("Enter a String")
c=''
for i in s:
if i in 'AEIOUaieou':
c+='*'
else:
c+=i
print("New String =",c)

Q-10) Finding the longest and the shortest word in a string:


CODE:
input_string = input("Enter a sentence: ")
words = input_string.split()
longest_word = max(words, key=len)
shortest_word = min(words, key=len)
print(f"The longest word is: '{longest_word}'")
print(f"The shortest word is: '{shortest_word}'")
Q-11) REVERSING EACH LETTER OF A STRING:
CODE :
input_string = "Hello world from Python"
words = input_string.split()
reversed_words = []
for word in words:
reversed_word = word[::-1]
reversed_words.append(reversed_word)
result = " ".join(reversed_words)
print("Original String:", input_string)
print("Reversed String:", result)

Q-12) WAP to read all words in string that start from a specific letter:
CODE:
input_string = "Apple banana apricot berry avocado"
specific_letter = "a"
specific_letter = specific_letter.lower()
words = input_string.split()
matching_words = []
for word in words:
if word.lower().startswith(specific_letter):
matching_words.append(word)
print("Original String:", input_string)
print(f"Words starting with '{specific_letter}':", matching_words)

Q-13)Adding two lists:


CODE:
l1=[4,5,6,7]
l2=[7,4,3,1]
print(l1+l2)

Q-14) CREATING A LIST:


CODE:
l=[]
while True:
n=int(input("Enter the element to add in list: "))
l.append(n)
ch=input("Do you wish to continue (y/n): " )
if ch=='n' or ch=='N':
break
print("Entered list is ", l)
print("Greatest element in list is: ",max(l))

Q-15) The sum of all the elements:


CODE:
l=eval(input("Enter list"))
s=0
for i in l:
s+=i
print("Sum =",s)

Q-16) The sum of all the odd and even elements:

CODE:
l = eval(input("Enter list"))
s=0
c=0
for i in l:
if i%2==0:
s+= i
else:
c+= i
print("Sum of even = ",s)
print("Sum of odd = ",c)

Q-17) Finding the longest word in a list:

CODE:
input_list = ["apple", "banana", "cherry", "blueberry"]
longest_word = max(input_list, key=len)
print("Longest word:", longest_word)
Q-18) Finding the pairs for a target sum:

CODE:
input_list = [1, 3, 2, 4, 5, 3]
target_sum = int(input("Enter the sum you have target: "))
pairs = []
seen = set()
for num in input_list:
complement = target_sum - num
if complement in seen:
pairs.append((complement, num))
seen.add(num)
print(f"Pairs that add up to {target_sum}:", pairs)

Q-19) Write a Python program to test if a variable is a list or tuple or a set:

CODE:
x=input("Enter a list or tuple or a set")
if type(x) is tuple:
print("It is a tuple")
elif type(x) is list:
print("It is a list")
else:
print("It is a set")

Q-20) Write a Python program which accepts a sequence of comma-separated


numbers from user and generate a list and a tuple with those numbers :

CODE:
values = input("Input some comma seprated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)

Q21) Write a python program to print sum of tuple elements:

CODE:
test_tup = (7, 5, 9, 1, 10, 3)
print("The original tuple is : " + str(test_tup))
res = sum(list(test_tup))
print("The sum of all tuple elements are : " + str(res))

Q22) Write a python program to Check if the given element is present in tuple or
not:
CODE:
test_tup = (10, 4, 5, 6, 8)
N = int(input("value to be checked:"))
res = False
for ele in test_tup :
if N == ele :
res = True
break
print("Does contain required value ? : " + str(res))

Q23) Write a Python program to get the 4th element from the last element of a
tuple.

CODE:
tuplex = ("p","y","t","h","o","n","p","r","o",7)
print(tuplex)
item= tuplex[-4]
print(item)

You might also like