0% found this document useful (0 votes)
103 views

Python Questions With Solutions

The document contains Python code snippets that demonstrate various programming concepts: 1. A program that accepts a radius from the user and calculates the area of a circle. 2. A function that prints all prime numbers between 1 and 100. 3. A program that calculates the sum of three numbers or returns thrice the sum if the numbers are equal.

Uploaded by

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

Python Questions With Solutions

The document contains Python code snippets that demonstrate various programming concepts: 1. A program that accepts a radius from the user and calculates the area of a circle. 2. A function that prints all prime numbers between 1 and 100. 3. A program that calculates the sum of three numbers or returns thrice the sum if the numbers are equal.

Uploaded by

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

1.

Write a Python program which accepts the radius of a circle from the user and
compute the area

import math as M
r=int(input("Enter the radius of a circle: "))
area=M.pi*r*r
print(round(area,2))

2. Write Python function to print all prime numbers between 1 to 100.

def prime():
for Number in range (1,101):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break

if (count == 0 and Number != 1):


print(" %d" %Number, end = ' ')

prime()

3.Write a Python program to calculate the sum of three given numbers, if the values
are equal then return thrice of their sum

a=int(input("Enter 1st no. for sum: "))


b=int(input("Enter 2nd no. for sum: "))
c=int(input("Enter 3rd no. for sum: "))

if(a==b and b==c):


print(3*(a+b+c))
else:
print(a+b+c)

4. Write program to find out vowels in given string.

s=input("Enter String: ")


v="AaEeIiOoUu"

for i in s:
if i in v:
print(i)

5. Write a Python program to calculate the sum of the digits in an integer.

sum=0
n=input("Enter any number: ")
for i in n:
sum=sum+int(i)
print(sum)

6. Write a Python program to reverse the element of list.

l=[]
size=int(input("Enter the size of the list: "))
for i in range(size):
n=int(input(f"Enter the number at position {i} : "))
l.append(n)

print(l[::-1])

7. Write a Python program to display the first and last element from the following
list

l=[]
size=int(input("Enter the size of the list: "))
for i in range(size):
n=int(input(f"Enter the number at position {i} : "))
l.append(n)

print(l[0],l[len(l)-1])

8. Write a Python program to count the number of occurrences of each element in a


given list

names=[]
size=int(input("Enter the size of the list: "))
for i in range(size):
n=input(f"Enter the String at position {i} : ")
names.append(n)
d={}
for i in range(len(names)):
x=names[i]
c=0
for j in range(i,len(names)):
c=names.count(x)
count=dict({x:c})
if x not in d.keys():
d.update(count)
print (d)

9. Write a program to delete duplicate elements from list.

l=[]
f=[]
size=int(input("Enter the size of the list: "))
for i in range(size):
n=int(input(f"Enter the number at position {i} : "))
l.append(n)

for j in range(len(l)):
if l[j] not in f:
f.append(l[j])
print(f)

10. Write a program to add following element in dictionary d. 1:'TCS', 2:'S2',


3:'Pune'

l=['TCS', 'S2', 'Pune']


d={}
for i in range(len(l)):
d.update({i+1:l[i]})

print(d)

11. Write a program to delete above elements from dictionary d

d.clear()
print(d)

12. Write program to update key 2 with value 's1'

d[2]='s1'

print(d)

You might also like