PYTHON BASIC PROGRAMS
#1.Hello
world program.
print("Hello World")
#2.Addition of two
numbers.
a=int(input("Enter the first
number"))
b=int(input("Enter the second number"))
print(a+b)
#3.To find the
square root &$ check the program.
a=float(input("enter the number")
# b= a ** 0.5
print(b)
a=float(input("enter the base of triangle"))
b=float(input("enter the height of triangle"))
c=(1/2)*a*b
print("The area of triangle is",c)
OR
a=float(input("enter the base side of triangle"))
b=float(input("enter the 2nd side of triangle"))
c=float(input("enter the 3rd side of triangle"))
d=(a+b+c)/2
area=(d*(d-a)*(d-b)*(d-c))**0.5
print(area)
#5.Swapping 2 variables.
a=int(input("enter the number1"))
b=(int(input("enter the second number")))
c=a
a=b
b=c
print(a,b)
#6.Program to
convert km to miles.
a=float(input("Enter the value in km"))
b=a*0.62137
print(b,"miles")
#7.Celcius to
farenheit.
a=float(input("Enter the value in calcius"))
b=(a*1.8)+32
print(b)
#8.To check number is positive, negative or 0.
a=(int(input("Enter the number")))
if a>0:
print("positive number")
elif a<0:
print("Negative number")
else:
print("Neither negative nor positive")
#9.To check no is odd or even.
a=int(input("enter the number"))
if a%2==0:
print("even number")
else:
print("odd number")
#10.To check leap
year.
l=int(input("enter the year"))
if l%4==0:
if l%400==0:
if l%100==0:
print("IS LEAP YEAR")
else:
print("Is not a leap year")
else:
print("Is a leap year")
else:
print("not a leap year")
#11.Find largest
among three numbers.
a=int(input("enter the 1st number"))
b=int(input("enter the 2nd number"))
c=int(input("enter the 3rd number"))
if a>b and a>c:
print(a," is the greatest number")
elif b>a and b>c:
print(b," is greatest number")
else:
print(c," is greatest number")
#12.To check prime
number.
count=False
a=int(input("enter the number"))
for i in range (2,a):
if (a%i==0):
count=True
break
if count:
print("Not a prime number")
else:
print("prime number")
#13.To print all prime numbers in range.
for n in range(1,100,1):
for i in range(2,n):
if n%i==0:
break
else:
print(n,"prime number")
#14.Factorial of a
number.
n=int(input("enter the number"))
def fact(n):
if(n==1):
return 1
return (n * fact(n-1))
print(fact(n))
OR
n=int(input("enter the number"))
fact=1
if n >1:
for i in range(1, n + 1, 1):
fact=fact*i
print("Factorial of number is",fact)
else:
print("fact not possible")
#15.Multiplication
table.
n=int(input("Enter the number"))
for i in range(1,11,1):
a=n*i
print(n,"*",i,"=",a)
#16.Fibonacci
sequence.
a=int(input("Enter the limit"))
n=0
n1=1
count=0
while count<a:
print(n)
new=n+n1
n=n1
n1=new
count+=1
#17.To check
armstrong number.
a=int(input("Enter the number"))
c=str(a)
b=len(c)
sum=0
temp=a
while temp>0:
dig=temp%10
sum+=dig**b
temp//=10
if a==sum:
print(a,"Armstrong number")
else:
print(a,"Not an armstrong number")
#18.To check
armstrong number in interval.
a=int(input("Enter the upper limit"))
b=int(input("Enter the lower limit"))
for i in range(b,a+1):
sum=0
temp=i
k=len(str(i))
while temp>0:
dig=temp%10
sum+=dig**k
temp//=10
if i==sum:
print(i,"Armstrong number")
#19.To find sum of
natural numbers.
sum=0
a=int(input("Enter the number"))
while a>0:
sum+=a
a-=1
print("Sum of natural numbers is ",sum)
#20.Program to find
number divisible by another number.
a=int(input("Enter the number to be checked"))
b=int(input("Enter the number with which a is to be checked"))
if a%b==0:
print(a,"is divisible by",b)
else:
print(a,'is not divisible',b)
#21.Program to
convert Decimal to binary,octal and hexadecimal.
a=int(input("Enter the number"))
print(bin(a),"in binary")
print(oct(a),"in octal")
print(hex(a),"in hexadecimal")
in output 0b is suffix for binary
0o for octal
0x for hexadecimal
#22.Program to find
ASCII value of character.
a=input("Enter the character")
d=ord(a)
print("The ASCII value of",a,"is",d)
#23.To find HCF or GCD.
# HCF can only be less than or equal to smaller number.
x=int(input("enter the first number"))
y=int(input("enter the second number"))
def HCF(x,y):
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if ((x%i==0) and (y%i==0)):
hcf=i
return hcf
print("HCF is",HCF(x,y))
#24.LCM
#Lcm can be greater than or equal to lagre number.
def 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
x=int(input("enter the first number"))
y=int(input("enter the
2nd number"))
print("LCM is",lcm(x,y))
#25.Factors of a
number.
a=int(input("enter the number"))
for i in range(1,a+1,1):
if a %i==0:
print(i)
#26.Simple
calculator.
def add(x,y):
return x+y
def sub(x,y):
return x-y
def mul(x,y):
return x*y
def div(x,y):
return x/y
def mod(x,y):
return x%y
print("Select operation")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Modulous")
while True:
choice=input("enter choice 1,2,3,4,5")
if choice in ("1","2","3","4","5"):
a=float(input("Enter the 1st number"))
b=float(input("Enter the 2nd number"))
if choice == "1":
print(a,"+",b,"=",add(a,b))
elif choice == "2":
print(a,"-",b,"=",sub(a,b))
elif choice == "3":
print(a,"*",b,"=",mul(a,b))
elif choice == "4":
print(a,"/",b,"=",div(a,b))
elif choice == "5":
print(a,"%",b,"=",mod(a,b))
break
else:
print("invalid input")
#27.Print calendar.
import calendar
yy=int(input("Enter the year"))
mm=int(input("Enter the month"))
print(calendar.month(yy,mm))
#28.Sum of natural numbers using recursion.
def recur_sum(n):
if n <= 1:
return n
else:
return n + recur_sum(n-1)
n = int(input("Enter the number"))
recur_sum(n)
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))
#29.To remove
punctuations from a string.
punct= '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
str="Hello!! My name is
\<Isha>/. What is your name?.."
punctuation=""
for char in str:
if char not in punct:
punctuation+=char
print(punctuation)
Comments