Python Examples
Python Examples
Python Examples
2 Python Program to Exchange the Values of Two Numbers Without Using a Temporary
Variable
ANS:
a=int(input("Enter value of first variable: "))
b=int(input("Enter value of second variable: "))
a=a+b
b=a-b
a=a-b
print("a is:",a," b is:",b)
6 Python Program to Take in the Marks of 5 Subjects and Display the Grade
ANS
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80&avg<90):
print("Grade: B")
elif(avg>=70&avg<80):
print("Grade: C")
elif(avg>=60&avg<70):
print("Grade: D")
else:
print("Grade: F")
15 Python Program to Print all Integers that Aren’t Divisible by Either 2 or 3 and Lie
between 1 and 50.
ANS:
for i in range(0,51):
if(i%2!=0&i%3!=0):
print(i)
16 Python Program to Read a Number n And Print the Series "1+2+…..+n= "
ANS:
n=int(input("Enter a number: "))
a=[]
for i in range(1,n+1):
print(i,sep=" ",end=" ")
if(i<n):
print("+",sep=" ",end=" ")
a.append(i)
print("=",sum(a))
print()
20 Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes
ANS:
n=int(input("Enter upper limit of range: "))
sieve=set(range(2,n+1))
while sieve:
prime=min(sieve)
print(prime,end="\t")
sieve-=set(range(prime,n+1,prime))
print()
2 Python Program to Exchange the Values of Two Numbers Without Using a Temporary
Variable
ANS:
a=int(input("Enter value of first variable: "))
b=int(input("Enter value of second variable: "))
a=a+b
b=a-b
a=a-b
print("a is:",a," b is:",b)
6 Python Program to Take in the Marks of 5 Subjects and Display the Grade
ANS
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80&avg<90):
print("Grade: B")
elif(avg>=70&avg<80):
print("Grade: C")
elif(avg>=60&avg<70):
print("Grade: D")
else:
print("Grade: F")
9 Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
ANS:
a=int(input("Enter first number:"))
15 Python Program to Print all Integers that Aren’t Divisible by Either 2 or 3 and Lie between
1 and 50.
ANS:
for i in range(0,51):
if(i%2!=0&i%3!=0):
print(i)
16 Python Program to Read a Number n And Print the Series "1+2+…..+n= "
ANS:
n=int(input("Enter a number: "))
a=[]
for i in range(1,n+1):
print(i,sep=" ",end=" ")
if(i<n):
print("+",sep=" ",end=" ")
a.append(i)
print("=",sum(a))
print()
17 Python Program to Read a Number n and Print the Natural Numbers Summation Pattern
ANS:
n=int(input("Enter a number: "))
for j in range(1,n+1):
20 Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes
ANS:
n=int(input("Enter upper limit of range: "))
sieve=set(range(2,n+1))
while sieve:
prime=min(sieve)
print(prime,end="\t")
sieve-=set(range(prime,n+1,prime))
print()