Sunday, June 26, 2022

Basic Python Program

 

 Basic Python Program

Write a program to display the message "Hello World" on to the Screen.
print("Hello World")

Output
Hello World


Write a program to display the message "Hello World" in two separate lines on to the Screen.
print("Hello \nWorld")

Output
Hello
World


Alternative method
print("Hello")
print("World")

Output
Hello
World


Write a program to display the message "Hello World" within double quotes.
print('"Hello World"')

Output
"Hello World"


Write a program to add two number. The numbers are accepted from the user.
a=int(input("Enter A"))
b=int(input("Enter B"))
c=a+b
print("Sum of two numbers ",c)

Output
Enter A 12
Enter B 3
Sum of two numbers  15


Write a program to find the sum and product of two number. The numbers are accepted from the user.
a=int(input("Enter A"))
b=int(input("Enter B"))
c=a+b
d=a*b
print("Sum of two numbers ",c)
print("Product of two numbers ",d)

Output
Enter A 12
Enter B 3
Sum of two numbers  15
Product of two numbers  36

Write a program to accept the length and breadth of a rectangle and find the area and perimeter of the rectangle.
l=int(input("Enter Length"))
b=int(input("Enter Breadth"))
a=l*b
p=2*(l+b)
print("Area of Rectangle ",a)
print("Perimeter of Rectangle ",p)

Output
Enter Length 4
Enter Breadth 5
Area of Rectangle  20
Perimeter of Rectangle  18


Write a program to find the area and circumference of the circle.
r=float(input("Enter Radius"))
a=3.142*r*r
c=2*3.142*r
print("Area of Circle ",a)
print("Circumference of Circle ",c)

Output
Enter Radius 7
Area of Circle  153.958
Circumference of Circle  43.988


Write a program to accept the distance in kilometer and convert it to meter, centimeter, millimeter
km=int(input("Enter Distance in Kilometer"))
m=km*1000
cm=m*100
mm=cm*10
print("Distance in meter ",m)
print("Distance in centimeter ",cm)
print("Distance in millimeter ",mm)

Output
Enter Distance in Kilometer 7
Distance in meter  7000
Distance in centimeter  700000
Distance in millimeter  7000000


Write a program to accept the memory capacity in kilobytes in bytes and bits.
kb=int(input("Enter Capacity in Kilobytes"))
b=1024*kb
t=b*8
print("Capacity in bytes ",b)
print("Capacity in bits ",t)

Output
Enter Capacity in Kilobytes 10
Capacity in bytes  10240
Capacity in bits  81920


Write a program to accept the temperature in degree centigrade and convert to temperature in degree Fahrenheit.
c=float(input("Enter temperature in centigrade"))
f=9*c/5+32
print("Temperature in Fahrenheit",f)

Output
Enter temperature in centigrade 100
Temperature in Fahrenheit 212.0


Write a program to accept the time in seconds and convert it to hour, minutes and seconds.
s=int(input("Enter time in seconds"))
h=s//3600
rs=s%3600
m=rs//60
rs=rs%60
print("Hour : ",h)
print("Minute : ",m)
print("Second : ",rs)

Output
Enter time in seconds3662
Hour :  1
Minute :  1
Second :  2


Write a program to accept the amount and give the denomination in 2000,500,200,100 as given by ATM.
a=int(input("Enter the Amount in Rupees"))
th=a//2000
ra=a%2000
fh=ra//500
ra=ra%500
tw=ra//200
ra=ra%200
oh=ra//100
print("2000 x ",th)
print("500 x ",fh)
print("200 x ",tw)
print("100 x ",oh)

Output
Enter the Amount in Rupees3800
2000 x  1
500 x  3
200 x  1
100 x  1


Selection Structure

Write a program to check whether the person is eligible to vote or not.
age=int(input("Enter the age"))
if age>=18:
    print("Eligible to vote")
else:
    print("Not Eligible to vote")

Output
Enter the age 19
Eligible to vote
Enter the age 17
Not Eligible to vote


Write a program to accept the temperature of the day if the temperature >= 40 display "Hot Day" else display "Cold Day"
t=int(input("Accept the Temperature"))
if t>=40:
    print("Hot Day")
else:
    print("Cold Day")

Output
Accept the Temperature41
Hot Day
Accept the Temperature30
Cold Day


Write a program to accept the marks of three subjects. If the total>=150 display "Pass" else "Try Hard"
m1=int(input("Enter Mark 1"))
m2=int(input("Enter Mark 2"))
m3=int(input("Enter Mark 3"))
t=m1+m2+m3
if t>=150:
    print("Pass")
else:
    print("Try Hard")

Output
Enter Mark 1 56
Enter Mark 2 78
Enter Mark 3 45
Pass
Enter Mark 123
Enter Mark 234
Enter Mark 345
Try Hard


Write a program to accept a number and check whether the number is odd or even.
n=int(input("Enter number"))
if n%2==0:
    print("Even")
else:
    print("Odd")

Output
Enter number23
Odd
Enter number24
Even


A special two-digit number is such that when the sum of its digits is added to the product of its digits , the result is equal to original number
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59

n=int(input("Enter the number"))
fd=n//10
ld=n%10
if n==(fd+ld)+(fd*ld):
    print("Special Number")
else:
    print("Not Special Number")

Output
Enter the number 59
Special Number
Enter the number 45
Not Special Number


Write a program to accept a non century year and check whether the leap year or not.
y=int(input("Enter year"))
if y%100==0:
    if y%400==0:
        print("leap year")
    else:
        print("not leap year")
else:
    print("leap year")

Output
Enter year 2000
leap year
Enter year 2020
leap year
Enter year 2100
not leap year


Accept a number and check whether the number is negative, positive and zero.
n=int(input("Enter number"))
if n<0:
    print("Negative Number")
elif n>0:
    print("Positive Number")
else:
    print("Zero")

Output
Enter number -45
Negative Number
Enter number 40
Positive Number
Enter number 0
Zero


Accept the cost price and selling price and find out whether he has made profit, loss, no profit no loss.
cp=int(input("Enter Cost Price"))
sp=int(input("Enter Selling Price"))
if cp>sp:
    print("Loss")
elif sp>cp:
    print("Profit")
else:
    print("No profit no loss")

Output
Enter Cost Price100
Enter Selling Price120
Profit


Accept the weight from the user. If the weight >100 display "Over  Weight" if the weight>=40 display "normal weight" else display "below weight"

wt=int(input("Enter Weight"))
if wt>100:
    print("Over Weight")
elif wt>=40:
    print("Normal Weight")
else:
    print("Below Weight")

Output
Enter Weight 120
Over Weight
Enter Weight 74
Normal Weight


Accept the age and display the following message
            Age        Remark
            <=12    Child
            <=19    Teenager
            <=35    Youth
            <=60    Adult
            >60    Senior Citizen

age=int(input("Enter Age"))
if age<=12:
    print("Child")
elif age<=19:
    print("Teenager")
elif age<=35:
    print("Youth")
elif age<=60:
    print("Adult")
elif age>60:
    print("Senior Citizen")

Output
Enter Age 40
Adult
Enter Age 22
Youth


Accept three marks of a student. If all the marks are greater than or equal to 40 declare it Pass else Fail
m1=int(input("Enter Mark1"))
m2=int(input("Enter Mark2"))
m3=int(input("Enter Mark3"))
if m1>=40 and m2>=40 and m3>=40:
    print("Pass")
else:
    print("Fail")

Output
Enter Mark1 45
Enter Mark2 55
Enter Mark3 60
Pass
Enter Mark1 25
Enter Mark2 60
Enter Mark3 90
Fail

Accept three sides of the triangle and find the greatest side.
a=int(input("Accept Side A"))
b=int(input("Accept Side B"))
c=int(input("Accept Side C"))
if a>b and a>c:
    print("Side A")
elif b>a and b>c:
    print("Side B")
else:
    print("Side C")

Output
Accept Side A 5
Accept Side B 6
Accept Side C 4
Side B

Accept three sides of the triangle and find the type of triangle "Equilateral", "Isosceles", "Scalene" triangle
a=int(input("Accept Side A"))
b=int(input("Accept Side B"))
c=int(input("Accept Side C"))
if a==b and a==c and b==c:
    print("Equilateral Triangle")
elif b==a or b==c or c==a:
    print("Isosceles Triangle")
else:
    print("Scalene Triangle")

Output
Accept Side A 6
Accept Side B 6
Accept Side C 6
Equilateral Triangle
Accept Side A 6
Accept Side B 5
Accept Side C 5
Isosceles Triangle

A shopkeeper gives a discount on the purchase of Laptop. The discount is as follows
Purchase Amount        Discount
<=35000            No discount
<=55000            5%
>55000            10%
Display the Discount Amount and Pay away price to the shopkeeper.

pa=int(input("Enter the Purchase Amount"))
if pa<35000:
    d=0
elif pa<=55000:
    d=.05*pa
else:
    d=.10*pa
print("Discount =",d)
pap=pa-d
print("Pay away Price =",pap)

Output
Enter the Purchase Amount 40000
Discount = 2000.0
Pay away Price = 38000.0


Write a Program to calculate charges for sending parcels through couriers when the charges are as follows. For the first 1 Kg Rs.15.00 , For additional weight , for every 500 gm or fraction thereof: Rs 5.00
wt=int(input("Enter weight in grams"))
if wt<=1000:
    ch=15
else:
    ch=15+(wt-1000)/500*5
print("Charges ",ch)

Output
Enter weight in grams 2500
Charges  30.0
Enter weight in grams 500
Charges  15


Write a program to calculate the tax of the employee on his salary. The salary is accepted from the user to calculate the tax.
upto 100000   :  No tax
From Rs. 100001 to Rs.150000 : 10% of income tax
From Rs.150001  to Rs.250000 :  Rs.5000+20% of tax
Above 250000 : Rs.25000+30% of tax

sal=int(input("Enter the Salary"))
if sal<=100000:
    tax=0
elif sal<=150000:
    tax=.10*(sal-100000)
elif sal<=250000:
    tax=5000+.20*(sal-150000)
else:
    tax=25000+.30*(sal-250000)
print("Tax paid ",tax)

Output
Enter the Salary 300000
Tax paid  40000.0
Enter the Salary200000
Tax paid  15000.0


Write a program to calculate and print the electricity bill to be paid by a customer. The customer pays a monthly rent of Rs.350.00
No. of units — Charge per unit
Upto 100 units — Rs. 2.50
For the next 100 units — Rs. 3.00
For next 200 units — Rs. 3.50
Beyond 400 units — Rs. 4.00

ut=int(input("Enter the unit consumed"))
if ut<=100:
    b=350+ut*2.50
elif ut<=200:
    b=350+250+(ut-100)*3.00
elif ut<=400:
    b=350+250+300+(ut-200)*3.50
else:
    b=350+250+300+700+(ut-400)*4.00
print("Bill ",b)

Output
Enter the unit consumed 500
Bill  2000.0



Basic Python Program

   Basic Python Program Write a program to display the message "Hello World" on to the Screen. print("Hello World") Outp...