Python program to solve the given math formula.

In this python basic program, we will solve the given math formula.
Formula : (a + b)2 = a^2 + b^2 + 2ab

Steps to solve the program
  1. Take two variables as input and assign value to them.
  2. Using logic solve the given math formula.
  3. Formula : (a + b)2 = a^2 + b^2 + 2ab
  4. Print the output.
				
					a = 2
b = 3
result = a**2+2*a*b+b**2

print("(a+b)^2: ",result)
				
			

Output :

				
					(a+b)^2:  25
				
			

Solve this Pythagorous theorem.

solve the given math formula.
Formula : (a – b)2 = a^2 + b^2 – 2ab

Program to interchange values between variables.

In this Python basic program, we will interchange values between variables.

Steps to solve the program

  1. Take two variables as input and assign value to them.
  2. Interchange their values using “, = “.
  3. Print the variables after interchanging the values.

Program:

a = 50
b = 70
a, b = b, a

print("value of a :", a)
print("value of b :", b)

Output:

value of a : 70
value of b : 50


Print the square and cube of a given number.

In this basic Python program, we will print the square and cube of a given number using the ** method.

Steps to solve the program

  1. Take a number as input.
  2. Print the square and cube using the ** (power) method.
  3. To print the square use  **2 and for the cube use **3.
  4. Print the output.

Program :

n = 9

print("Square of number :", n**2)

print("Cube of number :", n**3)

Output :

Square of number: 81
Cube of number: 729



Program to get the median of given numbers.

This Python basic program will get the median of given numbers. 

Steps to solve the program

  1. Take a list as input.
  2. Sort the given list using sort().
  3. Get the length of the list.
  4. If the length of the list is odd, then use the formula
    (n+1)/2 to find the median of the given list.
  5. If the length of the list is odd, then use the formula.
    Use the formula (n+1)/2 to find the median of the given list, where n is the length of the list.
  6. If the length of the list is even, then use the formula.
    Use the formula ((n/2 – 1) + n/2)/2 to find the median of the given list, where n is the length of the list
  7. Print the output.


Program:

# example list of values
values = [10, 20, 30, 40, 50]

# sort the list
values.sort()

# get length of the list
n = len(values)

if n % 2 == 1:
    median_value = values[n // 2]
else:
    median_value = (values[n // 2 - 1] + values[n // 2]) / 2

print(f"The median is: {median_value}")

Output:

The median is: 30


Program to get the Average of given numbers.

In this basic Python program, we will get the Average of given numbers.

Steps to solve the program
  1. Take three numbers as input.
  2. The average is calculated by adding the numbers divided by the total numbers ( i.e a+b+c/3).
  3. Print the output.

    Program :
a = 30
b = 20
c = 40

print("Average of Three Numbers : ", (a + b + c)/3)

Output :

Average of Three Numbers : 30.0


Program to repeat a given string 5 times.

In this Python basic program, we will repeat a given string 5 times.

Steps to solve the program
  1. Take a string as input.
  2. To repeat the string 5 times multiply the given string by 5.
  3. Print the output

Program:

str1 = "SQATools"
n1 = 5

print("Result: ", str1*n1)

Output:

Result: SQAToolsSQAToolsSQAToolsSQAToolsSQATools


Program to multiply two numbers.

In this python basic program, we will multiply two numbers using the * operator.

Steps to solve the program

1. Take two numbers as input.
2. Multiply two numbers using the “*” operator.
3. Print the result.

Programs

num1 = 20
num2 = 40

print("Multiplication of num1*num2 :", num1 * num2)

Output

Multiplication of num1*num2 : 800


Program to subtract two integers.

In this Python basic program, we will subtract two integers using the – operator.

Steps to solve the program

1. Take two numbers as input.
2. Subtract the two numbers using the “–” operator.
3. Print the result

Program:

num1 = 80
num2 = 20

print("Subtraction of num1 - num2 :", num1 - num2)

Output:

Subtraction of num1- num2 : 60


Program to add two integers

Steps to solve the program

1. Take two numbers as input.
2. Add the two numbers using the + operator.
3. Print the result.

Program:

num1 = 30
num2 = 40

print("Addition of num1+num2 :", num1+num2)

Output:

Addition of num1+num2 : 70



Python Basic Programs, Exercises

Python basic programs contains Python Programming Examples with all native Python data type, mathematical operations on Python Variables. Typecasting of Python Variables and get understanding of Python Fundamentals.

1). Python Program to add two integer values.

2). Python Program to subtract two integer values.

3). Python program to multiply two numbers.

4). Python program to repeat a given string 5 times.
Input :
str1 = “SQATools”
Output :
“SQAToolsSQAToolsSQAToolsSQAToolsSQATools” 

5). Python program to get the Average of given numbers.
Formula: sum of all the number/ total number
Input:
a = 40
b = 50
c = 30
Output :
Average = 40

6). Python program to get the median of given numbers.
Note: all the numbers should be arranged in ascending order
Formula : (n+1)/2
n = Number of values
Input : [45, 60, 61, 66, 70, 77, 80]
Output:  66

7). Python program to print the square and cube of a given number.
Input :
num1 = 9
Output :
Square = 81
Cube =   729

8). Python program to interchange values between variables.
Input :
a = 10
b = 20
Output :
a = 20
b = 10

9). Python program to solve this Pythagorous theorem.
Theorem : (a2 + b2 = c2)

10). Python program to solve the given math formula.
Formula : (a + b)2 = a^2 + b^2 + 2ab

11). Python program to solve the given math formula.
Formula : (a – b)2 = a^2 + b^2 – 2ab

12). Python program to solve the given math formula.
Formula : a2 – b2 = (a-b)(a+b)

13). Python program to solve the given math formula.
Formula : (a + b)3 = a3 + 3ab(a+b) + b3 

14). Python program to solve the given math formula.
Formula : (a – b)3 = a3 – 3a2b + 3ab2 – b3

15). Python program to calculate the area of the square.
Formula : area = a*a

16). Python program to calculate the area of a circle.
Formula = PI*r*r
r = radius
PI = 3.14

17). Python program to calculate the area of a cube.
Formula = 6*a*a

18). Python program to calculate the area of the cylinder.
Formula = 2*PI*r*h + 2*PI*r*r

19). Python program to check whether the given number is an Armstrong number or not.
Example: 153 = 1*1*1 + 5*5*5 + 3*3*3

20). Python program to calculate simple interest.
Formula = P+(P/r)*t
P = Principle Amount
r = Anual interest rate
t = time

21). Python program to print the current date in the given format
Output: 2023 Jan 05
Note: Use the DateTime library

22). Python program to calculate days between 2 dates.
Input date : (2023, 1, 5) (2023, 1, 22)
Output: 17 days

23). Python program to get the factorial of the given number.

24). Python program to reverse a given number.

25). Python program to get the Fibonacci series between 0 to 50.

26). Python program to check given number is palindrome or not.

27). Python program to calculate compound interest.

28). Python program to check the prime number.

29). Python program to check leap year.

30). Python program to check for the anagram.
Note: rearrangement of the letters of a word to another word, using all the original letters exactly once.

31). Python program to generate random numbers.

32). Python program to generate a random string with a specific length.

33). Python program to get the current date.

34). Python program to convert Decimal to Binary.

35). Python program to find the sum of natural numbers.

36). Python program to find HCF.

37). Python program to find LCM.

38). Python program to find the square root of a number.
Note: Use the math library to get the square root.

39). Python program to calculate the volume of a sphere.
Formula = (4/3*pi*r^2)
r = radius
pi = 3

40). Python program to perform mathematical operations on two numbers.