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

Python Practical

The document contains a series of Python programming exercises with solutions. It covers tasks such as adding elements of lists, generating multiplication tables, counting vowels and consonants, reversing strings, calculating factorials, determining triangle types, finding the LCM, checking for palindromes and prime numbers, and swapping two numbers without a third variable. Each exercise includes a brief description followed by the corresponding Python code.

Uploaded by

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

Python Practical

The document contains a series of Python programming exercises with solutions. It covers tasks such as adding elements of lists, generating multiplication tables, counting vowels and consonants, reversing strings, calculating factorials, determining triangle types, finding the LCM, checking for palindromes and prime numbers, and swapping two numbers without a third variable. Each exercise includes a brief description followed by the corresponding Python code.

Uploaded by

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

Python practical

1. Write a program to add the elements of the two lists.


Ans:
S1= [32,45,40]
S2= [35,30,42,38]
print("*** List S1 ***")
print(S1, len(S1))
print("*** List S2 ***")
print(S2,len(S2))
All=S1+S2
print("*** List All ***")
print(All, len(All))
2. Write a program to print a math table of a number.
Ans:
n=int(input("Enter a number:"))
for i in range(1,11):
print(n,"X",i , "X", n*i)
3. Write a program to input a string and display the count of vowels and
consonants in the string.
Ans:
str = input("Please enter a string as you wish: ")
vowels = 0
consonants = 0
for i in str:
if i in 'aeiouAEIOU':
vowels += 1
else:
consonants += 1

print("The number of vowels:", vowels)


print("The number of consonants:", consonants)

4. Write a program to input a string and display the string in the reverse
order.
Ans:
def reverse_string(str):
str1 = ""

for i in str:
str1 = i + str1

return strl
str = "Artificialintelligence"
print("The original string is: ",str)
print("The reverse string is",reverse_string(str))

5. Write a python code to take the input of a number n and then find and
display its factorial (n!)

Ans:
n = int(input("Enter a number:"))
factorial = 1
if n < 0:
print("Factorial does not exist for negative numbers")

elif n==0:
print("The factorial of 0 is 1")

else:
for i in range(1,n + 1):
factorial = factorial*i

print("The factorial of ",n," is", factorial)

6. Write a python to input the lengths of the three sides of a triangle and
display whether a triangle can be formed with the inputs or not. If a
triangle can be formed then display whether the triangle will be scalene,
isosceles or equilateral triangle.
Ans:
print("Input the sides of the triangle: ")
A = int(input("A: "))
B = int(input("B: "))
C = int(input("C: "))

if A == B == C:
print("Equilateral triangle")

elif A==B or B==c or A==C:


print("isosceles trangle")
else:
print("Scalene triangle")

7. Write a program to input two numbers and display the LCM of the two
numbers.

Ans:
def calculate_lcm(x, y):
if x > y:
greater = x
else:
greater = y

while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater

break
greater += 1
return lcm

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


num2 = int(input("Enter second number: "))
print("The LCM of", num1, "and", num2, "is", calculate_lcm(num1,num2))

8. Write a program to input a number and display whether it is a


palindrome or not

Ans:
num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10

if(temp==rev):
print("The number is a palindrome!")

else:
print("The number is not a palindrome!")

9. Write a program to input a number and display whether it is a prime


number or not

Ans:

num = int(input("Enter a number: "))


if num > 1:
for i in range(2, int(num/2)+1)
if num % i == 0:
print(num, 'is not a prime')
break
else:
print(num, "is a prime number")

else:

print(num, "is a composite number")

10. Write a program to input two numbers and swap both the numbers
without using the third number.

Ans.

x = int(input("Enter the value of x: "))


y = int(input("Enter the value of y: "))

print("Numbers before swapping: %d %d\n" %(x,y))

x=x+y
y=x-y
x=x-y

print("Numbers after swapping: %d %d\n"%(x,y))

You might also like