PythonPrograms
PythonPrograms
#Program1:
#Write a python program to ask your name and age and display it on the screen.
name=input("Enter name:")
age=input("Enter age:")
print("Your Name:",name)
print("Your Age:",age)
#Program2:
#Write a python program to input four numbers and display sum and average.
n1=int(input("Enter number1:"))
n2=int(input("Enter number2:"))
n3=int(input("Enter number3:"))
n4=int(input("Enter number4:"))
s=n1+n2+n3+n4
avg=s/4
print("Sum =",s)
print("Average =",avg)
#Program3:
#Write a python program to input base and height and calculate the area of the
#triangle.
radius=float(input("Enter radius:"))
volume=4/3*22/7*radius**3
surfacearea=4*22/7*radius**2
print("Volume of sphere =",round(volume,2))
print("Surface area of sphere =",round(surfacearea,2))
#Program5:
#Write a python program to input a number and display whether the number is
#even or odd.
num=int(input("Enter a number:"))
rem=num%2
if rem==0:
print("Number is even")
else:
print("Number is odd")
#Program6:
#Write a python program to input marks and calculate grade
marks=int(input("Enter marks:"))
if marks>=80:
grade='A'
elif marks>=60 and marks<80:
grade='B'
elif marks>=40 and marks<60:
grade='C'
else:
grade='D'
print("Grade = ",grade)
#Program7:
#Write a python program to input 3 numbers and display the largest number.
n1=int(input("Enter number1:"))
n2=int(input("Enter number2:"))
n3=int(input("Enter number3:"))
if n1>n2 and n1>n3:
#if n1>n2>n3: #alternative way
largest=n1
elif n2>n1 and n2>n3:
largest=n2
else:
largest=n3
print("Largest = ",largest)
#Program8:
#Write a python program to print a multiplication table of any number entered
#by the user.
num=int(input("Enter a number:"))
for i in range(1,11):
product=num*i
print(num,'X',i,'=',product)
#Program9:
#Write a python program to print the first 10 natural numbers.
n=int(input("Enter a number:"))
for i in range(1,n+1):
print(i)
#Program10:
#Write a python program to print all the factors of a number.
n=int(input("Enter a number:"))
for i in range(1,n+1):
if n%i==0:
print(i)
#Program11:
#Write a python program to input N numbers and calculate sum and average.
studlist = ["Namrata","Rahul","Anjali","Rakesh","John"]
print(studlist)
print("Total number of elements in list:",len(studlist))
name=studlist.pop(1)
print(name,"deleted!!!")
print(studlist)
#Program13:
#Write a python program to create a list of 5 numbers and perform the
#operations
numlist=[]
for i in range(5):
num=int(input("Enter a number:"))
numlist.append(num)
print(numlist)
print("Length of list:",len(numlist))
print("2nd to 4th element:",numlist[1:4])
print("3rd to 5th element:",numlist[-3:])
numlist=[]
for i in range(2,21,2):
numlist.append(i)
print(numlist)
for i in range(len(numlist)):
numlist[i]=numlist[i]+10
print("Updated list:",numlist)
#Program15:
#Write a python program to create a list and perform the operations.
numlist=[]
N=int(input("How many numbers to enter in list?"))
for i in range(N):
num=int(input("Enter a number:"))
numlist.append(num)
print(numlist)
newlist=[12,13,14]
numlist.extend(newlist)
print("List after extend operation:",numlist)
numlist.sort()
print("List after sorting:",numlist)
print("Maximum element in list:",max(numlist))
numlist.reverse()
print("List after reversal:",numlist)