Python Tutorial: Basic Program in Python

Tuesday, 21 July 2020

Basic Program in Python

List of basic programming exercises

1. Write a Python program to enter two numbers and find their sum.

num1=int(input("Enter any First numbers : "))   
num2=int(input("Enter any First numbers : "))
sum = num1 + num2
## Prints the sum of two numbers
print("Sum of %d and %d = %d" %(num1, num2, sum))
 

2. Write a Python program to enter two numbers and perform all arithmetic operations.


num1=int(input("Enter any First numbers : "))   
num2=int(input("Enter any Second numbers : "))
sum = num1 + num2
sub = num1 - num2
mult = num1 * num2
div = num1 / num2
mod = num1 % num2
##Print result of all arithmetic operations
print("SUM = ", sum)
print("DIFFERENCE = ", sub)
print("PRODUCT = ", mult)
print("QUOTIENT = ", div)
print("MODULUS = ", mod)
 

3. Write a Python program to enter length and breadth of a rectangle and find its perimeter.


length=int(input("Enter length of the rectangle: "))
width=int(input("Enter width of the rectangle: "))
##Calculate perimeter of rectangle
perimeter = 2 * (length + width)
##Print perimeter of rectangle
print("Perimeter of rectangle = %f units " %perimeter) 

4. Write a Python program to enter length and breadth of a rectangle and find its area.


length=int(input("Enter length of the rectangle: "))
width=int(input("Enter width of the rectangle: "))
##Calculate area of rectangle
area = length * width
##Print area of rectangle
print("Area of rectangle = %f sq. units "%area) 

5. Write a Python program to enter radius of a circle and find its diameter, circumference and area.

radius=float(input("Enter radius of circle: "))
## Calculate diameter, circumference and area
diameter = 2 * radius
circumference = 2 * 3.14 * radius
area = 3.14 * (radius * radius)
##Print all results
print("Diameter of circle = ",diameter ," units ")
print("Circumference of circle = %.2f units "%circumference)
print("Area of circle = %.2f sq. units "%area)
 

6. Write a Python program to enter length in centimeter and convert it into meter and kilometer.


cm=int(input("Enter length in centimeter: "))
##Convert centimeter into meter and kilometer
meter = cm / 100.0
km    = cm / 100000.0
print("Length in Meter = %.2f m" %meter)
print("Length in Kilometer = ", km," km")

7. Write a Python program to enter temperature in Celsius and convert it into Fahrenheit.


celsius=int(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9 / 5) + 32
print(celsius, " Celsius = ",fahrenheit," Fahrenheit")

8. Write a Python program to enter temperature in Fahrenheit and convert to Celsius


fahrenheit=int(input("Enter temperature in Celsius: "))
celsius = (fahrenheit - 32) * 5 / 9
print(fahrenheit, " Fahrenheit = ",celsius," Celsius")
 

9. Write a Python program to convert days into years, weeks and days.


##Input total number of days from user */
days=int(input("Enter days: "))
##Conversion
years = (days // 365)   # Ignoring leap year
weeks = (days % 365) // 7
days  = days - ((years * 365) + (weeks * 7))
##Print all resultant values
print("YEARS: ", years)
print("WEEKS: ", weeks)
print("DAYS: ", days)
 

10. Write a Python program to find power of any number x ^ y.

 
import math
##Input two numbers from user */
base=float(input("Enter base: "))
expo=float(input("Enter exponent: "))
##Calculates base^expo
power = math.pow(base, expo)
print(base,"^", expo,"=", power)

11. Write a Python program to enter any number and calculate its square root.


import math
num=float(input("Enter any number to find square root: "))
root = math.sqrt(num)
print("Square root of ", num," = ",root)

12. Write a Python program to enter two angles of a triangle and find the third angle.


a=int(input("Enter first angles of triangle: "))
b=int(input("Enter second angles of triangle: "))
##Compute third angle
c = 180 - (a + b)
print("Third angle of the triangle = ", c)

13. Write a Python program to enter base and height of a triangle and find its area.


##Input base and height of triangle */
base=int(input("Enter base of the triangle: "))
height=int(input("Enter height of the triangle: "))
##Calculate area of triangle
area = (base * height) / 2
##Print the resultant area
print("Area of the triangle = ", area," sq. units")
 

14. Write a Python program to calculate area of an equilateral triangle.


import math
##Input side of equilateral triangle
side=int(input("Enter side of an equilateral triangle: "))
##Calculate area of equilateral triangle
area = (math.sqrt(3) / 4) * (side * side)
##Print resultant area
print("Area of equilateral triangle = %0.2f units" %area)
 

15. Write a Python program to enter marks of five subjects and calculate total, average and percentage.

##Input marks of all five subjects
eng=int(input("Enter marks of English subjects: "))
phy=int(input("Enter marks of physics subjects: "))
chem=int(input("Enter marks of chemistry subjects: "))
math=int(input("Enter marks of math subjects: "))
comp=int(input("Enter marks of computer subjects: "))
## Calculate total, average and percentage
total = eng + phy + chem + math + comp
average = total / 5.0
percentage = (total / 500.0) * 100
## Print all results
print("Total marks = %.2f"%total)
print("Average marks = %.2f"%average)
print("Percentage = %.2f"%percentage)

16. Write a Python program to enter P, T, R and calculate Simple Interest.


## Input principle, rate and time
principle=int(input("Enter principle (amount): "))
time=int(input("Enter time: "))
rate=int(input("Enter rate: "))
##  Calculate simple interest
SI = (principle * time * rate) / 100
##Print the resultant value of SI
print("Simple Interest = %0.2f" %SI)

17. Write a Python program to enter P, T, R and calculate Compound Interest.


import math
## Input principle, rate and time
principle=int(input("Enter principle (amount): "))
time=int(input("Enter time: "))
rate=int(input("Enter rate: "))
#Calculate compound interest
CI = principle* (math.pow((1 + rate / 100), time))
##Print the resultant CI
print("Compound Interest = %0.2f" %CI)

18. Write a Python Program to enter a letter/character and print ASCII Value of a character.

c = input('Enter a letter :')
# print the ASCII value of assigned character in c
print("The ASCII value of " , c , " is", ord(c))

Note : ord() : It converts the given string of length one, return an integer representing the unicode code point of the character. For example, ord(‘a’) returns the integer 97.

19. Write a Python Program to enter a number  and print ASCII Value of a character.

c = int(input('Enter a number  :'))
# print the ASCII value of assigned character in c
print("The ASCII value of " , c , " is", chr(c))
20. Python program to swap two variables
x = int(input("Enter the first number"))
y = int(input("Enter the second number"))
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('The value of x after swapping: ',x)
print('The value of y after swapping: ',y)

              or

x = int(input("Enter the first number"))
y = int(input("Enter the second number"))
x, y = y, x
print("x =", x)
print("y =", y)

4 comments: