Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

PythonPrograms

The document contains a series of Python programs that demonstrate basic programming concepts such as input/output, arithmetic operations, control structures, and list manipulations. Each program addresses a specific task, including calculating sums, averages, areas, and handling user input for various scenarios. The examples serve as practical exercises for beginners to learn Python programming.

Uploaded by

4956jhanvi
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)
13 views

PythonPrograms

The document contains a series of Python programs that demonstrate basic programming concepts such as input/output, arithmetic operations, control structures, and list manipulations. Each program addresses a specific task, including calculating sums, averages, areas, and handling user input for various scenarios. The examples serve as practical exercises for beginners to learn Python programming.

Uploaded by

4956jhanvi
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/ 15

Python Programs

#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.

base=float(input("Enter base of triangle:"))


height=float(input("Enter height of triangle:"))
area=1/2*base*height
print("Area =",area)
#Program4:
#Write a python program to input radius and calculate volume and surface area
#of the sphere.

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.

N=int(input("Enter how many numbers?"))


I=0
S=0
while I<=N:
Num=int(input("Enter a number:"))
S=S+Num
I=I+1
print("Sum = ",S)
Avg = S/N
print("Average = ", Avg)
#Program12:
#Write a python program to create list of students selected for science quiz and
#perform the operations on the list

studlist = ["Namrata","Rahul","Anjali","Rakesh","John"]
print(studlist)
print("Total number of elements in list:",len(studlist))

name=input("Enter a name to delete:")


studlist.remove(name)
print(studlist)

name=input("Enter a name to append:")


studlist.append(name)
print(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:])

newval=int(input("Enter a new value to replace second number:"))


numlist[1]=newval
print("Updated list:",numlist)
#Program14:
#Write a python program to create a list of first 10 even numbers and increment
#each item in a list with 10 and print the final list.

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)

You might also like